Update arrayToTree() and treeToArray() for C.

pull/895/head
krahets 1 year ago
parent 5b1a219b8b
commit 0e10274bb4

@ -19,13 +19,13 @@ void testListNode() {
void testTreeNode() { void testTreeNode() {
int nums[] = {1, 2, 3, INT_MAX, 5, 6, INT_MAX}; int nums[] = {1, 2, 3, INT_MAX, 5, 6, INT_MAX};
int size = sizeof(nums) / sizeof(int); int size = sizeof(nums) / sizeof(int);
TreeNode *root = arrToTree(nums, size); TreeNode *root = arrayToTree(nums, size);
// print tree // print tree
printTree(root); printTree(root);
// tree to arr // tree to arr
int *arr = treeToArr(root); int *arr = treeToArray(root, size);
printArray(arr, size); printArray(arr, size);
} }

@ -26,7 +26,7 @@ ListNode *newListNode(int val) {
return node; return node;
} }
/* Generate a linked list with a vector */ /* Generate a linked list with an array */
ListNode *arrToLinkedList(const int *arr, size_t size) { ListNode *arrToLinkedList(const int *arr, size_t size) {
if (size <= 0) { if (size <= 0) {
return NULL; return NULL;

@ -1,7 +1,7 @@
/** /**
* File: print_util.h * File: print_util.h
* Created Time: 2022-12-21 * Created Time: 2022-12-21
* Author: MolDum (moldum@163.com)Reanon (793584285@qq.com) * Author: MolDum (moldum@163.com), Reanon (793584285@qq.com)
*/ */
#ifndef PRINT_UTIL_H #ifndef PRINT_UTIL_H
@ -133,7 +133,7 @@ static void printHeap(int arr[], int size) {
printf("堆的数组表示:"); printf("堆的数组表示:");
printArray(arr, size); printArray(arr, size);
printf("堆的树状表示:\n"); printf("堆的树状表示:\n");
root = arrToTree(arr, size); root = arrayToTree(arr, size);
printTree(root); printTree(root);
} }

@ -23,6 +23,7 @@ typedef struct TreeNode {
struct TreeNode *right; // 右子节点指针 struct TreeNode *right; // 右子节点指针
} TreeNode; } TreeNode;
/* 构造函数 */
TreeNode *newTreeNode(int val) { TreeNode *newTreeNode(int val) {
TreeNode *node; TreeNode *node;
@ -34,93 +35,71 @@ TreeNode *newTreeNode(int val) {
return node; return node;
} }
/* Generate a binary tree with an array */ // 序列化编码规则请参考:
TreeNode *arrToTree(const int *arr, size_t size) { // https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
if (size <= 0) { // 二叉树的数组表示:
return NULL; // [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
} // 二叉树的链表表示:
// /——— 15
int front, rear, index; // /——— 7
TreeNode *root, *node; // /——— 3
TreeNode **queue; // | \——— 6
// | \——— 12
// ——— 1
// \——— 2
// | /——— 9
// \——— 4
// \——— 8
/* 根节点 */ /* 将列表反序列化为二叉树:递归 */
root = newTreeNode(arr[0]); TreeNode *arrayToTreeDFS(int *arr, int size, int i) {
/* 辅助队列 */ if (i < 0 || i >= size || arr[i] == INT_MAX) {
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE); return NULL;
// 队列指针
front = 0, rear = 0;
// 将根节点放入队尾
queue[rear++] = root;
// 记录遍历数组的索引
index = 0;
while (front < rear) {
// 取队列中的头节点,并让头节点出队
node = queue[front++];
index++;
if (index < size) {
// represent null with INT_MAX
if (arr[index] != INT_MAX) {
node->left = newTreeNode(arr[index]);
queue[rear++] = node->left;
}
}
index++;
if (index < size) {
if (arr[index] != INT_MAX) {
node->right = newTreeNode(arr[index]);
queue[rear++] = node->right;
}
}
} }
// 释放辅助队列空间 TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
free(queue); root->val = arr[i];
root->left = arrayToTreeDFS(arr, size, 2 * i + 1);
root->right = arrayToTreeDFS(arr, size, 2 * i + 2);
return root; return root;
} }
/* Generate a binary tree with an array */ /* 将列表反序列化为二叉树 */
int *treeToArr(TreeNode *root) { TreeNode *arrayToTree(int *arr, int size) {
return arrayToTreeDFS(arr, size, 0);
}
/* 将二叉树序列化为列表:递归 */
void treeToArrayDFS(TreeNode *root, int i, int *res, int *size) {
if (root == NULL) { if (root == NULL) {
return NULL; return;
}
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++];
if (node != NULL) {
arr[index] = node->val;
queue[rear++] = node->left;
queue[rear++] = node->right;
} else {
arr[index] = INT_MAX;
} }
index++; while (i >= *size) {
res = realloc(res, (*size + 1) * sizeof(int));
res[*size] = INT_MAX;
(*size)++;
} }
return arr; res[i] = root->val;
treeToArrayDFS(root->left, 2 * i + 1, res, size);
treeToArrayDFS(root->right, 2 * i + 2, res, size);
}
/* 将二叉树序列化为列表 */
int *treeToArray(TreeNode *root, int *size) {
*size = 0;
int *res = NULL;
treeToArrayDFS(root, 0, res, size);
return res;
} }
/* Free the memory allocated to a tree */ /* 释放二叉树内存 */
void freeMemoryTree(TreeNode *root) { void freeMemoryTree(TreeNode *root) {
if (root == NULL) if (root == NULL)
return; return;
freeMemoryTree(root->left); freeMemoryTree(root->left);
freeMemoryTree(root->right); freeMemoryTree(root->right);
// 释放内存
free(root); free(root);
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save