mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:36:29 +08:00
Add a chapter of binary tree.
This commit is contained in:
parent
ac8d0bcfeb
commit
7fdb4d1443
11 changed files with 150 additions and 10 deletions
|
@ -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));
|
|
@ -1,4 +1,4 @@
|
|||
package chapter_computational_complexity.space_complexity;
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
|
@ -1,4 +1,4 @@
|
|||
package chapter_computational_complexity.time_complexity;
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity_types {
|
||||
/* 常数阶 */
|
|
@ -1,4 +1,4 @@
|
|||
package chapter_computational_complexity.time_complexity;
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -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];
|
||||
|
|
|
@ -29,7 +29,7 @@ class LinkedListQueue {
|
|||
|
||||
/* 出队 */
|
||||
public int poll() {
|
||||
// 删除头节点
|
||||
// 删除头结点
|
||||
return list.removeFirst();
|
||||
}
|
||||
|
||||
|
|
34
codes/java/chapter_tree/binary_tree.java
Normal file
34
codes/java/chapter_tree/binary_tree.java
Normal 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);
|
||||
}
|
||||
}
|
36
codes/java/chapter_tree/binary_tree_bfs.java
Normal file
36
codes/java/chapter_tree/binary_tree_bfs.java
Normal 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);
|
||||
}
|
||||
}
|
60
codes/java/chapter_tree/binary_tree_dfs.java
Normal file
60
codes/java/chapter_tree/binary_tree_dfs.java
Normal 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);
|
||||
}
|
||||
}
|
|
@ -22,3 +22,4 @@ TODO
|
|||
| 更新:各章节 Java 代码 | 2022-11-06 |
|
||||
| 更新:列表 Java 代码、配图 | 2022-11-07 |
|
||||
| 新增:栈与队列 | 2022-11-09 |
|
||||
| 新增:树 | 2022-11-12 |
|
||||
|
|
12
mkdocs.yml
12
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
|
Loading…
Reference in a new issue