2023-01-11 00:48:48 +08:00
|
|
|
/**
|
|
|
|
* File: tree_node.h
|
|
|
|
* Created Time: 2023-01-09
|
|
|
|
* Author: Reanon (793584285@qq.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TREE_NODE_H
|
|
|
|
#define TREE_NODE_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 14:31:23 +08:00
|
|
|
#include <limits.h>
|
|
|
|
|
2023-01-11 00:48:48 +08:00
|
|
|
#define MAX_NODE_SIZE 5000
|
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
/* 二叉树节点结构体 */
|
2023-01-11 00:48:48 +08:00
|
|
|
struct TreeNode {
|
2023-04-18 20:21:31 +08:00
|
|
|
int val; // 节点值
|
|
|
|
int height; // 节点高度
|
|
|
|
struct TreeNode *left; // 左子节点指针
|
|
|
|
struct TreeNode *right; // 右子节点指针
|
2023-01-11 00:48:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct TreeNode TreeNode;
|
|
|
|
|
2023-01-12 00:31:11 +08:00
|
|
|
TreeNode *newTreeNode(int val) {
|
2023-01-11 00:48:48 +08:00
|
|
|
TreeNode *node;
|
|
|
|
|
2023-04-17 21:13:15 +08:00
|
|
|
node = (TreeNode *)malloc(sizeof(TreeNode));
|
2023-01-11 00:48:48 +08:00
|
|
|
node->val = val;
|
|
|
|
node->height = 0;
|
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Generate a binary tree with an array */
|
2023-01-12 00:31:11 +08:00
|
|
|
TreeNode *arrToTree(const int *arr, size_t size) {
|
2023-01-11 00:48:48 +08:00
|
|
|
if (size <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int front, rear, index;
|
|
|
|
TreeNode *root, *node;
|
|
|
|
TreeNode **queue;
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 根节点 */
|
2023-01-12 00:31:11 +08:00
|
|
|
root = newTreeNode(arr[0]);
|
2023-01-11 00:48:48 +08:00
|
|
|
/* 辅助队列 */
|
2023-04-17 21:13:15 +08:00
|
|
|
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
2023-01-11 00:48:48 +08:00
|
|
|
// 队列指针
|
|
|
|
front = 0, rear = 0;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 将根节点放入队尾
|
2023-01-11 00:48:48 +08:00
|
|
|
queue[rear++] = root;
|
|
|
|
// 记录遍历数组的索引
|
|
|
|
index = 0;
|
|
|
|
while (front < rear) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 取队列中的头节点,并让头节点出队
|
2023-01-11 00:48:48 +08:00
|
|
|
node = queue[front++];
|
|
|
|
index++;
|
|
|
|
if (index < size) {
|
2023-04-18 20:21:31 +08:00
|
|
|
// represent null with INT_MAX
|
2023-04-18 14:31:23 +08:00
|
|
|
if (arr[index] != INT_MAX) {
|
2023-01-12 00:31:11 +08:00
|
|
|
node->left = newTreeNode(arr[index]);
|
2023-01-11 00:48:48 +08:00
|
|
|
queue[rear++] = node->left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
if (index < size) {
|
2023-04-18 14:31:23 +08:00
|
|
|
if (arr[index] != INT_MAX) {
|
2023-01-12 00:31:11 +08:00
|
|
|
node->right = newTreeNode(arr[index]);
|
2023-01-11 00:48:48 +08:00
|
|
|
queue[rear++] = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-04-17 22:15:06 +08:00
|
|
|
/* Generate a binary tree with an array */
|
2023-01-12 00:31:11 +08:00
|
|
|
int *treeToArr(TreeNode *root) {
|
2023-01-11 00:48:48 +08:00
|
|
|
if (root == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
int front, rear;
|
|
|
|
int index, *arr;
|
|
|
|
TreeNode *node;
|
|
|
|
TreeNode **queue;
|
|
|
|
/* 辅助队列 */
|
2023-04-17 21:13:15 +08:00
|
|
|
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
2023-01-11 00:48:48 +08:00
|
|
|
// 队列指针
|
|
|
|
front = 0, rear = 0;
|
2023-04-09 04:32:17 +08:00
|
|
|
// 将根节点放入队尾
|
2023-01-11 00:48:48 +08:00
|
|
|
queue[rear++] = root;
|
|
|
|
/* 辅助数组 */
|
2023-04-17 21:13:15 +08:00
|
|
|
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
2023-01-11 00:48:48 +08:00
|
|
|
// 数组指针
|
|
|
|
index = 0;
|
|
|
|
while (front < rear) {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 取队列中的头节点,并让头节点出队
|
2023-01-11 00:48:48 +08:00
|
|
|
node = queue[front++];
|
|
|
|
if (node != NULL) {
|
|
|
|
arr[index] = node->val;
|
|
|
|
queue[rear++] = node->left;
|
|
|
|
queue[rear++] = node->right;
|
|
|
|
} else {
|
2023-04-18 14:31:23 +08:00
|
|
|
arr[index] = INT_MAX;
|
2023-01-11 00:48:48 +08:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2023-04-15 23:51:35 +08:00
|
|
|
/* Free the memory allocated to a tree */
|
|
|
|
void freeMemoryTree(TreeNode *root) {
|
|
|
|
if (root == NULL)
|
|
|
|
return;
|
|
|
|
freeMemoryTree(root->left);
|
|
|
|
freeMemoryTree(root->right);
|
|
|
|
// 释放内存
|
|
|
|
free(root);
|
|
|
|
}
|
2023-01-11 00:48:48 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // TREE_NODE_H
|