From 7fdb4d1443f909a277555c882e5565febc40de93 Mon Sep 17 00:00:00 2001 From: krahets Date: Sat, 12 Nov 2022 22:48:46 +0800 Subject: [PATCH] Add a chapter of binary tree. --- .../leetcode_two_sum.java | 5 +- .../space_complexity_types.java | 2 +- .../time_complexity_types.java | 2 +- .../worst_best_time_complexity.java | 2 +- .../chapter_stack_and_queue/array_queue.java | 4 +- .../linkedlist_queue.java | 2 +- codes/java/chapter_tree/binary_tree.java | 34 +++++++++++ codes/java/chapter_tree/binary_tree_bfs.java | 36 +++++++++++ codes/java/chapter_tree/binary_tree_dfs.java | 60 +++++++++++++++++++ docs/index.md | 1 + mkdocs.yml | 12 +++- 11 files changed, 150 insertions(+), 10 deletions(-) rename codes/java/chapter_computational_complexity/{space_time_tradeoff => }/leetcode_two_sum.java (94%) rename codes/java/chapter_computational_complexity/{space_complexity => }/space_complexity_types.java (97%) rename codes/java/chapter_computational_complexity/{time_complexity => }/time_complexity_types.java (98%) rename codes/java/chapter_computational_complexity/{time_complexity => }/worst_best_time_complexity.java (95%) create mode 100644 codes/java/chapter_tree/binary_tree.java create mode 100644 codes/java/chapter_tree/binary_tree_bfs.java create mode 100644 codes/java/chapter_tree/binary_tree_dfs.java diff --git a/codes/java/chapter_computational_complexity/space_time_tradeoff/leetcode_two_sum.java b/codes/java/chapter_computational_complexity/leetcode_two_sum.java similarity index 94% rename from codes/java/chapter_computational_complexity/space_time_tradeoff/leetcode_two_sum.java rename to codes/java/chapter_computational_complexity/leetcode_two_sum.java index 786574d..447fc66 100644 --- a/codes/java/chapter_computational_complexity/space_time_tradeoff/leetcode_two_sum.java +++ b/codes/java/chapter_computational_complexity/leetcode_two_sum.java @@ -1,4 +1,4 @@ -package chapter_computational_complexity.space_time_tradeoff; +package chapter_computational_complexity; import java.util.*; @@ -36,10 +36,11 @@ public class leetcode_two_sum { int target = 9; // ====== Driver Code ====== + // 方法一 solution_brute_force slt1 = new solution_brute_force(); int[] res = slt1.twoSum(nums, target); System.out.println(Arrays.toString(res)); - + // 方法二 solution_hash_map slt2 = new solution_hash_map(); res = slt2.twoSum(nums, target); System.out.println(Arrays.toString(res)); diff --git a/codes/java/chapter_computational_complexity/space_complexity/space_complexity_types.java b/codes/java/chapter_computational_complexity/space_complexity_types.java similarity index 97% rename from codes/java/chapter_computational_complexity/space_complexity/space_complexity_types.java rename to codes/java/chapter_computational_complexity/space_complexity_types.java index fdbf3b7..7ad3236 100644 --- a/codes/java/chapter_computational_complexity/space_complexity/space_complexity_types.java +++ b/codes/java/chapter_computational_complexity/space_complexity_types.java @@ -1,4 +1,4 @@ -package chapter_computational_complexity.space_complexity; +package chapter_computational_complexity; import include.*; import java.util.*; diff --git a/codes/java/chapter_computational_complexity/time_complexity/time_complexity_types.java b/codes/java/chapter_computational_complexity/time_complexity_types.java similarity index 98% rename from codes/java/chapter_computational_complexity/time_complexity/time_complexity_types.java rename to codes/java/chapter_computational_complexity/time_complexity_types.java index f395694..4b85544 100644 --- a/codes/java/chapter_computational_complexity/time_complexity/time_complexity_types.java +++ b/codes/java/chapter_computational_complexity/time_complexity_types.java @@ -1,4 +1,4 @@ -package chapter_computational_complexity.time_complexity; +package chapter_computational_complexity; public class time_complexity_types { /* 常数阶 */ diff --git a/codes/java/chapter_computational_complexity/time_complexity/worst_best_time_complexity.java b/codes/java/chapter_computational_complexity/worst_best_time_complexity.java similarity index 95% rename from codes/java/chapter_computational_complexity/time_complexity/worst_best_time_complexity.java rename to codes/java/chapter_computational_complexity/worst_best_time_complexity.java index d9def6d..8c0009d 100644 --- a/codes/java/chapter_computational_complexity/time_complexity/worst_best_time_complexity.java +++ b/codes/java/chapter_computational_complexity/worst_best_time_complexity.java @@ -1,4 +1,4 @@ -package chapter_computational_complexity.time_complexity; +package chapter_computational_complexity; import java.util.*; diff --git a/codes/java/chapter_stack_and_queue/array_queue.java b/codes/java/chapter_stack_and_queue/array_queue.java index 8f455f4..1f25c99 100644 --- a/codes/java/chapter_stack_and_queue/array_queue.java +++ b/codes/java/chapter_stack_and_queue/array_queue.java @@ -45,7 +45,7 @@ class ArrayQueue { /* 出队 */ public int poll() { - // 删除头节点 + // 删除头结点 if (isEmpty()) throw new EmptyStackException(); int num = nums[front]; @@ -56,7 +56,7 @@ class ArrayQueue { /* 访问队首元素 */ public int peek() { - // 删除头节点 + // 删除头结点 if (isEmpty()) throw new EmptyStackException(); return nums[front]; diff --git a/codes/java/chapter_stack_and_queue/linkedlist_queue.java b/codes/java/chapter_stack_and_queue/linkedlist_queue.java index 4bbb10f..22224c8 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_queue.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_queue.java @@ -29,7 +29,7 @@ class LinkedListQueue { /* 出队 */ public int poll() { - // 删除头节点 + // 删除头结点 return list.removeFirst(); } diff --git a/codes/java/chapter_tree/binary_tree.java b/codes/java/chapter_tree/binary_tree.java new file mode 100644 index 0000000..76d6059 --- /dev/null +++ b/codes/java/chapter_tree/binary_tree.java @@ -0,0 +1,34 @@ +package chapter_tree; + +import include.*; + +public class binary_tree { + public static void main(String[] args) { + /* 初始化二叉树 */ + // 初始化结点 + TreeNode n1 = new TreeNode(1); + TreeNode n2 = new TreeNode(2); + TreeNode n3 = new TreeNode(3); + TreeNode n4 = new TreeNode(4); + TreeNode n5 = new TreeNode(5); + // 构建引用指向(即指针) + n1.left = n2; + n1.right = n3; + n2.left = n4; + n2.right = n5; + System.out.println("\n初始化二叉树\n"); + PrintUtil.printTree(n1); + + /* 插入与删除结点 */ + TreeNode P = new TreeNode(0); + // 在 n1 -> n2 中间插入结点 P + n1.left = P; + P.left = n2; + System.out.println("\n插入结点 P 后\n"); + PrintUtil.printTree(n1); + // 删除结点 P + n1.left = n2; + System.out.println("\n删除结点 P 后\n"); + PrintUtil.printTree(n1); + } +} diff --git a/codes/java/chapter_tree/binary_tree_bfs.java b/codes/java/chapter_tree/binary_tree_bfs.java new file mode 100644 index 0000000..f196937 --- /dev/null +++ b/codes/java/chapter_tree/binary_tree_bfs.java @@ -0,0 +1,36 @@ +package chapter_tree; + +import include.*; +import java.util.*; + +public class binary_tree_bfs { + /* 层序遍历 */ + static List hierOrder(TreeNode root) { + // 初始化队列,加入根结点 + Queue queue = new LinkedList<>() {{ add(root); }}; + // 初始化一个列表,用于保存遍历序列 + List list = new ArrayList<>(); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); // 队列出队 + list.add(node.val); // 保存结点 + if (node.left != null) + queue.offer(node.left); // 左子结点入队 + if (node.right != null) + queue.offer(node.right); // 右子结点入队 + } + return list; + } + + public static void main(String[] args) { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + TreeNode root = TreeNode.arrToTree(new Integer[] { + 1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null }); + System.out.println("\n初始化二叉树\n"); + PrintUtil.printTree(root); + + /* 层序遍历 */ + List list = hierOrder(root); + System.out.println("\n层序遍历的结点打印序列 = " + list); + } +} diff --git a/codes/java/chapter_tree/binary_tree_dfs.java b/codes/java/chapter_tree/binary_tree_dfs.java new file mode 100644 index 0000000..15e1221 --- /dev/null +++ b/codes/java/chapter_tree/binary_tree_dfs.java @@ -0,0 +1,60 @@ +package chapter_tree; + +import include.*; +import java.util.*; + +public class binary_tree_dfs { + // 初始化列表,用于存储遍历序列 + static ArrayList list = new ArrayList<>(); + + /* 前序遍历 */ + static void preOrder(TreeNode root) { + if (root == null) return; + // 访问优先级:根结点 -> 左子树 -> 右子树 + list.add(root.val); + preOrder(root.left); + preOrder(root.right); + } + + /* 中序遍历 */ + static void inOrder(TreeNode root) { + if (root == null) return; + // 访问优先级:左子树 -> 根结点 -> 右子树 + inOrder(root.left); + list.add(root.val); + inOrder(root.right); + } + + /* 后序遍历 */ + static void postOrder(TreeNode root) { + if (root == null) return; + // 访问优先级:左子树 -> 右子树 -> 根结点 + postOrder(root.left); + postOrder(root.right); + list.add(root.val); + } + + public static void main(String[] args) { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + TreeNode root = TreeNode.arrToTree(new Integer[] { + 1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null}); + System.out.println("\n初始化二叉树\n"); + PrintUtil.printTree(root); + + /* 前序遍历 */ + list.clear(); + preOrder(root); + System.out.println("\n前序遍历的结点打印序列 = " + list); + + /* 中序遍历 */ + list.clear(); + inOrder(root); + System.out.println("\n中序遍历的结点打印序列 = " + list); + + /* 后序遍历 */ + list.clear(); + postOrder(root); + System.out.println("\n后序遍历的结点打印序列 = " + list); + } +} diff --git a/docs/index.md b/docs/index.md index 22f426b..9edc7fa 100644 --- a/docs/index.md +++ b/docs/index.md @@ -22,3 +22,4 @@ TODO | 更新:各章节 Java 代码 | 2022-11-06 | | 更新:列表 Java 代码、配图 | 2022-11-07 | | 新增:栈与队列 | 2022-11-09 | +| 新增:树 | 2022-11-12 | diff --git a/mkdocs.yml b/mkdocs.yml index adeaf52..468f7a2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -20,11 +20,11 @@ theme: # - content.tabs.link - content.tooltips # - header.autohide - - navigation.expand + # - navigation.expand - navigation.indexes # - navigation.instant # - navigation.prune - - navigation.sections + # - navigation.sections # - navigation.tabs # - navigation.tabs.sticky # - navigation.top @@ -139,6 +139,14 @@ nav: - chapter_stack_and_queue/index.md - 栈: chapter_stack_and_queue/stack.md - 队列: chapter_stack_and_queue/queue.md + - 双向队列: chapter_stack_and_queue/deque.md - 小结: chapter_stack_and_queue/summary.md + - 树: + - chapter_tree/index.md + - 二叉树: chapter_tree/binary_tree.md + - 满二叉树: chapter_tree/perfect_complete_full_binary_tree.md + - 二叉搜索树: chapter_tree/binary_search_tree.md + - 平衡二叉树: chapter_tree/balanced_binary_tree.md + - 小结: chapter_tree/summary.md - License: - chapter_license/index.md \ No newline at end of file