mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-25 11:04:18 +08:00
build
This commit is contained in:
@@ -42,9 +42,8 @@ comments: true
|
||||
|
||||
```cpp title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 为了符合数据类型为 int ,使用 int 最大值标记空位
|
||||
// 该方法的使用前提是没有节点的值 = INT_MAX
|
||||
vector<int> tree = { 1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15 };
|
||||
// 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX
|
||||
vector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -82,7 +81,9 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 int 最大值标记空位,因此要求节点值不能为 INT_MAX
|
||||
int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -70,7 +70,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* AVL 树节点类 */
|
||||
/* AVL 树节点结构体 */
|
||||
type TreeNode struct {
|
||||
Val int // 节点值
|
||||
Height int // 节点高度
|
||||
@@ -82,6 +82,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val; // 节点值
|
||||
height; //节点高度
|
||||
@@ -99,6 +100,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val: number; // 节点值
|
||||
height: number; // 节点高度
|
||||
@@ -116,7 +118,27 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* AVL 树节点结构体 */
|
||||
struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
};
|
||||
|
||||
typedef struct TreeNode TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -267,9 +289,26 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{height}
|
||||
/* 获取节点高度 */
|
||||
int height(TreeNode *node) {
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
if (node != NULL) {
|
||||
return node->height;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
[class]{aVLTree}-[func]{updateHeight}
|
||||
/* 更新节点高度 */
|
||||
void updateHeight(TreeNode *node) {
|
||||
int lh = height(node->left);
|
||||
int rh = height(node->right);
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
if (lh > rh) {
|
||||
node->height = lh + 1;
|
||||
} else {
|
||||
node->height = rh + 1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -406,7 +445,15 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{balanceFactor}
|
||||
/* 获取平衡因子 */
|
||||
int balanceFactor(TreeNode *node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == NULL) {
|
||||
return 0;
|
||||
}
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node->left) - height(node->right);
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -590,7 +637,20 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{rightRotate}
|
||||
/* 右旋操作 */
|
||||
TreeNode *rightRotate(TreeNode *node) {
|
||||
TreeNode *child, *grandChild;
|
||||
child = node->left;
|
||||
grandChild = child->right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child->right = node;
|
||||
node->left = grandChild;
|
||||
// 更新节点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -774,7 +834,20 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{leftRotate}
|
||||
/* 左旋操作 */
|
||||
TreeNode *leftRotate(TreeNode *node) {
|
||||
TreeNode *child, *grandChild;
|
||||
child = node->right;
|
||||
grandChild = child->left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child->left = node;
|
||||
node->right = grandChild;
|
||||
// 更新节点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -1074,7 +1147,35 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{rotate}
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode *rotate(TreeNode *node) {
|
||||
// 获取节点 node 的平衡因子
|
||||
int bf = balanceFactor(node);
|
||||
// 左偏树
|
||||
if (bf > 1) {
|
||||
if (balanceFactor(node->left) >= 0) {
|
||||
// 右旋
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node->left = leftRotate(node->left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (bf < -1) {
|
||||
if (balanceFactor(node->right) <= 0) {
|
||||
// 左旋
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node->right = rightRotate(node->right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -1358,9 +1459,32 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{insert}
|
||||
/* 插入节点 */
|
||||
void insert(aVLTree *tree, int val) {
|
||||
tree->root = insertHelper(tree->root, val);
|
||||
}
|
||||
|
||||
[class]{aVLTree}-[func]{insertHelper}
|
||||
/* 递归插入节点(辅助方法) */
|
||||
TreeNode *insertHelper(TreeNode *node, int val) {
|
||||
if (node == NULL) {
|
||||
return newTreeNode(val);
|
||||
}
|
||||
/* 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);
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -1726,9 +1850,54 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{remove}
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeNode(aVLTree *tree, int val) {
|
||||
TreeNode *root = removeHelper(tree->root, val);
|
||||
}
|
||||
|
||||
[class]{aVLTree}-[func]{removeHelper}
|
||||
/* 递归删除节点(辅助方法) */
|
||||
TreeNode *removeHelper(TreeNode *node, int val) {
|
||||
TreeNode *child, *grandChild;
|
||||
if (node == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if (val < node->val) {
|
||||
node->left = removeHelper(node->left, val);
|
||||
} else if (val > node->val) {
|
||||
node->right = removeHelper(node->right, val);
|
||||
} else {
|
||||
if (node->left == NULL || node->right == NULL) {
|
||||
child = node->left;
|
||||
if (node->right != NULL) {
|
||||
child = node->right;
|
||||
}
|
||||
// 子节点数量 = 0 ,直接删除 node 并返回
|
||||
if (child == NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
// 子节点数量 = 1 ,直接删除 node
|
||||
node = child;
|
||||
}
|
||||
} else {
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
TreeNode *temp = node->right;
|
||||
while (temp->left != NULL) {
|
||||
temp = temp->left;
|
||||
}
|
||||
int tempVal = temp->val;
|
||||
node->right = removeHelper(node->right, temp->val);
|
||||
node->val = tempVal;
|
||||
}
|
||||
}
|
||||
// 更新节点高度
|
||||
updateHeight(node);
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -171,7 +171,25 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{search}
|
||||
/* 查找节点 */
|
||||
TreeNode *search(binarySearchTree *bst, int num) {
|
||||
TreeNode *cur = bst->root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
if (cur->val < num) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
cur = cur->right;
|
||||
} else if (cur->val > num) {
|
||||
// 目标节点在 cur 的左子树中
|
||||
cur = cur->left;
|
||||
} else {
|
||||
// 找到目标节点,跳出循环
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 返回目标节点
|
||||
return cur;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -448,7 +466,35 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{insert}
|
||||
/* 插入节点 */
|
||||
void insert(binarySearchTree *bst, int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (bst->root == NULL)
|
||||
return;
|
||||
TreeNode *cur = bst->root, *pre = NULL;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur->val == num) {
|
||||
return;
|
||||
}
|
||||
pre = cur;
|
||||
if (cur->val < num) {
|
||||
// 插入位置在 cur 的右子树中
|
||||
cur = cur->right;
|
||||
} else {
|
||||
// 插入位置在 cur 的左子树中
|
||||
cur = cur->left;
|
||||
}
|
||||
}
|
||||
// 插入节点 val
|
||||
TreeNode *node = newTreeNode(num);
|
||||
if (pre->val < num) {
|
||||
pre->right = node;
|
||||
} else {
|
||||
pre->left = node;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -903,7 +949,55 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{remove}
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeNode(binarySearchTree *bst, int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (bst->root == NULL)
|
||||
return;
|
||||
TreeNode *cur = bst->root, *pre = NULL;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
if (cur->val < num) {
|
||||
// 待删除节点在 root 的右子树中
|
||||
cur = cur->right;
|
||||
} else {
|
||||
// 待删除节点在 root 的左子树中
|
||||
cur = cur->left;
|
||||
}
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == NULL)
|
||||
return;
|
||||
// 判断待删除节点是否存在子节点
|
||||
if (cur->left == NULL || cur->right == NULL) {
|
||||
/* 子节点数量 = 0 or 1 */
|
||||
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
||||
TreeNode *child = cur->left != NULL ? cur->left : cur->right;
|
||||
// 删除节点 cur
|
||||
if (pre->left == cur) {
|
||||
pre->left = child;
|
||||
} else {
|
||||
pre->right = child;
|
||||
}
|
||||
} else {
|
||||
/* 子节点数量 = 2 */
|
||||
// 获取中序遍历中 cur 的下一个节点
|
||||
TreeNode *tmp = cur->right;
|
||||
while (tmp->left != NULL) {
|
||||
tmp = tmp->left;
|
||||
}
|
||||
int tmpVal = tmp->val;
|
||||
// 递归删除节点 tmp
|
||||
removeNode(bst, tmp->val);
|
||||
// 用 tmp 覆盖 cur
|
||||
cur->val = tmpVal;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -91,7 +91,27 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
|
||||
/* 二叉树节点结构体 */
|
||||
struct TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
struct TreeNode *left; // 左子节点指针
|
||||
struct TreeNode *right; // 右子节点指针
|
||||
};
|
||||
|
||||
typedef struct TreeNode TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -264,7 +284,18 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
TreeNode *n1 = newTreeNode(1);
|
||||
TreeNode *n2 = newTreeNode(2);
|
||||
TreeNode *n3 = newTreeNode(3);
|
||||
TreeNode *n4 = newTreeNode(4);
|
||||
TreeNode *n5 = newTreeNode(5);
|
||||
// 构建引用指向(即指针)
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
n2->left = n4;
|
||||
n2->right = n5;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -386,7 +417,13 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
|
||||
/* 插入与删除节点 */
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// 删除节点 P
|
||||
n1->left = n2;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -159,7 +159,44 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_bfs.c"
|
||||
[class]{}-[func]{levelOrder}
|
||||
/* 层序遍历 */
|
||||
int *levelOrder(TreeNode *root, int *size) {
|
||||
/* 辅助队列 */
|
||||
int front, rear;
|
||||
int index, *arr;
|
||||
TreeNode *node;
|
||||
TreeNode **queue;
|
||||
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 加入根节点
|
||||
queue[rear++] = root;
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
/* 辅助数组 */
|
||||
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
// 数组指针
|
||||
index = 0;
|
||||
while (front < rear) {
|
||||
// 队列出队
|
||||
node = queue[front++];
|
||||
// 保存节点值
|
||||
arr[index++] = node->val;
|
||||
if (node->left != NULL) {
|
||||
// 左子节点入队
|
||||
queue[rear++] = node->left;
|
||||
}
|
||||
if (node->right != NULL) {
|
||||
// 右子节点入队
|
||||
queue[rear++] = node->right;
|
||||
}
|
||||
}
|
||||
// 更新数组长度的值
|
||||
*size = index;
|
||||
arr = realloc(arr, sizeof(int) * (*size));
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -476,11 +513,35 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree_dfs.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
arr[(*size)++] = root->val;
|
||||
preOrder(root->left, size);
|
||||
preOrder(root->right, size);
|
||||
}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root->left, size);
|
||||
arr[(*size)++] = root->val;
|
||||
inOrder(root->right, size);
|
||||
}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode *root, int *size) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root->left, size);
|
||||
postOrder(root->right, size);
|
||||
arr[(*size)++] = root->val;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user