mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 18:37:48 +08:00 
			
		
		
		
	 5f7385c8a3
			
		
	
	5f7385c8a3
	
	
	
		
			
			* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
		
			
				
	
	
		
			167 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			167 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /**
 | ||
|  * File: array_binary_tree.c
 | ||
|  * Created Time: 2023-07-29
 | ||
|  * Author: Gonglja (glj0@outlook.com)
 | ||
|  */
 | ||
| 
 | ||
| #include "../utils/common.h"
 | ||
| 
 | ||
| /* 陣列表示下的二元樹結構體 */
 | ||
| typedef struct {
 | ||
|     int *tree;
 | ||
|     int size;
 | ||
| } ArrayBinaryTree;
 | ||
| 
 | ||
| /* 建構子 */
 | ||
| ArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {
 | ||
|     ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
 | ||
|     abt->tree = malloc(sizeof(int) * arrSize);
 | ||
|     memcpy(abt->tree, arr, sizeof(int) * arrSize);
 | ||
|     abt->size = arrSize;
 | ||
|     return abt;
 | ||
| }
 | ||
| 
 | ||
| /* 析構函式 */
 | ||
| void delArrayBinaryTree(ArrayBinaryTree *abt) {
 | ||
|     free(abt->tree);
 | ||
|     free(abt);
 | ||
| }
 | ||
| 
 | ||
| /* 串列容量 */
 | ||
| int size(ArrayBinaryTree *abt) {
 | ||
|     return abt->size;
 | ||
| }
 | ||
| 
 | ||
| /* 獲取索引為 i 節點的值 */
 | ||
| int val(ArrayBinaryTree *abt, int i) {
 | ||
|     // 若索引越界,則返回 INT_MAX ,代表空位
 | ||
|     if (i < 0 || i >= size(abt))
 | ||
|         return INT_MAX;
 | ||
|     return abt->tree[i];
 | ||
| }
 | ||
| 
 | ||
| /* 獲取索引為 i 節點的左子節點的索引 */
 | ||
| int left(int i) {
 | ||
|     return 2 * i + 1;
 | ||
| }
 | ||
| 
 | ||
| /* 獲取索引為 i 節點的右子節點的索引 */
 | ||
| int right(int i) {
 | ||
|     return 2 * i + 2;
 | ||
| }
 | ||
| 
 | ||
| /* 獲取索引為 i 節點的父節點的索引 */
 | ||
| int parent(int i) {
 | ||
|     return (i - 1) / 2;
 | ||
| }
 | ||
| 
 | ||
| /* 層序走訪 */
 | ||
| int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
 | ||
|     int *res = (int *)malloc(sizeof(int) * size(abt));
 | ||
|     int index = 0;
 | ||
|     // 直接走訪陣列
 | ||
|     for (int i = 0; i < size(abt); i++) {
 | ||
|         if (val(abt, i) != INT_MAX)
 | ||
|             res[index++] = val(abt, i);
 | ||
|     }
 | ||
|     *returnSize = index;
 | ||
|     return res;
 | ||
| }
 | ||
| 
 | ||
| /* 深度優先走訪 */
 | ||
| void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
 | ||
|     // 若為空位,則返回
 | ||
|     if (val(abt, i) == INT_MAX)
 | ||
|         return;
 | ||
|     // 前序走訪
 | ||
|     if (strcmp(order, "pre") == 0)
 | ||
|         res[(*index)++] = val(abt, i);
 | ||
|     dfs(abt, left(i), order, res, index);
 | ||
|     // 中序走訪
 | ||
|     if (strcmp(order, "in") == 0)
 | ||
|         res[(*index)++] = val(abt, i);
 | ||
|     dfs(abt, right(i), order, res, index);
 | ||
|     // 後序走訪
 | ||
|     if (strcmp(order, "post") == 0)
 | ||
|         res[(*index)++] = val(abt, i);
 | ||
| }
 | ||
| 
 | ||
| /* 前序走訪 */
 | ||
| int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
 | ||
|     int *res = (int *)malloc(sizeof(int) * size(abt));
 | ||
|     int index = 0;
 | ||
|     dfs(abt, 0, "pre", res, &index);
 | ||
|     *returnSize = index;
 | ||
|     return res;
 | ||
| }
 | ||
| 
 | ||
| /* 中序走訪 */
 | ||
| int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
 | ||
|     int *res = (int *)malloc(sizeof(int) * size(abt));
 | ||
|     int index = 0;
 | ||
|     dfs(abt, 0, "in", res, &index);
 | ||
|     *returnSize = index;
 | ||
|     return res;
 | ||
| }
 | ||
| 
 | ||
| /* 後序走訪 */
 | ||
| int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
 | ||
|     int *res = (int *)malloc(sizeof(int) * size(abt));
 | ||
|     int index = 0;
 | ||
|     dfs(abt, 0, "post", res, &index);
 | ||
|     *returnSize = index;
 | ||
|     return res;
 | ||
| }
 | ||
| 
 | ||
| /* Driver Code */
 | ||
| int main() {
 | ||
|     // 初始化二元樹
 | ||
|     // 使用 INT_MAX 代表空位 NULL
 | ||
|     int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
 | ||
|     int arrSize = sizeof(arr) / sizeof(arr[0]);
 | ||
|     TreeNode *root = arrayToTree(arr, arrSize);
 | ||
|     printf("\n初始化二元樹\n");
 | ||
|     printf("二元樹的陣列表示:\n");
 | ||
|     printArray(arr, arrSize);
 | ||
|     printf("二元樹的鏈結串列表示:\n");
 | ||
|     printTree(root);
 | ||
| 
 | ||
|     ArrayBinaryTree *abt = newArrayBinaryTree(arr, arrSize);
 | ||
| 
 | ||
|     // 訪問節點
 | ||
|     int i = 1;
 | ||
|     int l = left(i), r = right(i), p = parent(i);
 | ||
|     printf("\n當前節點的索引為 %d,值為 %d\n", i, val(abt, i));
 | ||
|     printf("其左子節點的索引為 %d,值為 %d\n", l, l < arrSize ? val(abt, l) : INT_MAX);
 | ||
|     printf("其右子節點的索引為 %d,值為 %d\n", r, r < arrSize ? val(abt, r) : INT_MAX);
 | ||
|     printf("其父節點的索引為 %d,值為 %d\n", p, p < arrSize ? val(abt, p) : INT_MAX);
 | ||
| 
 | ||
|     // 走訪樹
 | ||
|     int returnSize;
 | ||
|     int *res;
 | ||
| 
 | ||
|     res = levelOrder(abt, &returnSize);
 | ||
|     printf("\n層序走訪為: ");
 | ||
|     printArray(res, returnSize);
 | ||
|     free(res);
 | ||
| 
 | ||
|     res = preOrder(abt, &returnSize);
 | ||
|     printf("前序走訪為: ");
 | ||
|     printArray(res, returnSize);
 | ||
|     free(res);
 | ||
| 
 | ||
|     res = inOrder(abt, &returnSize);
 | ||
|     printf("中序走訪為: ");
 | ||
|     printArray(res, returnSize);
 | ||
|     free(res);
 | ||
| 
 | ||
|     res = postOrder(abt, &returnSize);
 | ||
|     printf("後序走訪為: ");
 | ||
|     printArray(res, returnSize);
 | ||
|     free(res);
 | ||
| 
 | ||
|     // 釋放記憶體
 | ||
|     delArrayBinaryTree(abt);
 | ||
|     return 0;
 | ||
| }
 |