mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Publish the C codes to the docs. (#469)
This commit is contained in:
@ -34,9 +34,8 @@
|
||||
|
||||
```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"
|
||||
@ -74,7 +73,9 @@
|
||||
=== "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#"
|
||||
|
||||
@ -62,7 +62,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* AVL 树节点类 */
|
||||
/* AVL 树节点结构体 */
|
||||
type TreeNode struct {
|
||||
Val int // 节点值
|
||||
Height int // 节点高度
|
||||
@ -74,6 +74,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "JavaScript"
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val; // 节点值
|
||||
height; //节点高度
|
||||
@ -91,6 +92,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val: number; // 节点值
|
||||
height: number; // 节点高度
|
||||
@ -108,7 +110,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#"
|
||||
@ -200,9 +222,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{height}
|
||||
[class]{}-[func]{height}
|
||||
|
||||
[class]{aVLTree}-[func]{updateHeight}
|
||||
[class]{}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -272,7 +294,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{balanceFactor}
|
||||
[class]{}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -364,7 +386,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{rightRotate}
|
||||
[class]{}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -436,7 +458,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{leftRotate}
|
||||
[class]{}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -529,7 +551,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{rotate}
|
||||
[class]{}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -609,7 +631,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{insert}
|
||||
|
||||
[class]{aVLTree}-[func]{insertHelper}
|
||||
[class]{}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -691,9 +713,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
[class]{aVLTree}-[func]{remove}
|
||||
[class]{aVLTree}-[func]{removeNode}
|
||||
|
||||
[class]{aVLTree}-[func]{removeHelper}
|
||||
[class]{}-[func]{removeHelper}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@ -237,7 +237,7 @@
|
||||
=== "C"
|
||||
|
||||
```c title="binary_search_tree.c"
|
||||
[class]{binarySearchTree}-[func]{remove}
|
||||
[class]{binarySearchTree}-[func]{removeNode}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@ -87,7 +87,27 @@
|
||||
=== "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#"
|
||||
@ -256,7 +276,18 @@
|
||||
=== "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#"
|
||||
@ -376,7 +407,13 @@
|
||||
=== "C"
|
||||
|
||||
```c title="binary_tree.c"
|
||||
|
||||
/* 插入与删除节点 */
|
||||
TreeNode *P = newTreeNode(0);
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
// 删除节点 P
|
||||
n1->left = n2;
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user