Format the Java codes with the Reat Hat extension.

This commit is contained in:
krahets 2023-04-14 00:12:10 +08:00
parent 7273ee24e8
commit f8513455b5
39 changed files with 195 additions and 205 deletions

3
.gitignore vendored
View File

@ -13,3 +13,6 @@ docs/overrides/
build/ build/
site/ site/
utils/ utils/
# test script
test.sh

View File

@ -13,8 +13,7 @@ public class array {
/* 随机返回一个数组元素 */ /* 随机返回一个数组元素 */
static int randomAccess(int[] nums) { static int randomAccess(int[] nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字 // 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = ThreadLocalRandom.current(). int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
nextInt(0, nums.length);
// 获取并返回随机元素 // 获取并返回随机元素
int randomNum = nums[randomIndex]; int randomNum = nums[randomIndex];
return randomNum; return randomNum;

View File

@ -20,7 +20,7 @@ class MyList {
nums = new int[capacity]; nums = new int[capacity];
} }
/* 获取列表长度(即当前元素数量)*/ /* 获取列表长度(即当前元素数量) */
public int size() { public int size() {
return size; return size;
} }

View File

@ -8,7 +8,6 @@ package chapter_computational_complexity;
import java.util.*; import java.util.*;
public class leetcode_two_sum { public class leetcode_two_sum {
/* 方法一:暴力枚举 */ /* 方法一:暴力枚举 */
static int[] twoSumBruteForce(int[] nums, int target) { static int[] twoSumBruteForce(int[] nums, int target) {
@ -40,7 +39,7 @@ public class leetcode_two_sum {
public static void main(String[] args) { public static void main(String[] args) {
// ======= Test Case ======= // ======= Test Case =======
int[] nums = { 2,7,11,15 }; int[] nums = { 2, 7, 11, 15 };
int target = 9; int target = 9;
// ====== Driver Code ====== // ====== Driver Code ======

View File

@ -52,7 +52,8 @@ public class space_complexity {
/* 线性阶(递归实现) */ /* 线性阶(递归实现) */
static void linearRecur(int n) { static void linearRecur(int n) {
System.out.println("递归 n = " + n); System.out.println("递归 n = " + n);
if (n == 1) return; if (n == 1)
return;
linearRecur(n - 1); linearRecur(n - 1);
} }
@ -73,7 +74,8 @@ public class space_complexity {
/* 平方阶(递归实现) */ /* 平方阶(递归实现) */
static int quadraticRecur(int n) { static int quadraticRecur(int n) {
if (n <= 0) return 0; if (n <= 0)
return 0;
// 数组 nums 长度为 n, n-1, ..., 2, 1 // 数组 nums 长度为 n, n-1, ..., 2, 1
int[] nums = new int[n]; int[] nums = new int[n];
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length); System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
@ -82,7 +84,8 @@ public class space_complexity {
/* 指数阶(建立满二叉树) */ /* 指数阶(建立满二叉树) */
static TreeNode buildTree(int n) { static TreeNode buildTree(int n) {
if (n == 0) return null; if (n == 0)
return null;
TreeNode root = new TreeNode(0); TreeNode root = new TreeNode(0);
root.left = buildTree(n - 1); root.left = buildTree(n - 1);
root.right = buildTree(n - 1); root.right = buildTree(n - 1);

View File

@ -81,7 +81,8 @@ public class time_complexity {
/* 指数阶(递归实现) */ /* 指数阶(递归实现) */
static int expRecur(int n) { static int expRecur(int n) {
if (n == 1) return 1; if (n == 1)
return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1; return expRecur(n - 1) + expRecur(n - 1) + 1;
} }
@ -97,13 +98,15 @@ public class time_complexity {
/* 对数阶(递归实现) */ /* 对数阶(递归实现) */
static int logRecur(float n) { static int logRecur(float n) {
if (n <= 1) return 0; if (n <= 1)
return 0;
return logRecur(n / 2) + 1; return logRecur(n / 2) + 1;
} }
/* 线性对数阶 */ /* 线性对数阶 */
static int linearLogRecur(float n) { static int linearLogRecur(float n) {
if (n <= 1) return 1; if (n <= 1)
return 1;
int count = linearLogRecur(n / 2) + int count = linearLogRecur(n / 2) +
linearLogRecur(n / 2); linearLogRecur(n / 2);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@ -114,7 +117,8 @@ public class time_complexity {
/* 阶乘阶(递归实现) */ /* 阶乘阶(递归实现) */
static int factorialRecur(int n) { static int factorialRecur(int n) {
if (n == 0) return 1; if (n == 0)
return 1;
int count = 0; int count = 0;
// 1 个分裂出 n // 1 个分裂出 n
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {

View File

@ -12,6 +12,7 @@ import java.util.*;
class Entry { class Entry {
public int key; public int key;
public String val; public String val;
public Entry(int key, String val) { public Entry(int key, String val) {
this.key = key; this.key = key;
this.val = val; this.val = val;
@ -21,6 +22,7 @@ class Entry {
/* 基于数组简易实现的哈希表 */ /* 基于数组简易实现的哈希表 */
class ArrayHashMap { class ArrayHashMap {
private List<Entry> buckets; private List<Entry> buckets;
public ArrayHashMap() { public ArrayHashMap() {
// 初始化数组包含 100 个桶 // 初始化数组包含 100 个桶
buckets = new ArrayList<>(); buckets = new ArrayList<>();
@ -39,7 +41,8 @@ class ArrayHashMap {
public String get(int key) { public String get(int key) {
int index = hashFunc(key); int index = hashFunc(key);
Entry pair = buckets.get(index); Entry pair = buckets.get(index);
if (pair == null) return null; if (pair == null)
return null;
return pair.val; return pair.val;
} }
@ -89,13 +92,12 @@ class ArrayHashMap {
/* 打印哈希表 */ /* 打印哈希表 */
public void print() { public void print() {
for (Entry kv: entrySet()) { for (Entry kv : entrySet()) {
System.out.println(kv.key + " -> " + kv.val); System.out.println(kv.key + " -> " + kv.val);
} }
} }
} }
public class array_hash_map { public class array_hash_map {
public static void main(String[] args) { public static void main(String[] args) {
/* 初始化哈希表 */ /* 初始化哈希表 */
@ -124,15 +126,15 @@ public class array_hash_map {
/* 遍历哈希表 */ /* 遍历哈希表 */
System.out.println("\n遍历键值对 Key->Value"); System.out.println("\n遍历键值对 Key->Value");
for (Entry kv: map.entrySet()) { for (Entry kv : map.entrySet()) {
System.out.println(kv.key + " -> " + kv.val); System.out.println(kv.key + " -> " + kv.val);
} }
System.out.println("\n单独遍历键 Key"); System.out.println("\n单独遍历键 Key");
for (int key: map.keySet()) { for (int key : map.keySet()) {
System.out.println(key); System.out.println(key);
} }
System.out.println("\n单独遍历值 Value"); System.out.println("\n单独遍历值 Value");
for (String val: map.valueSet()) { for (String val : map.valueSet()) {
System.out.println(val); System.out.println(val);
} }
} }

View File

@ -37,15 +37,15 @@ public class hash_map {
/* 遍历哈希表 */ /* 遍历哈希表 */
System.out.println("\n遍历键值对 Key->Value"); System.out.println("\n遍历键值对 Key->Value");
for (Map.Entry <Integer, String> kv: map.entrySet()) { for (Map.Entry<Integer, String> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue()); System.out.println(kv.getKey() + " -> " + kv.getValue());
} }
System.out.println("\n单独遍历键 Key"); System.out.println("\n单独遍历键 Key");
for (int key: map.keySet()) { for (int key : map.keySet()) {
System.out.println(key); System.out.println(key);
} }
System.out.println("\n单独遍历值 Value"); System.out.println("\n单独遍历值 Value");
for (String val: map.values()) { for (String val : map.values()) {
System.out.println(val); System.out.println(val);
} }
} }

View File

@ -9,7 +9,6 @@ package chapter_heap;
import include.*; import include.*;
import java.util.*; import java.util.*;
public class heap { public class heap {
public static void testPush(Queue<Integer> heap, int val) { public static void testPush(Queue<Integer> heap, int val) {
heap.offer(val); // 元素入堆 heap.offer(val); // 元素入堆

View File

@ -41,9 +41,9 @@ class MaxHeap {
/* 交换元素 */ /* 交换元素 */
private void swap(int i, int j) { private void swap(int i, int j) {
int a = maxHeap.get(i), int a = maxHeap.get(i);
b = maxHeap.get(j), int b = maxHeap.get(j);
tmp = a; int tmp = a;
maxHeap.set(i, b); maxHeap.set(i, b);
maxHeap.set(j, tmp); maxHeap.set(j, tmp);
} }
@ -111,7 +111,8 @@ class MaxHeap {
if (r < size() && maxHeap.get(r) > maxHeap.get(ma)) if (r < size() && maxHeap.get(r) > maxHeap.get(ma))
ma = r; ma = r;
// 若节点 i 最大或索引 l, r 越界则无需继续堆化跳出 // 若节点 i 最大或索引 l, r 越界则无需继续堆化跳出
if (ma == i) break; if (ma == i)
break;
// 交换两节点 // 交换两节点
swap(i, ma); swap(i, ma);
// 循环向下堆化 // 循环向下堆化
@ -127,7 +128,6 @@ class MaxHeap {
} }
} }
public class my_heap { public class my_heap {
public static void main(String[] args) { public static void main(String[] args) {
/* 初始化大顶堆 */ /* 初始化大顶堆 */

View File

@ -25,7 +25,7 @@ public class bubble_sort {
} }
} }
/* 冒泡排序(标志优化)*/ /* 冒泡排序(标志优化) */
static void bubbleSortWithFlag(int[] nums) { static void bubbleSortWithFlag(int[] nums) {
// 外循环待排序元素数量为 n-1, n-2, ..., 1 // 外循环待排序元素数量为 n-1, n-2, ..., 1
for (int i = nums.length - 1; i > 0; i--) { for (int i = nums.length - 1; i > 0; i--) {
@ -40,7 +40,8 @@ public class bubble_sort {
flag = true; // 记录交换元素 flag = true; // 记录交换元素
} }
} }
if (!flag) break; // 此轮冒泡未交换任何元素直接跳出 if (!flag)
break; // 此轮冒泡未交换任何元素直接跳出
} }
} }

View File

@ -38,7 +38,8 @@ public class merge_sort {
/* 归并排序 */ /* 归并排序 */
static void mergeSort(int[] nums, int left, int right) { static void mergeSort(int[] nums, int left, int right) {
// 终止条件 // 终止条件
if (left >= right) return; // 当子数组长度为 1 时终止递归 if (left >= right)
return; // 当子数组长度为 1 时终止递归
// 划分阶段 // 划分阶段
int mid = (left + right) / 2; // 计算中点 int mid = (left + right) / 2; // 计算中点
mergeSort(nums, left, mid); // 递归左子数组 mergeSort(nums, left, mid); // 递归左子数组

View File

@ -47,7 +47,8 @@ public class radix_sort {
// 获取数组的最大元素用于判断最大位数 // 获取数组的最大元素用于判断最大位数
int m = Integer.MIN_VALUE; int m = Integer.MIN_VALUE;
for (int num : nums) for (int num : nums)
if (num > m) m = num; if (num > m)
m = num;
// 按照从低位到高位的顺序遍历 // 按照从低位到高位的顺序遍历
for (int exp = 1; exp <= m; exp *= 10) for (int exp = 1; exp <= m; exp *= 10)
// 对数组元素的第 k 位执行计数排序 // 对数组元素的第 k 位执行计数排序

View File

@ -11,6 +11,7 @@ import java.util.*;
/* 基于数组实现的栈 */ /* 基于数组实现的栈 */
class ArrayStack { class ArrayStack {
private ArrayList<Integer> stack; private ArrayList<Integer> stack;
public ArrayStack() { public ArrayStack() {
// 初始化列表动态数组 // 初始化列表动态数组
stack = new ArrayList<>(); stack = new ArrayList<>();

View File

@ -13,6 +13,7 @@ class ListNode {
int val; // 节点值 int val; // 节点值
ListNode next; // 后继节点引用指针 ListNode next; // 后继节点引用指针
ListNode prev; // 前驱节点引用指针 ListNode prev; // 前驱节点引用指针
ListNode(int val) { ListNode(int val) {
this.val = val; this.val = val;
prev = next = null; prev = next = null;

View File

@ -27,7 +27,8 @@ class AVLTree {
/* 获取平衡因子 */ /* 获取平衡因子 */
public int balanceFactor(TreeNode node) { public int balanceFactor(TreeNode node) {
// 空节点平衡因子为 0 // 空节点平衡因子为 0
if (node == null) return 0; if (node == null)
return 0;
// 节点平衡因子 = 左子树高度 - 右子树高度 // 节点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right); return height(node.left) - height(node.right);
} }
@ -98,7 +99,8 @@ class AVLTree {
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
private TreeNode insertHelper(TreeNode node, int val) { private TreeNode insertHelper(TreeNode node, int val) {
if (node == null) return new TreeNode(val); if (node == null)
return new TreeNode(val);
/* 1. 查找插入位置,并插入节点 */ /* 1. 查找插入位置,并插入节点 */
if (val < node.val) if (val < node.val)
node.left = insertHelper(node.left, val); node.left = insertHelper(node.left, val);
@ -121,7 +123,8 @@ class AVLTree {
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
private TreeNode removeHelper(TreeNode node, int val) { private TreeNode removeHelper(TreeNode node, int val) {
if (node == null) return null; if (node == null)
return null;
/* 1. 查找节点,并删除之 */ /* 1. 查找节点,并删除之 */
if (val < node.val) if (val < node.val)
node.left = removeHelper(node.left, val); node.left = removeHelper(node.left, val);
@ -152,7 +155,8 @@ class AVLTree {
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */ /* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private TreeNode getInOrderNext(TreeNode node) { private TreeNode getInOrderNext(TreeNode node) {
if (node == null) return node; if (node == null)
return node;
// 循环访问左子节点直到叶节点时为最小节点跳出 // 循环访问左子节点直到叶节点时为最小节点跳出
while (node.left != null) { while (node.left != null) {
node = node.left; node = node.left;

View File

@ -25,7 +25,8 @@ class BinarySearchTree {
/* 构建二叉搜索树 */ /* 构建二叉搜索树 */
public TreeNode buildTree(int[] nums, int i, int j) { public TreeNode buildTree(int[] nums, int i, int j) {
if (i > j) return null; if (i > j)
return null;
// 将数组中间节点作为根节点 // 将数组中间节点作为根节点
int mid = (i + j) / 2; int mid = (i + j) / 2;
TreeNode root = new TreeNode(nums[mid]); TreeNode root = new TreeNode(nums[mid]);
@ -41,11 +42,14 @@ class BinarySearchTree {
// 循环查找越过叶节点后跳出 // 循环查找越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 目标节点在 cur 的右子树中 // 目标节点在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num)
cur = cur.right;
// 目标节点在 cur 的左子树中 // 目标节点在 cur 的左子树中
else if (cur.val > num) cur = cur.left; else if (cur.val > num)
cur = cur.left;
// 找到目标节点跳出循环 // 找到目标节点跳出循环
else break; else
break;
} }
// 返回目标节点 // 返回目标节点
return cur; return cur;
@ -54,49 +58,62 @@ class BinarySearchTree {
/* 插入节点 */ /* 插入节点 */
public TreeNode insert(int num) { public TreeNode insert(int num) {
// 若树为空直接提前返回 // 若树为空直接提前返回
if (root == null) return null; if (root == null)
return null;
TreeNode cur = root, pre = null; TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出 // 循环查找越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 找到重复节点直接返回 // 找到重复节点直接返回
if (cur.val == num) return null; if (cur.val == num)
return null;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子树中 // 插入位置在 cur 的左子树中
else cur = cur.left; else
cur = cur.left;
} }
// 插入节点 val // 插入节点 val
TreeNode node = new TreeNode(num); TreeNode node = new TreeNode(num);
if (pre.val < num) pre.right = node; if (pre.val < num)
else pre.left = node; pre.right = node;
else
pre.left = node;
return node; return node;
} }
/* 删除节点 */ /* 删除节点 */
public TreeNode remove(int num) { public TreeNode remove(int num) {
// 若树为空直接提前返回 // 若树为空直接提前返回
if (root == null) return null; if (root == null)
return null;
TreeNode cur = root, pre = null; TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出 // 循环查找越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 找到待删除节点跳出循环 // 找到待删除节点跳出循环
if (cur.val == num) break; if (cur.val == num)
break;
pre = cur; pre = cur;
// 待删除节点在 cur 的右子树中 // 待删除节点在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num)
cur = cur.right;
// 待删除节点在 cur 的左子树中 // 待删除节点在 cur 的左子树中
else cur = cur.left; else
cur = cur.left;
} }
// 若无待删除节点则直接返回 // 若无待删除节点则直接返回
if (cur == null) return null; if (cur == null)
return null;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 child = null / 该子节点 // 当子节点数量 = 0 / 1 child = null / 该子节点
TreeNode child = cur.left != null ? cur.left : cur.right; TreeNode child = cur.left != null ? cur.left : cur.right;
// 删除节点 cur // 删除节点 cur
if (pre.left == cur) pre.left = child; if (pre.left == cur)
else pre.right = child; pre.left = child;
else
pre.right = child;
} }
// 子节点数量 = 2 // 子节点数量 = 2
else { else {
@ -113,7 +130,8 @@ class BinarySearchTree {
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */ /* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
public TreeNode getInOrderNext(TreeNode root) { public TreeNode getInOrderNext(TreeNode root) {
if (root == null) return root; if (root == null)
return root;
// 循环访问左子节点直到叶节点时为最小节点跳出 // 循环访问左子节点直到叶节点时为最小节点跳出
while (root.left != null) { while (root.left != null) {
root = root.left; root = root.left;

View File

@ -15,7 +15,8 @@ public class binary_tree_dfs {
/* 前序遍历 */ /* 前序遍历 */
static void preOrder(TreeNode root) { static void preOrder(TreeNode root) {
if (root == null) return; if (root == null)
return;
// 访问优先级根节点 -> 左子树 -> 右子树 // 访问优先级根节点 -> 左子树 -> 右子树
list.add(root.val); list.add(root.val);
preOrder(root.left); preOrder(root.left);
@ -24,7 +25,8 @@ public class binary_tree_dfs {
/* 中序遍历 */ /* 中序遍历 */
static void inOrder(TreeNode root) { static void inOrder(TreeNode root) {
if (root == null) return; if (root == null)
return;
// 访问优先级左子树 -> 根节点 -> 右子树 // 访问优先级左子树 -> 根节点 -> 右子树
inOrder(root.left); inOrder(root.left);
list.add(root.val); list.add(root.val);
@ -33,7 +35,8 @@ public class binary_tree_dfs {
/* 后序遍历 */ /* 后序遍历 */
static void postOrder(TreeNode root) { static void postOrder(TreeNode root) {
if (root == null) return; if (root == null)
return;
// 访问优先级左子树 -> 右子树 -> 根节点 // 访问优先级左子树 -> 右子树 -> 根节点
postOrder(root.left); postOrder(root.left);
postOrder(root.right); postOrder(root.right);

View File

@ -6,9 +6,7 @@
package include; package include;
/** /* Definition for a singly-linked list node */
* Definition for a singly-linked list node
*/
public class ListNode { public class ListNode {
public int val; public int val;
public ListNode next; public ListNode next;
@ -17,11 +15,7 @@ public class ListNode {
val = x; val = x;
} }
/** /* Generate a linked list with an array */
* Generate a linked list with an array
* @param arr
* @return
*/
public static ListNode arrToLinkedList(int[] arr) { public static ListNode arrToLinkedList(int[] arr) {
ListNode dum = new ListNode(0); ListNode dum = new ListNode(0);
ListNode head = dum; ListNode head = dum;
@ -32,12 +26,7 @@ public class ListNode {
return dum.next; return dum.next;
} }
/** /* Get a list node with specific value from a linked list */
* Get a list node with specific value from a linked list
* @param head
* @param val
* @return
*/
public static ListNode getListNode(ListNode head, int val) { public static ListNode getListNode(ListNode head, int val) {
while (head != null && head.val != val) { while (head != null && head.val != val) {
head = head.next; head = head.next;

View File

@ -8,7 +8,6 @@ package include;
import java.util.*; import java.util.*;
class Trunk { class Trunk {
Trunk prev; Trunk prev;
String str; String str;
@ -21,11 +20,7 @@ class Trunk {
public class PrintUtil { public class PrintUtil {
/** /* Print a matrix (Array) */
* Print a matrix (Array)
* @param <T>
* @param matrix
*/
public static <T> void printMatrix(T[][] matrix) { public static <T> void printMatrix(T[][] matrix) {
System.out.println("["); System.out.println("[");
for (T[] row : matrix) { for (T[] row : matrix) {
@ -34,11 +29,7 @@ public class PrintUtil {
System.out.println("]"); System.out.println("]");
} }
/** /* Print a matrix (List) */
* Print a matrix (List)
* @param <T>
* @param matrix
*/
public static <T> void printMatrix(List<List<T>> matrix) { public static <T> void printMatrix(List<List<T>> matrix) {
System.out.println("["); System.out.println("[");
for (List<T> row : matrix) { for (List<T> row : matrix) {
@ -47,10 +38,7 @@ public class PrintUtil {
System.out.println("]"); System.out.println("]");
} }
/** /* Print a linked list */
* Print a linked list
* @param head
*/
public static void printLinkedList(ListNode head) { public static void printLinkedList(ListNode head) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
while (head != null) { while (head != null) {
@ -64,18 +52,12 @@ public class PrintUtil {
* The interface of the tree printer * The interface of the tree printer
* This tree printer is borrowed from TECHIE DELIGHT * This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/ * https://www.techiedelight.com/c-program-print-binary-tree/
* @param root
*/ */
public static void printTree(TreeNode root) { public static void printTree(TreeNode root) {
printTree(root, null, false); printTree(root, null, false);
} }
/** /* Print a binary tree */
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
public static void printTree(TreeNode root, Trunk prev, boolean isLeft) { public static void printTree(TreeNode root, Trunk prev, boolean isLeft) {
if (root == null) { if (root == null) {
return; return;
@ -107,10 +89,7 @@ public class PrintUtil {
printTree(root.left, trunk, false); printTree(root.left, trunk, false);
} }
/** /* Helper function to print branches of the binary tree */
* Helper function to print branches of the binary tree
* @param p
*/
public static void showTrunks(Trunk p) { public static void showTrunks(Trunk p) {
if (p == null) { if (p == null) {
return; return;
@ -120,22 +99,14 @@ public class PrintUtil {
System.out.print(p.str); System.out.print(p.str);
} }
/** /* Print a hash map */
* Print a hash map
* @param <K>
* @param <V>
* @param map
*/
public static <K, V> void printHashMap(Map<K, V> map) { public static <K, V> void printHashMap(Map<K, V> map) {
for (Map.Entry <K, V> kv: map.entrySet()) { for (Map.Entry<K, V> kv : map.entrySet()) {
System.out.println(kv.getKey() + " -> " + kv.getValue()); System.out.println(kv.getKey() + " -> " + kv.getValue());
} }
} }
/** /* Print a heap (PriorityQueue) */
* Print a heap (PriorityQueue)
* @param queue
*/
public static void printHeap(Queue<Integer> queue) { public static void printHeap(Queue<Integer> queue) {
List<Integer> list = new ArrayList<>(queue); List<Integer> list = new ArrayList<>(queue);
System.out.print("堆的数组表示:"); System.out.print("堆的数组表示:");

View File

@ -8,9 +8,7 @@ package include;
import java.util.*; import java.util.*;
/** /* Definition for a binary tree node. */
* Definition for a binary tree node.
*/
public class TreeNode { public class TreeNode {
public int val; // 节点值 public int val; // 节点值
public int height; // 节点高度 public int height; // 节点高度
@ -21,11 +19,7 @@ public class TreeNode {
val = x; val = x;
} }
/** /* Generate a binary tree given an array */
* Generate a binary tree given an array
* @param list
* @return
*/
public static TreeNode listToTree(List<Integer> list) { public static TreeNode listToTree(List<Integer> list) {
int size = list.size(); int size = list.size();
if (size == 0) if (size == 0)
@ -34,14 +28,16 @@ public class TreeNode {
TreeNode root = new TreeNode(list.get(0)); TreeNode root = new TreeNode(list.get(0));
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }}; Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
int i = 0; int i = 0;
while(!queue.isEmpty()) { while (!queue.isEmpty()) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
if (++i >= size) break; if (++i >= size)
break;
if (list.get(i) != null) { if (list.get(i) != null) {
node.left = new TreeNode(list.get(i)); node.left = new TreeNode(list.get(i));
queue.add(node.left); queue.add(node.left);
} }
if (++i >= size) break; if (++i >= size)
break;
if (list.get(i) != null) { if (list.get(i) != null) {
node.right = new TreeNode(list.get(i)); node.right = new TreeNode(list.get(i));
queue.add(node.right); queue.add(node.right);
@ -50,23 +46,19 @@ public class TreeNode {
return root; return root;
} }
/** /* Serialize a binary tree to a list */
* Serialize a binary tree to a list
* @param root
* @return
*/
public static List<Integer> treeToList(TreeNode root) { public static List<Integer> treeToList(TreeNode root) {
List<Integer> list = new ArrayList<>(); List<Integer> list = new ArrayList<>();
if(root == null) return list; if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }}; Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
while(!queue.isEmpty()) { while (!queue.isEmpty()) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
if(node != null) { if (node != null) {
list.add(node.val); list.add(node.val);
queue.add(node.left); queue.add(node.left);
queue.add(node.right); queue.add(node.right);
} } else {
else {
list.add(null); list.add(null);
} }
} }

View File

@ -11,6 +11,7 @@ import java.util.*;
/* 顶点类 */ /* 顶点类 */
public class Vertex { public class Vertex {
public int val; public int val;
public Vertex(int val) { public Vertex(int val) {
this.val = val; this.val = val;
} }

View File

@ -1,3 +1 @@
__pycache__ __pycache__
test_all.sh