从上到下打印二叉树
Posted HelloCoding08
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从上到下打印二叉树相关的知识,希望对你有一定的参考价值。
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如,给定如下二叉树: root = [ 3, 5 , 1, 6, 2, 9, 8, null, null,7 , 4 ]
返回: [3, 5, 1, 6, 2, 9, 8, 7, 4]
相当于二叉树的层次遍历
//树的值的类型
typedef int TreeElem;
//树的结点类型
typedef struct tree_node TreeNode;
//队列结点类型
typedef struct queue_node QueueNode;
//队列元素类型
typedef struct tree_node* QueueElem;
typedef struct tree_node* Tree;
typedef struct queue_node* Queue;
struct tree_node {
TreeElem elem;
TreeNode* lchild;
TreeNode* rchild;
};
struct queue_node {
int front;
int rear;
QueueElem elem[MAXSIZE];
};
//创建队列
Queue create_queue () {
Queue queue = (Queue)malloc(sizeof(QueueNode));
queue->front = 0;
queue->rear = 0;
return queue;
}
//判队列是否为空
int is_empty (Queue queue) {
if (queue->front == queue->rear) {
return 1;
}
return 0;
}
//判队列是否为满
int is_full (Queue queue) {
if ((queue->rear + 1) % MAXSIZE == queue->front) {
return 1;
}
return 0;
}
//进队列
int en_queue (Queue queue, QueueElem elem) {
if (is_full(queue)) {
return 0;
}
queue->elem[queue->rear] = elem;
queue->rear = (queue->rear + 1) % MAXSIZE;
return 1;
}
//出队列
int de_queue (Queue queue, QueueElem* elem) {
if (is_empty(queue)) {
return 0;
}
*elem = queue->elem[queue->front];
queue->front = (queue->front + 1) % MAXSIZE;
return 1;
}
//得到队首的元素
int peek_queue (Queue queue, QueueElem* elem) {
if (is_empty(queue)) {
return 0;
}
*elem = queue->elem[queue->front];
return 1;
}
//队列的大小
int queue_size (Queue queue) {
if (is_empty(queue)) {
return 0;
}
int size = (queue->rear + MAXSIZE - queue->front) % MAXSIZE;
return size;
}
//清空队列
void clear_queue (Queue queue) {
queue->front = 0;
queue->rear = 0;
}
//创建树
Tree create_tree (FILE* fp) {
Tree tree;
TreeElem elem;
fscanf(fp, "%d", &elem);
if (0 == elem) {
return NULL;
}
else {
tree = (Tree)malloc(sizeof(TreeNode));
tree->elem = elem;
tree->lchild = create_tree(fp);
tree->rchild = create_tree(fp);
}
return tree;
}
int get_max(int a, int b) {
return a > b ? a : b;
}
//计算树的结点的个数
int count_node (Tree tree) {
if (tree == NULL) {
return 0;
}
//左孩子 + 右孩子 + 树根
int count = count_node(tree->lchild) + count_node(tree->rchild) + 1;
return count;
}
//树的层次遍历
int* level_order (Tree tree, int* size) {
//计算出结点的个数
int count = count_node(tree);
int* result = (int*)malloc(sizeof(int) * count);
//结果的索引
int index = 0;
//初始化数据,防止脏数据
for (int i = 0; i < count; i++) {
result[i] = -1;
}
//创建队列
Queue queue = create_queue();
//把根节点加入队列
en_queue(queue, tree);
//用于出队列元素
QueueElem de_elem;
while (!is_empty(queue)) {
//出队列
de_queue(queue, &de_elem);
//加入到结果数组中
result[index++] = de_elem->elem;
//左孩子不为空,把左孩子加入队列
if (de_elem->lchild != NULL) {
en_queue(queue, de_elem->lchild);
}
//同理
if (de_elem->rchild != NULL) {
en_queue(queue, de_elem->rchild);
}
}
//释放内存
free(queue);
//把result的长度带出去
*size = index;
return result;
}
void show (int* result, int size) {
for (int i = 0; i < size; i++) {
printf("%d\t", result[i]);
}
printf("\n");
}
int main () {
char path[] = "./demo.txt";
FILE* fp = fopen(path, "r");
if (fp == NULL) {
perror("fopen error");
exit(-1);
}
Tree tree = create_tree(fp);
fclose(fp);
int size = 0;
int* result = level_order(tree, &size);
show(result, size);
return 0;
}
demo.txt
以上是关于从上到下打印二叉树的主要内容,如果未能解决你的问题,请参考以下文章
剑指 Offer 32 - III. 从上到下打印二叉树 III(java解题)