Add a chapter of binary tree.

This commit is contained in:
krahets 2022-11-12 22:48:46 +08:00
parent ac8d0bcfeb
commit 7fdb4d1443
11 changed files with 150 additions and 10 deletions

View File

@ -1,4 +1,4 @@
package chapter_computational_complexity.space_time_tradeoff; package chapter_computational_complexity;
import java.util.*; import java.util.*;
@ -36,10 +36,11 @@ public class leetcode_two_sum {
int target = 9; int target = 9;
// ====== Driver Code ====== // ====== Driver Code ======
// 方法一
solution_brute_force slt1 = new solution_brute_force(); solution_brute_force slt1 = new solution_brute_force();
int[] res = slt1.twoSum(nums, target); int[] res = slt1.twoSum(nums, target);
System.out.println(Arrays.toString(res)); System.out.println(Arrays.toString(res));
// 方法二
solution_hash_map slt2 = new solution_hash_map(); solution_hash_map slt2 = new solution_hash_map();
res = slt2.twoSum(nums, target); res = slt2.twoSum(nums, target);
System.out.println(Arrays.toString(res)); System.out.println(Arrays.toString(res));

View File

@ -1,4 +1,4 @@
package chapter_computational_complexity.space_complexity; package chapter_computational_complexity;
import include.*; import include.*;
import java.util.*; import java.util.*;

View File

@ -1,4 +1,4 @@
package chapter_computational_complexity.time_complexity; package chapter_computational_complexity;
public class time_complexity_types { public class time_complexity_types {
/* 常数阶 */ /* 常数阶 */

View File

@ -1,4 +1,4 @@
package chapter_computational_complexity.time_complexity; package chapter_computational_complexity;
import java.util.*; import java.util.*;

View File

@ -45,7 +45,7 @@ class ArrayQueue {
/* 出队 */ /* 出队 */
public int poll() { public int poll() {
// 删除头 // 删除头
if (isEmpty()) if (isEmpty())
throw new EmptyStackException(); throw new EmptyStackException();
int num = nums[front]; int num = nums[front];
@ -56,7 +56,7 @@ class ArrayQueue {
/* 访问队首元素 */ /* 访问队首元素 */
public int peek() { public int peek() {
// 删除头 // 删除头
if (isEmpty()) if (isEmpty())
throw new EmptyStackException(); throw new EmptyStackException();
return nums[front]; return nums[front];

View File

@ -29,7 +29,7 @@ class LinkedListQueue {
/* 出队 */ /* 出队 */
public int poll() { public int poll() {
// 删除头 // 删除头
return list.removeFirst(); return list.removeFirst();
} }

View File

@ -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);
}
}

View File

@ -0,0 +1,36 @@
package chapter_tree;
import include.*;
import java.util.*;
public class binary_tree_bfs {
/* 层序遍历 */
static List<Integer> hierOrder(TreeNode root) {
// 初始化队列加入根结点
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
// 初始化一个列表用于保存遍历序列
List<Integer> 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<Integer> list = hierOrder(root);
System.out.println("\n层序遍历的结点打印序列 = " + list);
}
}

View File

@ -0,0 +1,60 @@
package chapter_tree;
import include.*;
import java.util.*;
public class binary_tree_dfs {
// 初始化列表用于存储遍历序列
static ArrayList<Integer> 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);
}
}

View File

@ -22,3 +22,4 @@ TODO
| 更新:各章节 Java 代码 | 2022-11-06 | | 更新:各章节 Java 代码 | 2022-11-06 |
| 更新:列表 Java 代码、配图 | 2022-11-07 | | 更新:列表 Java 代码、配图 | 2022-11-07 |
| 新增:栈与队列 | 2022-11-09 | | 新增:栈与队列 | 2022-11-09 |
| 新增:树 | 2022-11-12 |

View File

@ -20,11 +20,11 @@ theme:
# - content.tabs.link # - content.tabs.link
- content.tooltips - content.tooltips
# - header.autohide # - header.autohide
- navigation.expand # - navigation.expand
- navigation.indexes - navigation.indexes
# - navigation.instant # - navigation.instant
# - navigation.prune # - navigation.prune
- navigation.sections # - navigation.sections
# - navigation.tabs # - navigation.tabs
# - navigation.tabs.sticky # - navigation.tabs.sticky
# - navigation.top # - navigation.top
@ -139,6 +139,14 @@ nav:
- chapter_stack_and_queue/index.md - chapter_stack_and_queue/index.md
- : chapter_stack_and_queue/stack.md - : chapter_stack_and_queue/stack.md
- 队列: chapter_stack_and_queue/queue.md - 队列: chapter_stack_and_queue/queue.md
- 双向队列: chapter_stack_and_queue/deque.md
- 小结: chapter_stack_and_queue/summary.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: - License:
- chapter_license/index.md - chapter_license/index.md