refactor: Replace 结点 with 节点 (#452)

* Replace 结点 with 节点
Update the footnotes in the figures

* Update mindmap

* Reduce the size of the mindmap.png
This commit is contained in:
Yudong Jin
2023-04-09 04:32:17 +08:00
committed by GitHub
parent 3f4e32b2b0
commit 1c8b7ef559
395 changed files with 2056 additions and 2056 deletions

View File

@@ -9,7 +9,7 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class linked_list
{
/* 在链表的点 n0 之后插入点 P */
/* 在链表的点 n0 之后插入点 P */
public static void insert(ListNode n0, ListNode P)
{
ListNode? n1 = n0.next;
@@ -17,7 +17,7 @@ public class linked_list
n0.next = P;
}
/* 删除链表的点 n0 之后的首个点 */
/* 删除链表的点 n0 之后的首个点 */
public static void remove(ListNode n0)
{
if (n0.next == null)
@@ -28,7 +28,7 @@ public class linked_list
n0.next = n1;
}
/* 访问链表中索引为 index 的点 */
/* 访问链表中索引为 index 的点 */
public static ListNode? access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
@@ -40,7 +40,7 @@ public class linked_list
return head;
}
/* 在链表中查找值为 target 的首个点 */
/* 在链表中查找值为 target 的首个点 */
public static int find(ListNode head, int target)
{
int index = 0;
@@ -59,7 +59,7 @@ public class linked_list
public void Test()
{
// 初始化链表
// 初始化各个
// 初始化各个
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
@@ -72,20 +72,20 @@ public class linked_list
n3.next = n4;
Console.WriteLine($"初始化的链表为{n0}");
// 插入
// 插入
insert(n0, new ListNode(0));
Console.WriteLine($"插入点后的链表为{n0}");
Console.WriteLine($"插入点后的链表为{n0}");
// 删除
// 删除
remove(n0);
Console.WriteLine($"删除点后的链表为{n0}");
Console.WriteLine($"删除点后的链表为{n0}");
// 访问
// 访问
ListNode? node = access(n0, 3);
Console.WriteLine($"链表中索引 3 处的点的值 = {node?.val}");
Console.WriteLine($"链表中索引 3 处的点的值 = {node?.val}");
// 查找
// 查找
int index = find(n0, 2);
Console.WriteLine($"链表中值为 2 的点的索引 = {index}");
Console.WriteLine($"链表中值为 2 的点的索引 = {index}");
}
}

View File

@@ -26,7 +26,7 @@ class MaxHeap
{
// 将列表元素原封不动添加进堆
maxHeap = new List<int>(nums);
// 堆化除叶点以外的其他所有
// 堆化除叶点以外的其他所有
var size = parent(this.size() - 1);
for (int i = size; i >= 0; i--)
{
@@ -34,19 +34,19 @@ class MaxHeap
}
}
/* 获取左子点索引 */
/* 获取左子点索引 */
int left(int i)
{
return 2 * i + 1;
}
/* 获取右子点索引 */
/* 获取右子点索引 */
int right(int i)
{
return 2 * i + 2;
}
/* 获取父点索引 */
/* 获取父点索引 */
int parent(int i)
{
return (i - 1) / 2; // 向下整除
@@ -61,7 +61,7 @@ class MaxHeap
/* 元素入堆 */
public void push(int val)
{
// 添加
// 添加
maxHeap.Add(val);
// 从底至顶堆化
siftUp(size() - 1);
@@ -79,17 +79,17 @@ class MaxHeap
return size() == 0;
}
/* 从点 i 开始,从底至顶堆化 */
/* 从点 i 开始,从底至顶堆化 */
void siftUp(int i)
{
while (true)
{
// 获取点 i 的父
// 获取点 i 的父
int p = parent(i);
// 若“越过根点”或“点无需修复”,则结束堆化
// 若“越过根点”或“点无需修复”,则结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两
// 交换两
swap(i, p);
// 循环向上堆化
i = p;
@@ -102,9 +102,9 @@ class MaxHeap
// 判空处理
if (isEmpty())
throw new IndexOutOfRangeException();
// 交换根点与最右叶点(即交换首元素与尾元素)
// 交换根点与最右叶点(即交换首元素与尾元素)
swap(0, size() - 1);
// 删除
// 删除
int val = maxHeap.Last();
maxHeap.RemoveAt(size() - 1);
// 从顶至底堆化
@@ -113,20 +113,20 @@ class MaxHeap
return val;
}
/* 从点 i 开始,从顶至底堆化 */
/* 从点 i 开始,从顶至底堆化 */
void siftDown(int i)
{
while (true)
{
// 判断点 i, l, r 中值最大的点,记为 ma
// 判断点 i, l, r 中值最大的点,记为 ma
int l = left(i), r = right(i), ma = i;
if (l < size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若“点 i 最大”或“越过叶点”,则结束堆化
// 若“点 i 最大”或“越过叶点”,则结束堆化
if (ma == i) break;
// 交换两
// 交换两
swap(i, ma);
// 循环向下堆化
i = ma;

View File

@@ -23,7 +23,7 @@ public class hashing_search
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{
// 哈希表的 key: 目标点值value: 点对象
// 哈希表的 key: 目标点值value: 点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
@@ -50,10 +50,10 @@ public class hashing_search
Dictionary<int, ListNode> map1 = new();
while (head != null)
{
map1[head.val] = head; // key: 点值value:
map1[head.val] = head; // key: 点值value:
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标点值 3 的对应点对象为 " + node);
Console.WriteLine("目标点值 3 的对应点对象为 " + node);
}
}

View File

@@ -31,12 +31,12 @@ public class linear_search
// 遍历链表
while (head != null)
{
// 找到目标点,返回之
// 找到目标点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return null;
}
@@ -53,6 +53,6 @@ public class linear_search
/* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target);
Console.WriteLine("目标点值 3 的对应点对象为 " + node);
Console.WriteLine("目标点值 3 的对应点对象为 " + node);
}
}

View File

@@ -8,12 +8,12 @@ using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue
{
/* 双向链表点 */
/* 双向链表点 */
public class ListNode
{
public int val; // 点值
public ListNode? next; // 后继点引用(指针)
public ListNode? prev; // 前驱点引用(指针)
public int val; // 点值
public ListNode? next; // 后继点引用(指针)
public ListNode? prev; // 前驱点引用(指针)
public ListNode(int val)
{
@@ -26,7 +26,7 @@ namespace hello_algo.chapter_stack_and_queue
/* 基于双向链表实现的双向队列 */
public class LinkedListDeque
{
private ListNode? front, rear; // 头点 front, 尾点 rear
private ListNode? front, rear; // 头点 front, 尾点 rear
private int queSize = 0; // 双向队列的长度
public LinkedListDeque()
@@ -63,7 +63,7 @@ namespace hello_algo.chapter_stack_and_queue
// 将 node 添加至链表头部
front.prev = node;
node.next = front;
front = node; // 更新头
front = node; // 更新头
}
// 队尾入队操作
else
@@ -71,7 +71,7 @@ namespace hello_algo.chapter_stack_and_queue
// 将 node 添加至链表尾部
rear.next = node;
node.prev = rear;
rear = node; // 更新尾
rear = node; // 更新尾
}
queSize++; // 更新队列长度
@@ -102,8 +102,8 @@ namespace hello_algo.chapter_stack_and_queue
// 队首出队操作
if (isFront)
{
val = front.val; // 暂存头点值
// 删除头
val = front.val; // 暂存头点值
// 删除头
ListNode fNext = front.next;
if (fNext != null)
{
@@ -111,13 +111,13 @@ namespace hello_algo.chapter_stack_and_queue
front.next = null;
}
front = fNext; // 更新头
front = fNext; // 更新头
}
// 队尾出队操作
else
{
val = rear.val; // 暂存尾点值
// 删除尾
val = rear.val; // 暂存尾点值
// 删除尾
ListNode rPrev = rear.prev;
if (rPrev != null)
{
@@ -125,7 +125,7 @@ namespace hello_algo.chapter_stack_and_queue
rear.prev = null;
}
rear = rPrev; // 更新尾
rear = rPrev; // 更新尾
}
queSize--; // 更新队列长度

View File

@@ -12,7 +12,7 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的队列 */
class LinkedListQueue
{
private ListNode? front, rear; // 头点 front ,尾点 rear
private ListNode? front, rear; // 头点 front ,尾点 rear
private int queSize = 0;
public LinkedListQueue()
@@ -36,14 +36,14 @@ class LinkedListQueue
/* 入队 */
public void push(int num)
{
// 尾点后添加 num
// 尾点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾点都指向该
// 如果队列为空,则令头、尾点都指向该
if (front == null)
{
front = node;
rear = node;
// 如果队列不为空,则将该点添加到尾点后
// 如果队列不为空,则将该点添加到尾点后
}
else if (rear != null)
{
@@ -57,7 +57,7 @@ class LinkedListQueue
public int pop()
{
int num = peek();
// 删除头
// 删除头
front = front?.next;
queSize--;
return num;

View File

@@ -12,7 +12,7 @@ namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的栈 */
class LinkedListStack
{
private ListNode? stackPeek; // 将头点作为栈顶
private ListNode? stackPeek; // 将头点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()

View File

@@ -12,28 +12,28 @@ namespace hello_algo.chapter_tree;
/* AVL 树 */
class AVLTree
{
public TreeNode? root; // 根
public TreeNode? root; // 根
/* 获取点高度 */
/* 获取点高度 */
public int height(TreeNode? node)
{
// 空点高度为 -1 ,叶点高度为 0
// 空点高度为 -1 ,叶点高度为 0
return node == null ? -1 : node.height;
}
/* 更新点高度 */
/* 更新点高度 */
private void updateHeight(TreeNode node)
{
// 点高度等于最高子树高度 + 1
// 点高度等于最高子树高度 + 1
node.height = Math.Max(height(node.left), height(node.right)) + 1;
}
/* 获取平衡因子 */
public int balanceFactor(TreeNode? node)
{
// 空点平衡因子为 0
// 空点平衡因子为 0
if (node == null) return 0;
// 点平衡因子 = 左子树高度 - 右子树高度
// 点平衡因子 = 左子树高度 - 右子树高度
return height(node.left) - height(node.right);
}
@@ -45,10 +45,10 @@ class AVLTree
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新点高度
// 更新点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
@@ -60,17 +60,17 @@ class AVLTree
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新点高度
// 更新点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node)
{
// 获取点 node 的平衡因子
// 获取点 node 的平衡因子
int balanceFactorInt = balanceFactor(node);
// 左偏树
if (balanceFactorInt > 1)
@@ -106,43 +106,43 @@ class AVLTree
return node;
}
/* 插入点 */
/* 插入点 */
public TreeNode? insert(int val)
{
root = insertHelper(root, val);
return root;
}
/* 递归插入点(辅助方法) */
/* 递归插入点(辅助方法) */
private TreeNode? insertHelper(TreeNode? node, int val)
{
if (node == null) return new TreeNode(val);
/* 1. 查找插入位置,并插入点 */
/* 1. 查找插入位置,并插入点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; // 重复点不插入,直接返回
updateHeight(node); // 更新点高度
return node; // 重复点不插入,直接返回
updateHeight(node); // 更新点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
/* 删除点 */
/* 删除点 */
public TreeNode? remove(int val)
{
root = removeHelper(root, val);
return root;
}
/* 递归删除点(辅助方法) */
/* 递归删除点(辅助方法) */
private TreeNode? removeHelper(TreeNode? node, int val)
{
if (node == null) return null;
/* 1. 查找点,并删除之 */
/* 1. 查找点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
@@ -152,33 +152,33 @@ class AVLTree
if (node.left == null || node.right == null)
{
TreeNode? child = node.left != null ? node.left : node.right;
// 子点数量 = 0 ,直接删除 node 并返回
// 子点数量 = 0 ,直接删除 node 并返回
if (child == null)
return null;
// 子点数量 = 1 ,直接删除 node
// 子点数量 = 1 ,直接删除 node
else
node = child;
}
else
{
// 子点数量 = 2 ,则将中序遍历的下个点删除,并用该点替换当前
// 子点数量 = 2 ,则将中序遍历的下个点删除,并用该点替换当前
TreeNode? temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp.val);
node.val = temp.val;
}
}
updateHeight(node); // 更新点高度
updateHeight(node); // 更新点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
private TreeNode? getInOrderNext(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子点,直到叶点时为最小点,跳出
// 循环访问左子点,直到叶点时为最小点,跳出
while (node.left != null)
{
node = node.left;
@@ -186,24 +186,24 @@ class AVLTree
return node;
}
/* 查找点 */
/* 查找点 */
public TreeNode? search(int val)
{
TreeNode? cur = root;
// 循环查找,越过叶点后跳出
// 循环查找,越过叶点后跳出
while (cur != null)
{
// 目标点在 cur 的右子树中
// 目标点在 cur 的右子树中
if (cur.val < val)
cur = cur.right;
// 目标点在 cur 的左子树中
// 目标点在 cur 的左子树中
else if (cur.val > val)
cur = cur.left;
// 找到目标点,跳出循环
// 找到目标点,跳出循环
else
break;
}
// 返回目标
// 返回目标
return cur;
}
}
@@ -213,14 +213,14 @@ public class avl_tree
static void testInsert(AVLTree tree, int val)
{
tree.insert(val);
Console.WriteLine("\n插入" + val + "AVL 树为");
Console.WriteLine("\n插入" + val + "AVL 树为");
PrintUtil.PrintTree(tree.root);
}
static void testRemove(AVLTree tree, int val)
{
tree.remove(val);
Console.WriteLine("\n删除" + val + "AVL 树为");
Console.WriteLine("\n删除" + val + "AVL 树为");
PrintUtil.PrintTree(tree.root);
}
@@ -230,8 +230,8 @@ public class avl_tree
/* 初始化空 AVL 树 */
AVLTree avlTree = new AVLTree();
/* 插入点 */
// 请关注插入点后AVL 树是如何保持平衡的
/* 插入点 */
// 请关注插入点后AVL 树是如何保持平衡的
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
@@ -243,17 +243,17 @@ public class avl_tree
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* 插入重复点 */
/* 插入重复点 */
testInsert(avlTree, 7);
/* 删除点 */
// 请关注删除点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的
testRemove(avlTree, 5); // 删除度为 1 的
testRemove(avlTree, 4); // 删除度为 2 的
/* 删除点 */
// 请关注删除点后AVL 树是如何保持平衡的
testRemove(avlTree, 8); // 删除度为 0 的
testRemove(avlTree, 5); // 删除度为 1 的
testRemove(avlTree, 4); // 删除度为 2 的
/* 查询点 */
/* 查询点 */
TreeNode? node = avlTree.search(7);
Console.WriteLine("\n查找到的点对象为 " + node + "点值 = " + node?.val);
Console.WriteLine("\n查找到的点对象为 " + node + "点值 = " + node?.val);
}
}

View File

@@ -19,7 +19,7 @@ class BinarySearchTree
root = buildTree(nums, 0, nums.Length - 1); // 构建二叉搜索树
}
/* 获取二叉树根点 */
/* 获取二叉树根点 */
public TreeNode? getRoot()
{
return root;
@@ -29,7 +29,7 @@ class BinarySearchTree
public TreeNode? buildTree(int[] nums, int i, int j)
{
if (i > j) return null;
// 将数组中间点作为根
// 将数组中间点作为根
int mid = (i + j) / 2;
TreeNode root = new TreeNode(nums[mid]);
// 递归建立左子树和右子树
@@ -38,34 +38,34 @@ class BinarySearchTree
return root;
}
/* 查找点 */
/* 查找点 */
public TreeNode? search(int num)
{
TreeNode? cur = root;
// 循环查找,越过叶点后跳出
// 循环查找,越过叶点后跳出
while (cur != null)
{
// 目标点在 cur 的右子树中
// 目标点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 目标点在 cur 的左子树中
// 目标点在 cur 的左子树中
else if (cur.val > num) cur = cur.left;
// 找到目标点,跳出循环
// 找到目标点,跳出循环
else break;
}
// 返回目标
// 返回目标
return cur;
}
/* 插入点 */
/* 插入点 */
public TreeNode? insert(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶点后跳出
// 循环查找,越过叶点后跳出
while (cur != null)
{
// 找到重复点,直接返回
// 找到重复点,直接返回
if (cur.val == num) return null;
pre = cur;
// 插入位置在 cur 的右子树中
@@ -74,7 +74,7 @@ class BinarySearchTree
else cur = cur.left;
}
// 插入点 val
// 插入点 val
TreeNode node = new TreeNode(num);
if (pre != null)
{
@@ -85,31 +85,31 @@ class BinarySearchTree
}
/* 删除点 */
/* 删除点 */
public TreeNode? remove(int num)
{
// 若树为空,直接提前返回
if (root == null) return null;
TreeNode? cur = root, pre = null;
// 循环查找,越过叶点后跳出
// 循环查找,越过叶点后跳出
while (cur != null)
{
// 找到待删除点,跳出循环
// 找到待删除点,跳出循环
if (cur.val == num) break;
pre = cur;
// 待删除点在 cur 的右子树中
// 待删除点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 待删除点在 cur 的左子树中
// 待删除点在 cur 的左子树中
else cur = cur.left;
}
// 若无待删除点,则直接返回
// 若无待删除点,则直接返回
if (cur == null || pre == null) return null;
// 子点数量 = 0 or 1
// 子点数量 = 0 or 1
if (cur.left == null || cur.right == null)
{
// 当子点数量 = 0 / 1 时, child = null / 该子
// 当子点数量 = 0 / 1 时, child = null / 该子
TreeNode? child = cur.left != null ? cur.left : cur.right;
// 删除点 cur
// 删除点 cur
if (pre.left == cur)
{
pre.left = child;
@@ -119,15 +119,15 @@ class BinarySearchTree
pre.right = child;
}
}
// 子点数量 = 2
// 子点数量 = 2
else
{
// 获取中序遍历中 cur 的下一个
// 获取中序遍历中 cur 的下一个
TreeNode? nex = getInOrderNext(cur.right);
if (nex != null)
{
int tmp = nex.val;
// 递归删除点 nex
// 递归删除点 nex
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
@@ -136,11 +136,11 @@ class BinarySearchTree
return cur;
}
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
private TreeNode? getInOrderNext(TreeNode? root)
{
if (root == null) return root;
// 循环访问左子点,直到叶点时为最小点,跳出
// 循环访问左子点,直到叶点时为最小点,跳出
while (root.left != null)
{
root = root.left;
@@ -160,24 +160,24 @@ public class binary_search_tree
Console.WriteLine("\n初始化的二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 查找点 */
/* 查找点 */
TreeNode? node = bst.search(7);
Console.WriteLine("\n查找到的点对象为 " + node + "点值 = " + node.val);
Console.WriteLine("\n查找到的点对象为 " + node + "点值 = " + node.val);
/* 插入点 */
/* 插入点 */
node = bst.insert(16);
Console.WriteLine("\n插入点 16 后,二叉树为\n");
Console.WriteLine("\n插入点 16 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
/* 删除点 */
/* 删除点 */
bst.remove(1);
Console.WriteLine("\n删除点 1 后,二叉树为\n");
Console.WriteLine("\n删除点 1 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(2);
Console.WriteLine("\n删除点 2 后,二叉树为\n");
Console.WriteLine("\n删除点 2 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
bst.remove(4);
Console.WriteLine("\n删除点 4 后,二叉树为\n");
Console.WriteLine("\n删除点 4 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot());
}
}

View File

@@ -15,7 +15,7 @@ public class binary_tree
public void Test()
{
/* 初始化二叉树 */
// 初始化
// 初始化
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
@@ -29,16 +29,16 @@ public class binary_tree
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(n1);
/* 插入与删除点 */
/* 插入与删除点 */
TreeNode P = new TreeNode(0);
// 在 n1 -> n2 中间插入点 P
// 在 n1 -> n2 中间插入点 P
n1.left = P;
P.left = n2;
Console.WriteLine("\n插入点 P 后\n");
Console.WriteLine("\n插入点 P 后\n");
PrintUtil.PrintTree(n1);
// 删除点 P
// 删除点 P
n1.left = n2;
Console.WriteLine("\n删除点 P 后\n");
Console.WriteLine("\n删除点 P 后\n");
PrintUtil.PrintTree(n1);
}
}

View File

@@ -15,7 +15,7 @@ public class binary_tree_bfs
/* 层序遍历 */
public List<int> levelOrder(TreeNode root)
{
// 初始化队列,加入根
// 初始化队列,加入根
Queue<TreeNode> queue = new();
queue.Enqueue(root);
// 初始化一个列表,用于保存遍历序列
@@ -23,11 +23,11 @@ public class binary_tree_bfs
while (queue.Count != 0)
{
TreeNode node = queue.Dequeue(); // 队列出队
list.Add(node.val); // 保存点值
list.Add(node.val); // 保存点值
if (node.left != null)
queue.Enqueue(node.left); // 左子点入队
queue.Enqueue(node.left); // 左子点入队
if (node.right != null)
queue.Enqueue(node.right); // 右子点入队
queue.Enqueue(node.right); // 右子点入队
}
return list;
}
@@ -42,6 +42,6 @@ public class binary_tree_bfs
PrintUtil.PrintTree(root);
List<int> list = levelOrder(root);
Console.WriteLine("\n层序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
Console.WriteLine("\n层序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
}
}

View File

@@ -17,7 +17,7 @@ public class binary_tree_dfs
void preOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:根点 -> 左子树 -> 右子树
// 访问优先级:根点 -> 左子树 -> 右子树
list.Add(root.val);
preOrder(root.left);
preOrder(root.right);
@@ -27,7 +27,7 @@ public class binary_tree_dfs
void inOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 根点 -> 右子树
// 访问优先级:左子树 -> 根点 -> 右子树
inOrder(root.left);
list.Add(root.val);
inOrder(root.right);
@@ -37,7 +37,7 @@ public class binary_tree_dfs
void postOrder(TreeNode? root)
{
if (root == null) return;
// 访问优先级:左子树 -> 右子树 -> 根
// 访问优先级:左子树 -> 右子树 -> 根
postOrder(root.left);
postOrder(root.right);
list.Add(root.val);
@@ -54,14 +54,14 @@ public class binary_tree_dfs
list.Clear();
preOrder(root);
Console.WriteLine("\n前序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
Console.WriteLine("\n前序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
list.Clear();
inOrder(root);
Console.WriteLine("\n中序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
Console.WriteLine("\n中序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
list.Clear();
postOrder(root);
Console.WriteLine("\n后序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
Console.WriteLine("\n后序遍历的点打印序列 = " + string.Join(",", list.ToArray()));
}
}

View File

@@ -8,10 +8,10 @@ namespace hello_algo.include;
public class TreeNode
{
public int val; // 点值
public int height; // 点高度
public TreeNode? left; // 左子点引用
public TreeNode? right; // 右子点引用
public int val; // 点值
public int height; // 点高度
public TreeNode? left; // 左子点引用
public TreeNode? right; // 右子点引用
public TreeNode(int x)
{