mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-23 18:11:45 +08:00
Simplify struct declarations of C.
Use PascalCase for all structs in C. SImplify n_queens.c Format C code for chapter of graph.
This commit is contained in:
@@ -7,26 +7,24 @@
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 数组表示下的二叉树类 */
|
||||
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;
|
||||
@@ -49,7 +47,7 @@ int parent(int i) {
|
||||
}
|
||||
|
||||
/* 深度优先遍历 */
|
||||
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;
|
||||
@@ -73,7 +71,7 @@ void dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
vector *levelOrder(arrayBinaryTree *abt) {
|
||||
vector *levelOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(abt); i++) {
|
||||
@@ -86,21 +84,21 @@ vector *levelOrder(arrayBinaryTree *abt) {
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
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;
|
||||
@@ -129,7 +127,7 @@ int main() {
|
||||
vectorPushback(vArr, &arr[i], sizeof(int));
|
||||
}
|
||||
// 数组表示下的二叉树类
|
||||
arrayBinaryTree *abt = newArrayBinaryTree(vArr);
|
||||
ArrayBinaryTree *abt = newArrayBinaryTree(vArr);
|
||||
|
||||
// 访问节点
|
||||
int i = 1;
|
||||
|
||||
@@ -7,15 +7,13 @@
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* AVL Tree */
|
||||
struct aVLTree {
|
||||
typedef struct {
|
||||
TreeNode *root;
|
||||
};
|
||||
|
||||
typedef struct aVLTree aVLTree;
|
||||
} AVLTree;
|
||||
|
||||
/* 构建 AVL 树 */
|
||||
aVLTree *newAVLTree() {
|
||||
aVLTree *tree = (aVLTree *)malloc(sizeof(aVLTree));
|
||||
AVLTree *newAVLTree() {
|
||||
AVLTree *tree = (AVLTree *)malloc(sizeof(AVLTree));
|
||||
tree->root = NULL;
|
||||
return tree;
|
||||
}
|
||||
@@ -134,7 +132,7 @@ TreeNode *insertHelper(TreeNode *node, int val) {
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
void insert(aVLTree *tree, int val) {
|
||||
void insert(AVLTree *tree, int val) {
|
||||
tree->root = insertHelper(tree->root, val);
|
||||
}
|
||||
|
||||
@@ -183,12 +181,12 @@ TreeNode *removeHelper(TreeNode *node, int val) {
|
||||
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeItem(aVLTree *tree, int val) {
|
||||
void removeItem(AVLTree *tree, int val) {
|
||||
TreeNode *root = removeHelper(tree->root, val);
|
||||
}
|
||||
|
||||
/* 查找节点 */
|
||||
TreeNode *search(aVLTree *tree, int val) {
|
||||
TreeNode *search(AVLTree *tree, int val) {
|
||||
TreeNode *cur = tree->root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
@@ -207,13 +205,13 @@ TreeNode *search(aVLTree *tree, int val) {
|
||||
return cur;
|
||||
}
|
||||
|
||||
void testInsert(aVLTree *tree, int val) {
|
||||
void testInsert(AVLTree *tree, int val) {
|
||||
insert(tree, val);
|
||||
printf("\n插入节点 %d 后,AVL 树为 \n", val);
|
||||
printTree(tree->root);
|
||||
}
|
||||
|
||||
void testRemove(aVLTree *tree, int val) {
|
||||
void testRemove(AVLTree *tree, int val) {
|
||||
removeItem(tree, val);
|
||||
printf("\n删除节点 %d 后,AVL 树为 \n", val);
|
||||
printTree(tree->root);
|
||||
@@ -222,7 +220,7 @@ void testRemove(aVLTree *tree, int val) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化空 AVL 树 */
|
||||
aVLTree *tree = (aVLTree *)newAVLTree();
|
||||
AVLTree *tree = (AVLTree *)newAVLTree();
|
||||
/* 插入节点 */
|
||||
// 请关注插入节点后,AVL 树是如何保持平衡的
|
||||
testInsert(tree, 1);
|
||||
|
||||
@@ -7,11 +7,9 @@
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 二叉搜索树 */
|
||||
struct binarySearchTree {
|
||||
typedef struct {
|
||||
TreeNode *root;
|
||||
};
|
||||
|
||||
typedef struct binarySearchTree binarySearchTree;
|
||||
} BinarySearchTree;
|
||||
|
||||
/* 比较器:从小到大排序 */
|
||||
int sortIntHelper(const void *a, const void *b) {
|
||||
@@ -32,8 +30,8 @@ TreeNode *buildTree(int nums[], int i, int j) {
|
||||
return root;
|
||||
}
|
||||
|
||||
binarySearchTree *newBinarySearchTree(int nums[], int size) {
|
||||
binarySearchTree *bst = (binarySearchTree *)malloc(sizeof(binarySearchTree));
|
||||
BinarySearchTree *newBinarySearchTree(int nums[], int size) {
|
||||
BinarySearchTree *bst = (BinarySearchTree *)malloc(sizeof(BinarySearchTree));
|
||||
TreeNode *root;
|
||||
// 从小到大排序数组
|
||||
qsort(nums, size, sizeof(int), sortIntHelper);
|
||||
@@ -44,12 +42,12 @@ binarySearchTree *newBinarySearchTree(int nums[], int size) {
|
||||
}
|
||||
|
||||
/* 获取二叉树根节点 */
|
||||
TreeNode *getRoot(binarySearchTree *bst) {
|
||||
TreeNode *getRoot(BinarySearchTree *bst) {
|
||||
return bst->root;
|
||||
}
|
||||
|
||||
/* 查找节点 */
|
||||
TreeNode *search(binarySearchTree *bst, int num) {
|
||||
TreeNode *search(BinarySearchTree *bst, int num) {
|
||||
TreeNode *cur = bst->root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != NULL) {
|
||||
@@ -69,7 +67,7 @@ TreeNode *search(binarySearchTree *bst, int num) {
|
||||
}
|
||||
|
||||
/* 插入节点 */
|
||||
void insert(binarySearchTree *bst, int num) {
|
||||
void insert(BinarySearchTree *bst, int num) {
|
||||
// 若树为空,则初始化根节点
|
||||
if (bst->root == NULL) {
|
||||
bst->root = newTreeNode(num);
|
||||
@@ -102,7 +100,7 @@ void insert(binarySearchTree *bst, int num) {
|
||||
|
||||
/* 删除节点 */
|
||||
// 由于引入了 stdio.h ,此处无法使用 remove 关键词
|
||||
void removeItem(binarySearchTree *bst, int num) {
|
||||
void removeItem(BinarySearchTree *bst, int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (bst->root == NULL)
|
||||
return;
|
||||
@@ -154,7 +152,7 @@ void removeItem(binarySearchTree *bst, int num) {
|
||||
int main() {
|
||||
/* 初始化二叉搜索树 */
|
||||
int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
binarySearchTree *bst = newBinarySearchTree(nums, sizeof(nums) / sizeof(int));
|
||||
BinarySearchTree *bst = newBinarySearchTree(nums, sizeof(nums) / sizeof(int));
|
||||
printf("初始化的二叉树为\n");
|
||||
printTree(getRoot(bst));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user