您的位置:首页 > 其它

一种二叉树层序遍历实现

2017-12-21 16:34 375 查看
今天终于有时间尝试自己实现二叉树层序遍历,因为层序遍历需要借用一个队列,所以不像前序遍历、中序遍历和后序遍历(直接递归即可)那样容易实现,但是经过一番折腾后,居然成功了,呵呵,为防止自己遗忘,故写下此博客。

编程思路如下:

1、分别建立一颗二叉树和一个队列

2、先访问根节点

3、再判断是否有左右儿子节点,若存在则将其入队(先左儿子再右儿子)

4、出队一次

5、以出队节点指针为新根节点指针,递归调用2-4,直至新根节点指针为NULL

#include <stdio.h>
#include <stdlib.h>

typedef struct BTNode *BTptr;

typedef struct BTNode {
char data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTree;

struct WorkQueue {
struct BTNode *data;
struct WorkQueue *next;
};
//--------------------------------------
//globe var
//--------------------------------------
struct WorkQueue *head,*tail;
struct WorkQueue *wq = NULL;

//-----------------------------------------------------
//workqueue implement
//-----------------------------------------------------
static void create_workqueue(struct WorkQueue **q)
{
if(NULL == *q)
{
*q = malloc(sizeof(struct WorkQueue));
if(NULL == *q)
{
printf("Error: out of memory!!\n");
return;
}
head = tail = NULL;
(*q)->next = NULL;
}
return;
}

static int queue(struct WorkQueue *q,struct BTNode *c)
{
struct WorkQueue *tmp = NULL;
if(head == NULL)
{
tmp = malloc(sizeof(struct WorkQueue));
if(tmp == NULL)
{
printf("Error: out of memory!!\n");
return -1;
}
head = tail = tmp;
tmp->next = NULL;
tmp->data = c;
}
else
{
tmp = malloc(sizeof(struct WorkQueue));
if(tmp == NULL)
{
printf("Error: out of memory!!\n");
return -1;
}
tail->next = tmp;
tail = tmp;
tmp->data = c;
tmp->next = NULL;
}
return 0;
}

static struct BTNode *dequeue(struct WorkQueue *q)
{
struct BTNode *c;
if(head == NULL)
{
return NULL;
}
if(head == tail)
{
c = head->data;
head = tail = NULL;
}
else {
c = head->data;
head = head->next;
}
return c;
}
//-----------------------------------------------------
//Binary tree implement
//-----------------------------------------------------
void create_tree(BTptr *T)
{
char c;
scanf("%c",&c);
if('#' == c)
{
*T = NULL;
return;
}

*T = (BTptr)malloc(sizeof(BTree));
if(*T == NULL)
{
printf("Error: no memory to alloc in system!!\n");
return;
}
(*T)->data = c;
create_tree(&((*T)->lchild));
create_tree(&((*T)->rchild));
}

void print_preorder(BTptr T)
{
if(T == NULL)
return;
printf("\t%c\t",T->data);
print_preorder(T->lchild);
print_preorder(T->rchild);
}

void print_inorder(BTptr T)
{
if(T == NULL)
return;
print_inorder(T->lchild);
printf("\t%c\t",T->data);
print_inorder(T->rchild);
}

void print_postorder(BTptr T)
{
if(T == NULL)
return;
print_postorder(T->lchild);
print_postorder(T->rchild);
printf("\t%c\t",T->data);
}

int print_layerorder(BTptr T)
{
if(T == NULL)
return 0;
printf("\t%c\t",T->data);
if(T->lchild != NULL)
queue(wq,T->lchild);
if(T->rchild != NULL)
queue(wq,T->rchild);
return print_layerorder(dequeue(wq));
}

int main(void)
{
BTptr T;
create_tree(&T);
printf("当前树的前序遍历为:");
print_preorder(T);
printf("\n");
//printf("当前树的中序遍历为:");
//print_inorder(T);
//printf("\n");
//printf("当前树的后序遍历为:");
//print_postorder(T);
//printf("\n");

create_workqueue(&wq);
printf("当前树的层序遍历为:");
print_layerorder(T);
printf("\n");
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐