This commit is contained in:
krahets
2023-10-27 23:48:10 +08:00
parent a42c1d62b9
commit 368cbf4261
10 changed files with 195 additions and 212 deletions
@@ -1067,21 +1067,24 @@ comments: true
=== "C"
```c title="array_binary_tree.c"
/* 数组表示下的二叉树 */
/* 数组表示下的二叉树结构体 */
typedef struct {
vector *tree;
int *tree;
int size;
} ArrayBinaryTree;
/* 构造函数 */
ArrayBinaryTree *newArrayBinaryTree(vector *arr) {
ArrayBinaryTree *newABT = malloc(sizeof(ArrayBinaryTree));
newABT->tree = arr;
return newABT;
ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) {
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
abt->tree = malloc(sizeof(int) * arrSize);
memcpy(abt->tree, arr, sizeof(int) * arrSize);
abt->size = arrSize;
return abt;
}
/* 节点数量 */
int size(ArrayBinaryTree *abt) {
return abt->tree->size;
return abt->size;
}
/* 获取索引为 i 节点的值 */
@@ -1089,64 +1092,64 @@ comments: true
// 若索引越界,则返回 INT_MAX ,代表空位
if (i < 0 || i >= size(abt))
return INT_MAX;
return *(int *)abt->tree->data[i];
return abt->tree[i];
}
/* 层序遍历 */
int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
// 直接遍历数组
for (int i = 0; i < size(abt); i++) {
if (val(abt, i) != INT_MAX)
res[index++] = val(abt, i);
}
*returnSize = index;
return res;
}
/* 深度优先遍历 */
void dfs(ArrayBinaryTree *abt, int i, const char *order, vector *res) {
void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
// 若为空位,则返回
if (val(abt, i) == INT_MAX)
return;
// 前序遍历
if (strcmp(order, "pre") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
dfs(abt, left(i), order, res);
if (strcmp(order, "pre") == 0)
res[(*index)++] = val(abt, i);
dfs(abt, left(i), order, res, index);
// 中序遍历
if (strcmp(order, "in") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
dfs(abt, right(i), order, res);
if (strcmp(order, "in") == 0)
res[(*index)++] = val(abt, i);
dfs(abt, right(i), order, res, index);
// 后序遍历
if (strcmp(order, "post") == 0) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(tmp));
}
}
/* 层序遍历 */
vector *levelOrder(ArrayBinaryTree *abt) {
vector *res = newVector();
// 直接遍历数组
for (int i = 0; i < size(abt); i++) {
if (val(abt, i) != INT_MAX) {
int tmp = val(abt, i);
vectorPushback(res, &tmp, sizeof(int));
}
}
return res;
if (strcmp(order, "post") == 0)
res[(*index)++] = val(abt, i);
}
/* 前序遍历 */
vector *preOrder(ArrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "pre", res);
int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "pre", res, &index);
*returnSize = index;
return res;
}
/* 中序遍历 */
vector *inOrder(ArrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "in", res);
int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "in", res, &index);
*returnSize = index;
return res;
}
/* 后序遍历 */
vector *postOrder(ArrayBinaryTree *abt) {
vector *res = newVector();
dfs(abt, 0, "post", res);
int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
int *res = (int *)malloc(sizeof(int) * size(abt));
int index = 0;
dfs(abt, 0, "post", res, &index);
*returnSize = index;
return res;
}
```
+7 -7
View File
@@ -227,19 +227,19 @@ comments: true
```rust title="binary_tree_bfs.rs"
/* 层序遍历 */
fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
// 初始化队列,加入根
// 初始化队列,加入根
let mut que = VecDeque::new();
que.push_back(Rc::clone(&root));
// 初始化一个列表,用于保存遍历序列
let mut vec = Vec::new();
while let Some(node) = que.pop_front() { // 队列出队
vec.push(node.borrow().val); // 保存点值
vec.push(node.borrow().val); // 保存点值
if let Some(left) = node.borrow().left.as_ref() {
que.push_back(Rc::clone(left)); // 左子点入队
que.push_back(Rc::clone(left)); // 左子点入队
}
if let Some(right) = node.borrow().right.as_ref() {
que.push_back(Rc::clone(right)); // 右子点入队
que.push_back(Rc::clone(right)); // 右子点入队
};
}
vec
@@ -654,7 +654,7 @@ comments: true
let mut result = vec![];
if let Some(node) = root {
// 访问优先级:根点 -> 左子树 -> 右子树
// 访问优先级:根点 -> 左子树 -> 右子树
result.push(node.borrow().val);
result.append(&mut pre_order(node.borrow().left.as_ref()));
result.append(&mut pre_order(node.borrow().right.as_ref()));
@@ -667,7 +667,7 @@ comments: true
let mut result = vec![];
if let Some(node) = root {
// 访问优先级:左子树 -> 根点 -> 右子树
// 访问优先级:左子树 -> 根点 -> 右子树
result.append(&mut in_order(node.borrow().left.as_ref()));
result.push(node.borrow().val);
result.append(&mut in_order(node.borrow().right.as_ref()));
@@ -680,7 +680,7 @@ comments: true
let mut result = vec![];
if let Some(node) = root {
// 访问优先级:左子树 -> 右子树 -> 根
// 访问优先级:左子树 -> 右子树 -> 根
result.append(&mut post_order(node.borrow().left.as_ref()));
result.append(&mut post_order(node.borrow().right.as_ref()));
result.push(node.borrow().val);
+2 -2
View File
@@ -30,7 +30,7 @@ comments: true
!!! question "为什么 DFS 遍历二叉树有前、中、后三种顺序,分别有什么用呢?"
DFS 的前、中、后序遍历和访问数组的顺序类似,是遍历二叉树的基本方法,利用这三种遍历方法,我们可以得到一个特定顺序的遍历结果。例如在二叉搜索树中,由于点大小满足 `左子点值 < 根点值 < 右子点值` ,因此我们只要按照 `左->根->右` 的优先级遍历树,就可以获得有序的节点序列。
DFS 的前、中、后序遍历和访问数组的顺序类似,是遍历二叉树的基本方法,利用这三种遍历方法,我们可以得到一个特定顺序的遍历结果。例如在二叉搜索树中,由于点大小满足 `左子点值 < 根点值 < 右子点值` ,因此我们只要按照 `左->根->右` 的优先级遍历树,就可以获得有序的节点序列。
!!! question "右旋操作是处理失衡节点 `node``child``grand_child` 之间的关系,那 `node` 的父节点和 `node` 原来的连接不需要维护吗?右旋操作后岂不是断掉了?"
@@ -38,7 +38,7 @@ comments: true
!!! question "在 C++ 中,函数被划分到 `private``public` 中,这方面有什么考量吗?为什么要将 `height()` 函数和 `updateHeight()` 函数分别放在 `public``private` 中呢?"
主要看方法的使用范围,如果方法只在类内部使用,那么就设计为 `private` 。例如,用户单独调用 `updateHeight()` 是没有意义的,它只是插入、删除操作中的一步。而 `height()` 是访问点高度,类似于 `vector.size()` ,因此设置成 `public` 以便使用。
主要看方法的使用范围,如果方法只在类内部使用,那么就设计为 `private` 。例如,用户单独调用 `updateHeight()` 是没有意义的,它只是插入、删除操作中的一步。而 `height()` 是访问点高度,类似于 `vector.size()` ,因此设置成 `public` 以便使用。
!!! question "请问如何从一组输入数据构建一个二叉搜索树?根节点的选择是不是很重要?"