diff --git a/codes/c/CMakeLists.txt b/codes/c/CMakeLists.txt index dc9e56af3..1a208dd9d 100644 --- a/codes/c/CMakeLists.txt +++ b/codes/c/CMakeLists.txt @@ -9,3 +9,4 @@ add_subdirectory(include) add_subdirectory(chapter_computational_complexity) add_subdirectory(chapter_array_and_linkedlist) add_subdirectory(chapter_sorting) +add_subdirectory(chapter_tree) diff --git a/codes/c/chapter_tree/CMakeLists.txt b/codes/c/chapter_tree/CMakeLists.txt new file mode 100644 index 000000000..779315b7b --- /dev/null +++ b/codes/c/chapter_tree/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(binary_search binary_tree.c) +add_executable(binary_tree_bfs binary_tree_bfs.c) +add_executable(binary_tree_dfs binary_tree_dfs.c) +add_executable(binary_search_tree binary_search_tree.c) diff --git a/codes/c/chapter_tree/binary_search_tree.c b/codes/c/chapter_tree/binary_search_tree.c new file mode 100644 index 000000000..f993faec3 --- /dev/null +++ b/codes/c/chapter_tree/binary_search_tree.c @@ -0,0 +1,8 @@ +/** + * File: binary_search_tree.c + * Created Time: 2023-01-11 + * Author: Reanon (793584285@qq.com) + */ + +#include "../include/include.h" + diff --git a/codes/c/chapter_tree/binary_tree.c b/codes/c/chapter_tree/binary_tree.c new file mode 100644 index 000000000..0a2f946a7 --- /dev/null +++ b/codes/c/chapter_tree/binary_tree.c @@ -0,0 +1,43 @@ +/** + * File: binary_tree.c + * Created Time: 2023-01-11 + * Author: Reanon (793584285@qq.com) + */ + +#include "../include/include.h" + +/* Driver Code */ +int main() { + /* 初始化二叉树 */ + // 初始化结点 + 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; + printf("初始化二叉树\n"); + PrintTree(n1); + + /* 插入与删除结点 */ + TreeNode* P = NewTreeNode(0); + // 在 n1 -> n2 中间插入结点 P + n1->left = P; + P->left = n2; + printf("插入结点 P 后\n"); + PrintTree(n1); + + // 删除结点 P + n1->left = n2; + // 释放内存 + free(P); + printf("删除结点 P 后\n"); + PrintTree(n1); + + return 0; +} + diff --git a/codes/c/chapter_tree/binary_tree_bfs.c b/codes/c/chapter_tree/binary_tree_bfs.c new file mode 100644 index 000000000..abad3735f --- /dev/null +++ b/codes/c/chapter_tree/binary_tree_bfs.c @@ -0,0 +1,66 @@ +/** + * File: binary_tree_bfs.c + * Created Time: 2023-01-11 + * Author: Reanon (793584285@qq.com) + */ + +#include "../include/include.h" + +/* 层序遍历 */ +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; +} + + +/* Driver Code */ +int main() { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; + int size = sizeof(nums) / sizeof(int); + TreeNode *root = ArrayToTree(nums, size); + printf("初始化二叉树\n"); + PrintTree(root); + + /* 层序遍历 */ + // 需要传入数组的长度 + int *arr = levelOrder(root, &size); + printf("层序遍历的结点打印序列 = "); + PrintArray(arr, size); + + return 0; +} \ No newline at end of file diff --git a/codes/c/chapter_tree/binary_tree_dfs.c b/codes/c/chapter_tree/binary_tree_dfs.c new file mode 100644 index 000000000..f72e6687b --- /dev/null +++ b/codes/c/chapter_tree/binary_tree_dfs.c @@ -0,0 +1,72 @@ +/** + * File: binary_tree_dfs.c + * Created Time: 2023-01-11 + * Author: Reanon (793584285@qq.com) + */ + +#include "../include/include.h" + +/* 辅助数组,用于存储遍历序列 */ +int *arr; + +/* 前序遍历 */ +void preOrder(TreeNode *root, int *size) { + + if (root == NULL) return; + // 访问优先级:根结点 -> 左子树 -> 右子树 + arr[(*size)++] = root->val; + preOrder(root->left, size); + preOrder(root->right, size); +} + +/* 中序遍历 */ +void inOrder(TreeNode *root, int *size) { + if (root == NULL) return; + // 访问优先级:左子树 -> 根结点 -> 右子树 + inOrder(root->left, size); + arr[(*size)++] = root->val; + inOrder(root->right, size); +} + +/* 后序遍历 */ +void postOrder(TreeNode *root, int *size) { + if (root == NULL) return; + // 访问优先级:左子树 -> 右子树 -> 根结点 + postOrder(root->left, size); + postOrder(root->right, size); + arr[(*size)++] = root->val; +} + + +/* Driver Code */ +int main() { + /* 初始化二叉树 */ + // 这里借助了一个从数组直接生成二叉树的函数 + int nums[] = {1, 2, 3, 4, 5, 6, 7}; + int size = sizeof(nums) / sizt ceof(int); + TreeNode *root = ArrayToTree(nums, size); + printf("初始化二叉树\n"); + PrintTree(root); + + /* 前序遍历 */ + // 初始化辅助数组 + arr = (int *) malloc(sizeof(int) * MAX_NODE_SIZE); + size = 0; + preOrder(root, &size); + printf("前序遍历的结点打印序列 = "); + PrintArray(arr, size); + + /* 中序遍历 */ + size = 0; + inOrder(root, &size); + printf("中序遍历的结点打印序列 = "); + PrintArray(arr, size); + + /* 后序遍历 */ + size = 0; + postOrder(root, &size); + printf("后序遍历的结点打印序列 = "); + PrintArray(arr, size); + + return 0; +} diff --git a/codes/go/chapter_tree/binary_search_tree_test.go b/codes/go/chapter_tree/binary_search_tree_test.go index 2109d5a44..2a864d138 100644 --- a/codes/go/chapter_tree/binary_search_tree_test.go +++ b/codes/go/chapter_tree/binary_search_tree_test.go @@ -20,7 +20,7 @@ func TestBinarySearchTree(t *testing.T) { fmt.Println("\n二叉树的根结点为:", node.Val) // 查找结点 - node = bst.Search(7) + node = bst.search(7) fmt.Println("查找到的结点对象为", node, ",结点值 =", node.Val) // 插入结点