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

@@ -6,14 +6,14 @@
#include "../include/include.hpp"
/* 在链表的点 n0 之后插入点 P */
/* 在链表的点 n0 之后插入点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode* n1 = n0->next;
P->next = n1;
n0->next = P;
}
/* 删除链表的点 n0 之后的首个点 */
/* 删除链表的点 n0 之后的首个点 */
void remove(ListNode* n0) {
if (n0->next == nullptr)
return;
@@ -25,7 +25,7 @@ void remove(ListNode* n0) {
delete P;
}
/* 访问链表中索引为 index 的点 */
/* 访问链表中索引为 index 的点 */
ListNode* access(ListNode* head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
@@ -35,7 +35,7 @@ ListNode* access(ListNode* head, int index) {
return head;
}
/* 在链表中查找值为 target 的首个点 */
/* 在链表中查找值为 target 的首个点 */
int find(ListNode* head, int target) {
int index = 0;
while (head != nullptr) {
@@ -51,7 +51,7 @@ int find(ListNode* head, int target) {
/* Driver Code */
int main() {
/* 初始化链表 */
// 初始化各个
// 初始化各个
ListNode* n0 = new ListNode(1);
ListNode* n1 = new ListNode(3);
ListNode* n2 = new ListNode(2);
@@ -65,23 +65,23 @@ int main() {
cout << "初始化的链表为" << endl;
PrintUtil::printLinkedList(n0);
/* 插入点 */
/* 插入点 */
insert(n0, new ListNode(0));
cout << "插入点后的链表为" << endl;
cout << "插入点后的链表为" << endl;
PrintUtil::printLinkedList(n0);
/* 删除点 */
/* 删除点 */
remove(n0);
cout << "删除点后的链表为" << endl;
cout << "删除点后的链表为" << endl;
PrintUtil::printLinkedList(n0);
/* 访问点 */
/* 访问点 */
ListNode* node = access(n0, 3);
cout << "链表中索引 3 处的点的值 = " << node->val << endl;
cout << "链表中索引 3 处的点的值 = " << node->val << endl;
/* 查找点 */
/* 查找点 */
int index = find(n0, 2);
cout << "链表中值为 2 的点的索引 = " << index << endl;
cout << "链表中值为 2 的点的索引 = " << index << endl;
// 释放内存
freeMemoryLinkedList(n0);

View File

@@ -12,7 +12,7 @@ public:
// 邻接表key: 顶点value该顶点的所有邻接顶点
unordered_map<Vertex*, vector<Vertex*>> adjList;
/* 在 vector 中删除指定点 */
/* 在 vector 中删除指定点 */
void remove(vector<Vertex*> &vec, Vertex *vet) {
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == vet) {

View File

@@ -12,47 +12,47 @@ private:
// 使用动态数组,这样无需考虑扩容问题
vector<int> 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; // 向下取整
}
/* 从点 i 开始,从底至顶堆化 */
/* 从点 i 开始,从底至顶堆化 */
void siftUp(int i) {
while (true) {
// 获取点 i 的父
// 获取点 i 的父
int p = parent(i);
// 当“越过根点”或“点无需修复”时,结束堆化
// 当“越过根点”或“点无需修复”时,结束堆化
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 交换两
// 交换两
swap(maxHeap[i], maxHeap[p]);
// 循环向上堆化
i = p;
}
}
/* 从点 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;
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if (l < size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
ma = r;
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if (ma == i)
break;
swap(maxHeap[i], maxHeap[ma]);
@@ -66,7 +66,7 @@ public:
MaxHeap(vector<int> nums) {
// 将列表元素原封不动添加进堆
maxHeap = nums;
// 堆化除叶点以外的其他所有
// 堆化除叶点以外的其他所有
for (int i = parent(size() - 1); i >= 0; i--) {
siftDown(i);
}
@@ -89,7 +89,7 @@ public:
/* 元素入堆 */
void push(int val) {
// 添加
// 添加
maxHeap.push_back(val);
// 从底至顶堆化
siftUp(size() - 1);
@@ -101,9 +101,9 @@ public:
if (empty()) {
throw out_of_range("堆为空");
}
// 交换根点与最右叶点(即交换首元素与尾元素)
// 交换根点与最右叶点(即交换首元素与尾元素)
swap(maxHeap[0], maxHeap[size() - 1]);
// 删除
// 删除
maxHeap.pop_back();
// 从顶至底堆化
siftDown(0);

View File

@@ -17,7 +17,7 @@ int hashingSearchArray(unordered_map<int, int> map, int target) {
/* 哈希查找(链表) */
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
// 哈希表的 key: 目标点值value: 点对象
// 哈希表的 key: 目标点值value: 点对象
// 若哈希表中无此 key ,返回 nullptr
if (map.find(target) == map.end())
return nullptr;
@@ -44,11 +44,11 @@ int main() {
// 初始化哈希表
unordered_map<int, ListNode*> map1;
while (head != nullptr) {
map1[head->val] = head; // key: 点值value:
map1[head->val] = head; // key: 点值value:
head = head->next;
}
ListNode* node = hashingSearchLinkedList(map1, target);
cout << "目标点值 3 的对应点对象为 " << node << endl;
cout << "目标点值 3 的对应点对象为 " << node << endl;
return 0;
}

View File

@@ -22,12 +22,12 @@ int linearSearchArray(vector<int>& nums, int target) {
ListNode* linearSearchLinkedList(ListNode* head, int target) {
// 遍历链表
while (head != nullptr) {
// 找到目标点,返回之
// 找到目标点,返回之
if (head->val == target)
return head;
head = head->next;
}
// 未找到目标点,返回 nullptr
// 未找到目标点,返回 nullptr
return nullptr;
}
@@ -44,7 +44,7 @@ int main() {
/* 在链表中执行线性查找 */
ListNode* head = vecToLinkedList(nums);
ListNode* node = linearSearchLinkedList(head, target);
cout << "目标点值 3 的对应点对象为 " << node << endl;
cout << "目标点值 3 的对应点对象为 " << node << endl;
return 0;
}

View File

@@ -7,18 +7,18 @@
#include "../include/include.hpp"
/* 双向链表点 */
/* 双向链表点 */
struct DoublyListNode {
int val; // 点值
DoublyListNode *next; // 后继点指针
DoublyListNode *prev; // 前驱点指针
int val; // 点值
DoublyListNode *next; // 后继点指针
DoublyListNode *prev; // 前驱点指针
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {}
};
/* 基于双向链表实现的双向队列 */
class LinkedListDeque {
private:
DoublyListNode *front, *rear; // 头点 front ,尾点 rear
DoublyListNode *front, *rear; // 头点 front ,尾点 rear
int queSize = 0; // 双向队列的长度
public:
@@ -27,7 +27,7 @@ public:
/* 析构方法 */
~LinkedListDeque() {
// 遍历链表删除点,释放内存
// 遍历链表删除点,释放内存
DoublyListNode *pre, *cur = front;
while (cur != nullptr) {
pre = cur;
@@ -57,13 +57,13 @@ public:
// 将 node 添加至链表头部
front->prev = node;
node->next = front;
front = node; // 更新头
front = node; // 更新头
// 队尾入队操作
} else {
// 将 node 添加至链表尾部
rear->next = node;
node->prev = rear;
rear = node; // 更新尾
rear = node; // 更新尾
}
queSize++; // 更新队列长度
}
@@ -86,26 +86,26 @@ public:
int val;
// 队首出队操作
if (isFront) {
val = front->val; // 暂存头点值
// 删除头
val = front->val; // 暂存头点值
// 删除头
DoublyListNode *fNext = front->next;
if (fNext != nullptr) {
fNext->prev = nullptr;
front->next = nullptr;
delete front;
}
front = fNext; // 更新头
front = fNext; // 更新头
// 队尾出队操作
} else {
val = rear->val; // 暂存尾点值
// 删除尾
val = rear->val; // 暂存尾点值
// 删除尾
DoublyListNode *rPrev = rear->prev;
if (rPrev != nullptr) {
rPrev->next = nullptr;
rear->prev = nullptr;
delete rear;
}
rear = rPrev; // 更新尾
rear = rPrev; // 更新尾
}
queSize--; // 更新队列长度
return val;

View File

@@ -9,7 +9,7 @@
/* 基于链表实现的队列 */
class LinkedListQueue {
private:
ListNode *front, *rear; // 头点 front ,尾点 rear
ListNode *front, *rear; // 头点 front ,尾点 rear
int queSize;
public:
@@ -20,7 +20,7 @@ public:
}
~LinkedListQueue() {
// 遍历链表删除点,释放内存
// 遍历链表删除点,释放内存
freeMemoryLinkedList(front);
}
@@ -36,14 +36,14 @@ public:
/* 入队 */
void push(int num) {
// 尾点后添加 num
// 尾点后添加 num
ListNode* node = new ListNode(num);
// 如果队列为空,则令头、尾点都指向该
// 如果队列为空,则令头、尾点都指向该
if (front == nullptr) {
front = node;
rear = node;
}
// 如果队列不为空,则将该点添加到尾点后
// 如果队列不为空,则将该点添加到尾点后
else {
rear->next = node;
rear = node;
@@ -54,7 +54,7 @@ public:
/* 出队 */
void pop() {
int num = peek();
// 删除头
// 删除头
ListNode *tmp = front;
front = front->next;
// 释放内存

View File

@@ -9,7 +9,7 @@
/* 基于链表实现的栈 */
class LinkedListStack {
private:
ListNode* stackTop; // 将头点作为栈顶
ListNode* stackTop; // 将头点作为栈顶
int stkSize; // 栈的长度
public:
@@ -19,7 +19,7 @@ public:
}
~LinkedListStack() {
// 遍历链表删除点,释放内存
// 遍历链表删除点,释放内存
freeMemoryLinkedList(stackTop);
}

View File

@@ -9,11 +9,11 @@
/* AVL 树 */
class AVLTree {
public:
TreeNode* root; // 根
TreeNode* root; // 根
private:
/* 更新点高度 */
/* 更新点高度 */
void updateHeight(TreeNode* node) {
// 点高度等于最高子树高度 + 1
// 点高度等于最高子树高度 + 1
node->height = max(height(node->left), height(node->right)) + 1;
}
@@ -24,10 +24,10 @@ private:
// 以 child 为原点,将 node 向右旋转
child->right = node;
node->left = grandChild;
// 更新点高度
// 更新点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
@@ -38,16 +38,16 @@ private:
// 以 child 为原点,将 node 向左旋转
child->left = node;
node->right = grandChild;
// 更新点高度
// 更新点高度
updateHeight(node);
updateHeight(child);
// 返回旋转后子树的根
// 返回旋转后子树的根
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode* rotate(TreeNode* node) {
// 获取点 node 的平衡因子
// 获取点 node 的平衡因子
int _balanceFactor = balanceFactor(node);
// 左偏树
if (_balanceFactor > 1) {
@@ -75,40 +75,40 @@ private:
return node;
}
/* 递归插入点(辅助方法) */
/* 递归插入点(辅助方法) */
TreeNode* insertHelper(TreeNode* node, int val) {
if (node == nullptr)
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;
}
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
/* 获取中序遍历中的下一个点(仅适用于 root 有左子点的情况) */
TreeNode* getInOrderNext(TreeNode* node) {
if (node == nullptr)
return node;
// 循环访问左子点,直到叶点时为最小点,跳出
// 循环访问左子点,直到叶点时为最小点,跳出
while (node->left != nullptr) {
node = node->left;
}
return node;
}
/* 递归删除点(辅助方法) */
/* 递归删除点(辅助方法) */
TreeNode* removeHelper(TreeNode* node, int val) {
if (node == nullptr)
return nullptr;
/* 1. 查找点,并删除之 */
/* 1. 查找点,并删除之 */
if (val < node->val)
node->left = removeHelper(node->left, val);
else if (val > node->val)
@@ -116,74 +116,74 @@ private:
else {
if (node->left == nullptr || node->right == nullptr) {
TreeNode* child = node->left != nullptr ? node->left : node->right;
// 子点数量 = 0 ,直接删除 node 并返回
// 子点数量 = 0 ,直接删除 node 并返回
if (child == nullptr) {
delete node;
return nullptr;
}
// 子点数量 = 1 ,直接删除 node
// 子点数量 = 1 ,直接删除 node
else {
delete node;
node = child;
}
} else {
// 子点数量 = 2 ,则将中序遍历的下个点删除,并用该点替换当前
// 子点数量 = 2 ,则将中序遍历的下个点删除,并用该点替换当前
TreeNode* temp = getInOrderNext(node->right);
int tempVal = temp->val;
node->right = removeHelper(node->right, temp->val);
node->val = tempVal;
}
}
updateHeight(node); // 更新点高度
updateHeight(node); // 更新点高度
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
// 返回子树的根
// 返回子树的根
return node;
}
public:
/* 获取点高度 */
/* 获取点高度 */
int height(TreeNode* node) {
// 空点高度为 -1 ,叶点高度为 0
// 空点高度为 -1 ,叶点高度为 0
return node == nullptr ? -1 : node->height;
}
/* 获取平衡因子 */
int balanceFactor(TreeNode* node) {
// 空点平衡因子为 0
// 空点平衡因子为 0
if (node == nullptr) return 0;
// 点平衡因子 = 左子树高度 - 右子树高度
// 点平衡因子 = 左子树高度 - 右子树高度
return height(node->left) - height(node->right);
}
/* 插入点 */
/* 插入点 */
TreeNode* insert(int val) {
root = insertHelper(root, val);
return root;
}
/* 删除点 */
/* 删除点 */
TreeNode* remove(int val) {
root = removeHelper(root, val);
return root;
}
/* 查找点 */
/* 查找点 */
TreeNode* search(int val) {
TreeNode* cur = root;
// 循环查找,越过叶点后跳出
// 循环查找,越过叶点后跳出
while (cur != nullptr) {
// 目标点在 cur 的右子树中
// 目标点在 cur 的右子树中
if (cur->val < val)
cur = cur->right;
// 目标点在 cur 的左子树中
// 目标点在 cur 的左子树中
else if (cur->val > val)
cur = cur->left;
// 找到目标点,跳出循环
// 找到目标点,跳出循环
else
break;
}
// 返回目标
// 返回目标
return cur;
}
@@ -198,21 +198,21 @@ public:
void testInsert(AVLTree& tree, int val) {
tree.insert(val);
cout << "\n插入" << val << "AVL 树为" << endl;
cout << "\n插入" << val << "AVL 树为" << endl;
PrintUtil::printTree(tree.root);
}
void testRemove(AVLTree& tree, int val) {
tree.remove(val);
cout << "\n删除" << val << "AVL 树为" << endl;
cout << "\n删除" << val << "AVL 树为" << endl;
PrintUtil::printTree(tree.root);
}
int main() {
/* 初始化空 AVL 树 */
AVLTree avlTree;
/* 插入点 */
// 请关注插入点后AVL 树是如何保持平衡的
/* 插入点 */
// 请关注插入点后AVL 树是如何保持平衡的
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
@@ -224,16 +224,16 @@ int main() {
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);
cout << "\n查找到的点对象为 " << node << "点值 = " << node->val << endl;
cout << "\n查找到的点对象为 " << node << "点值 = " << node->val << endl;
}

View File

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

View File

@@ -10,7 +10,7 @@
/* Driver Code */
int main() {
/* 初始化二叉树 */
// 初始化
// 初始化
TreeNode* n1 = new TreeNode(1);
TreeNode* n2 = new TreeNode(2);
TreeNode* n3 = new TreeNode(3);
@@ -24,17 +24,17 @@ int main() {
cout << endl << "初始化二叉树\n" << endl;
PrintUtil::printTree(n1);
/* 插入与删除点 */
/* 插入与删除点 */
TreeNode* P = new TreeNode(0);
// 在 n1 -> n2 中间插入点 P
// 在 n1 -> n2 中间插入点 P
n1->left = P;
P->left = n2;
cout << endl << "插入点 P 后\n" << endl;
cout << endl << "插入点 P 后\n" << endl;
PrintUtil::printTree(n1);
// 删除点 P
// 删除点 P
n1->left = n2;
delete P; // 释放内存
cout << endl << "删除点 P 后\n" << endl;
cout << endl << "删除点 P 后\n" << endl;
PrintUtil::printTree(n1);
// 释放内存

View File

@@ -8,7 +8,7 @@
/* 层序遍历 */
vector<int> levelOrder(TreeNode* root) {
// 初始化队列,加入根
// 初始化队列,加入根
queue<TreeNode*> queue;
queue.push(root);
// 初始化一个列表,用于保存遍历序列
@@ -16,11 +16,11 @@ vector<int> levelOrder(TreeNode* root) {
while (!queue.empty()) {
TreeNode* node = queue.front();
queue.pop(); // 队列出队
vec.push_back(node->val); // 保存点值
vec.push_back(node->val); // 保存点值
if (node->left != nullptr)
queue.push(node->left); // 左子点入队
queue.push(node->left); // 左子点入队
if (node->right != nullptr)
queue.push(node->right); // 右子点入队
queue.push(node->right); // 右子点入队
}
return vec;
}
@@ -36,7 +36,7 @@ int main() {
/* 层序遍历 */
vector<int> vec = levelOrder(root);
cout << endl << "层序遍历的点打印序列 = ";
cout << endl << "层序遍历的点打印序列 = ";
PrintUtil::printVector(vec);
return 0;

View File

@@ -12,7 +12,7 @@ vector<int> vec;
/* 前序遍历 */
void preOrder(TreeNode* root) {
if (root == nullptr) return;
// 访问优先级:根点 -> 左子树 -> 右子树
// 访问优先级:根点 -> 左子树 -> 右子树
vec.push_back(root->val);
preOrder(root->left);
preOrder(root->right);
@@ -21,7 +21,7 @@ void preOrder(TreeNode* root) {
/* 中序遍历 */
void inOrder(TreeNode* root) {
if (root == nullptr) return;
// 访问优先级:左子树 -> 根点 -> 右子树
// 访问优先级:左子树 -> 根点 -> 右子树
inOrder(root->left);
vec.push_back(root->val);
inOrder(root->right);
@@ -30,7 +30,7 @@ void inOrder(TreeNode* root) {
/* 后序遍历 */
void postOrder(TreeNode* root) {
if (root == nullptr) return;
// 访问优先级:左子树 -> 右子树 -> 根
// 访问优先级:左子树 -> 右子树 -> 根
postOrder(root->left);
postOrder(root->right);
vec.push_back(root->val);
@@ -48,19 +48,19 @@ int main() {
/* 前序遍历 */
vec.clear();
preOrder(root);
cout << endl << "前序遍历的点打印序列 = ";
cout << endl << "前序遍历的点打印序列 = ";
PrintUtil::printVector(vec);
/* 中序遍历 */
vec.clear();
inOrder(root);
cout << endl << "中序遍历的点打印序列 = ";
cout << endl << "中序遍历的点打印序列 = ";
PrintUtil::printVector(vec);
/* 后序遍历 */
vec.clear();
postOrder(root);
cout << endl << "后序遍历的点打印序列 = ";
cout << endl << "后序遍历的点打印序列 = ";
PrintUtil::printVector(vec);
return 0;