2023-08-03 14:48:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: array_binary_tree.c
|
|
|
|
|
* Created Time: 2023-07-29
|
|
|
|
|
* Author: Gonglja (glj0@outlook.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../utils/common.h"
|
|
|
|
|
|
2023-10-29 00:09:54 +08:00
|
|
|
|
/* 数组表示下的二叉树结构体 */
|
2023-10-18 02:16:26 +08:00
|
|
|
|
typedef struct {
|
2023-10-27 23:26:48 +08:00
|
|
|
|
int *tree;
|
|
|
|
|
int size;
|
2023-10-18 02:16:26 +08:00
|
|
|
|
} ArrayBinaryTree;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
|
2023-10-29 00:09:54 +08:00
|
|
|
|
/* 构造函数 */
|
2023-11-01 05:14:22 +08:00
|
|
|
|
ArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {
|
2023-10-27 23:26:48 +08:00
|
|
|
|
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
|
|
|
|
|
abt->tree = malloc(sizeof(int) * arrSize);
|
|
|
|
|
memcpy(abt->tree, arr, sizeof(int) * arrSize);
|
|
|
|
|
abt->size = arrSize;
|
|
|
|
|
return abt;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 05:14:22 +08:00
|
|
|
|
/* 析构函数 */
|
|
|
|
|
void delArrayBinaryTree(ArrayBinaryTree *abt) {
|
|
|
|
|
free(abt->tree);
|
|
|
|
|
free(abt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 14:48:10 +08:00
|
|
|
|
/* 节点数量 */
|
2023-10-18 02:16:26 +08:00
|
|
|
|
int size(ArrayBinaryTree *abt) {
|
2023-10-27 23:26:48 +08:00
|
|
|
|
return abt->size;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 i 节点的值 */
|
2023-10-18 02:16:26 +08:00
|
|
|
|
int val(ArrayBinaryTree *abt, int i) {
|
2023-08-03 14:48:10 +08:00
|
|
|
|
// 若索引越界,则返回 INT_MAX ,代表空位
|
|
|
|
|
if (i < 0 || i >= size(abt))
|
|
|
|
|
return INT_MAX;
|
2023-10-27 23:26:48 +08:00
|
|
|
|
return abt->tree[i];
|
2023-08-03 14:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取索引为 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-27 23:26:48 +08:00
|
|
|
|
/* 层序遍历 */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 14:48:10 +08:00
|
|
|
|
/* 深度优先遍历 */
|
2023-10-27 23:26:48 +08:00
|
|
|
|
void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
|
2023-08-03 14:48:10 +08:00
|
|
|
|
// 若为空位,则返回
|
|
|
|
|
if (val(abt, i) == INT_MAX)
|
|
|
|
|
return;
|
|
|
|
|
// 前序遍历
|
2023-10-27 23:26:48 +08:00
|
|
|
|
if (strcmp(order, "pre") == 0)
|
|
|
|
|
res[(*index)++] = val(abt, i);
|
|
|
|
|
dfs(abt, left(i), order, res, index);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
// 中序遍历
|
2023-10-27 23:26:48 +08:00
|
|
|
|
if (strcmp(order, "in") == 0)
|
|
|
|
|
res[(*index)++] = val(abt, i);
|
|
|
|
|
dfs(abt, right(i), order, res, index);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
// 后序遍历
|
2023-10-27 23:26:48 +08:00
|
|
|
|
if (strcmp(order, "post") == 0)
|
|
|
|
|
res[(*index)++] = val(abt, i);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 前序遍历 */
|
2023-10-27 23:26:48 +08:00
|
|
|
|
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;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 中序遍历 */
|
2023-10-27 23:26:48 +08:00
|
|
|
|
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;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 后序遍历 */
|
2023-10-27 23:26:48 +08:00
|
|
|
|
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;
|
2023-08-03 14:48:10 +08:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
int main() {
|
|
|
|
|
// 初始化二叉树
|
2023-10-27 23:26:48 +08:00
|
|
|
|
// 使用 INT_MAX 代表空位 NULL
|
2023-08-03 14:48:10 +08:00
|
|
|
|
int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
2023-10-27 23:26:48 +08:00
|
|
|
|
int arrSize = sizeof(arr) / sizeof(arr[0]);
|
|
|
|
|
TreeNode *root = arrayToTree(arr, arrSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("\n初始化二叉树\n");
|
|
|
|
|
printf("二叉树的数组表示:\n");
|
2023-10-27 23:26:48 +08:00
|
|
|
|
printArray(arr, arrSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("二叉树的链表表示:\n");
|
|
|
|
|
printTree(root);
|
|
|
|
|
|
2023-11-01 05:14:22 +08:00
|
|
|
|
ArrayBinaryTree *abt = newArrayBinaryTree(arr, arrSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
|
|
|
|
|
// 访问节点
|
|
|
|
|
int i = 1;
|
|
|
|
|
int l = left(i), r = right(i), p = parent(i);
|
2023-10-27 23:26:48 +08:00
|
|
|
|
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);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
|
|
|
|
|
// 遍历树
|
2023-10-27 23:26:48 +08:00
|
|
|
|
int returnSize;
|
|
|
|
|
int *res;
|
|
|
|
|
|
|
|
|
|
res = levelOrder(abt, &returnSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("\n层序遍历为: ");
|
2023-10-27 23:26:48 +08:00
|
|
|
|
printArray(res, returnSize);
|
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
|
|
res = preOrder(abt, &returnSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("前序遍历为: ");
|
2023-10-27 23:26:48 +08:00
|
|
|
|
printArray(res, returnSize);
|
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
|
|
res = inOrder(abt, &returnSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("中序遍历为: ");
|
2023-10-27 23:26:48 +08:00
|
|
|
|
printArray(res, returnSize);
|
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
|
|
res = postOrder(abt, &returnSize);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
printf("后序遍历为: ");
|
2023-10-27 23:26:48 +08:00
|
|
|
|
printArray(res, returnSize);
|
|
|
|
|
free(res);
|
|
|
|
|
|
|
|
|
|
// 释放内存
|
2023-11-01 05:14:22 +08:00
|
|
|
|
delArrayBinaryTree(abt);
|
2023-08-03 14:48:10 +08:00
|
|
|
|
return 0;
|
2023-10-27 23:26:48 +08:00
|
|
|
|
}
|