mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
build
This commit is contained in:
@@ -1068,26 +1068,24 @@ comments: true
|
||||
|
||||
```c title="array_binary_tree.c"
|
||||
/* 数组表示下的二叉树类 */
|
||||
struct arrayBinaryTree {
|
||||
typedef struct {
|
||||
vector *tree;
|
||||
};
|
||||
|
||||
typedef struct arrayBinaryTree arrayBinaryTree;
|
||||
} ArrayBinaryTree;
|
||||
|
||||
/* 构造函数 */
|
||||
arrayBinaryTree *newArrayBinaryTree(vector *arr) {
|
||||
arrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));
|
||||
ArrayBinaryTree *newArrayBinaryTree(vector *arr) {
|
||||
ArrayBinaryTree *newABT = malloc(sizeof(ArrayBinaryTree));
|
||||
newABT->tree = arr;
|
||||
return newABT;
|
||||
}
|
||||
|
||||
/* 节点数量 */
|
||||
int size(arrayBinaryTree *abt) {
|
||||
int size(ArrayBinaryTree *abt) {
|
||||
return abt->tree->size;
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
int val(arrayBinaryTree *abt, int i) {
|
||||
int val(ArrayBinaryTree *abt, int i) {
|
||||
// 若索引越界,则返回 INT_MAX ,代表空位
|
||||
if (i < 0 || i >= size(abt))
|
||||
return INT_MAX;
|
||||
@@ -1095,7 +1093,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 深度优先遍历 */
|
||||
void dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {
|
||||
void dfs(ArrayBinaryTree *abt, int i, const char *order, vector *res) {
|
||||
// 若为空位,则返回
|
||||
if (val(abt, i) == INT_MAX)
|
||||
return;
|
||||
@@ -1119,7 +1117,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
vector *levelOrder(arrayBinaryTree *abt) {
|
||||
vector *levelOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(abt); i++) {
|
||||
@@ -1132,21 +1130,21 @@ comments: true
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
vector *preOrder(arrayBinaryTree *abt) {
|
||||
vector *preOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
vector *inOrder(arrayBinaryTree *abt) {
|
||||
vector *inOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
vector *postOrder(arrayBinaryTree *abt) {
|
||||
vector *postOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "post", res);
|
||||
return res;
|
||||
|
||||
@@ -189,14 +189,12 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
|
||||
```c title=""
|
||||
/* AVL 树节点结构体 */
|
||||
struct TreeNode {
|
||||
TreeNode struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
};
|
||||
|
||||
typedef struct TreeNode TreeNode;
|
||||
} TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
@@ -1836,7 +1834,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
|
||||
```c title="avl_tree.c"
|
||||
/* 插入节点 */
|
||||
void insert(aVLTree *tree, int val) {
|
||||
void insert(AVLTree *tree, int val) {
|
||||
tree->root = insertHelper(tree->root, val);
|
||||
}
|
||||
|
||||
@@ -2361,7 +2359,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
```c title="avl_tree.c"
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeItem(aVLTree *tree, int val) {
|
||||
void removeItem(AVLTree *tree, int val) {
|
||||
TreeNode *root = removeHelper(tree->root, val);
|
||||
}
|
||||
|
||||
|
||||
@@ -274,7 +274,7 @@ comments: true
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
/* 查找节点 */
|
||||
TreeNode *search(binarySearchTree *bst, int num) {
|
||||
TreeNode *search(BinarySearchTree *bst, int num) {
|
||||
TreeNode *cur = bst->root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
@@ -673,7 +673,7 @@ comments: true
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
/* 插入节点 */
|
||||
void insert(binarySearchTree *bst, int num) {
|
||||
void insert(BinarySearchTree *bst, int num) {
|
||||
// 若树为空,则初始化根节点
|
||||
if (bst->root == NULL) {
|
||||
bst->root = newTreeNode(num);
|
||||
@@ -1357,7 +1357,7 @@ comments: true
|
||||
```c title="binary_search_tree.c"
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeItem(binarySearchTree *bst, int num) {
|
||||
void removeItem(BinarySearchTree *bst, int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (bst->root == NULL)
|
||||
return;
|
||||
|
||||
@@ -161,14 +161,12 @@ comments: true
|
||||
|
||||
```c title=""
|
||||
/* 二叉树节点结构体 */
|
||||
struct TreeNode {
|
||||
typedef struct TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
struct TreeNode *left; // 左子节点指针
|
||||
struct TreeNode *right; // 右子节点指针
|
||||
};
|
||||
|
||||
typedef struct TreeNode TreeNode;
|
||||
} TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
@@ -616,11 +614,11 @@ comments: true
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| | 完美二叉树 | 链表 |
|
||||
| ----------------------------- | ---------- | ---------- |
|
||||
| 第 $i$ 层的节点数量 | $2^{i-1}$ | $1$ |
|
||||
| 高度 $h$ 树的叶节点数量 | $2^h$ | $1$ |
|
||||
| 高度 $h$ 树的节点总数 | $2^{h+1} - 1$ | $h + 1$ |
|
||||
| 节点总数 $n$ 树的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |
|
||||
| | 完美二叉树 | 链表 |
|
||||
| ----------------------- | ------------------ | ------- |
|
||||
| 第 $i$ 层的节点数量 | $2^{i-1}$ | $1$ |
|
||||
| 高度 $h$ 树的叶节点数量 | $2^h$ | $1$ |
|
||||
| 高度 $h$ 树的节点总数 | $2^{h+1} - 1$ | $h + 1$ |
|
||||
| 节点总数 $n$ 树的高度 | $\log_2 (n+1) - 1$ | $n - 1$ |
|
||||
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user