二叉树的创建和遍历

来源:互联网 发布:mac脚本编辑器 用途 编辑:程序博客网 时间:2024/06/05 21:53

先按先序的顺序输入节点,空的节点以 # 代替,然后以先序遍历,中序遍历,后序遍历,层次遍历的顺序输出。

其中层次遍历中,采用队列的方式缓存节点,如果队列已满则中断遍历,此时输出的结果会不完整,队列大小根据宏 LEN_QUEUE确定。


/********************************************************************************* *      Copyright:  (C) 2015 zq979999<zq979999@outlook.com> *                  All rights reserved. * *       Filename:  tree.c *    Description:  This file  *                  *        Version:  1.0.0(2015年09月13日) *         Author:  zq979999 <zq979999@outlook.com> *      ChangeLog:  1, Release initial version on "2015年09月13日 15时16分08秒" *                  ********************************************************************************/#include <stdio.h>#include <stdlib.h>typedef struct _tree{    char val;    struct _tree *left;    struct _tree *right;}tree;tree *tree_creat(){    char ch;    tree *newtree = NULL;//= malloc(sizeof(tree));    ch = getchar();     if(ch != '#')    {        newtree = malloc(sizeof(tree));        newtree->val = ch;        newtree->left = tree_creat();        newtree->right = tree_creat();    }    return newtree;}void pre_print(tree *mytree){    if(mytree)    {        printf("%c", mytree->val);        pre_print(mytree->left);        pre_print(mytree->right);    }}void in_print(tree *mytree){     if(mytree)    {        in_print(mytree->left);        printf("%c", mytree->val);        in_print(mytree->right);    }}void post_print(tree* mytree){    if(mytree)    {        post_print(mytree->left);        post_print(mytree->right);        printf("%c", mytree->val);    }}#define LEN_QUEUE 20int  layer_print(tree *mytree){    tree queue[LEN_QUEUE];        int head = 0;    int tail = 0;    int i;        queue[tail] = *mytree;    tail = (++tail) % LEN_QUEUE;    while(head != tail)    {        printf("%c", queue[head].val);        if(queue[head].left != NULL)        {            queue[tail] = *(queue[head].left);            tail = (++tail) % LEN_QUEUE;#if 0            if((head - tail) == 1 )            {                printf("queue is full.error!\n");                return -1;            }#endif        }        if(queue[head].right != NULL)        {            queue[tail] = *(queue[head].right);            tail = (++tail) % LEN_QUEUE;#if 0            if((head - tail) == 1)            {                printf("queue is full.error!\n");                return -1;            }#endif        }       head = (++head) % LEN_QUEUE;    }    }int main(void){    tree *mytree;        printf("please input a series char\n");    mytree = tree_creat();#if 1        pre_print(mytree);    printf("\n");    in_print(mytree);    printf("\n");    post_print(mytree);    printf("\n");#endif    layer_print(mytree);    printf("\n");}

2015/9/14   更改队列为链表的形式,这样就能动态决定队列的长度,层次输出改进为每输出一层则换行

在源代码中添加且更改layer_print()函数:

typedef struct _queue{    tree *node;    struct _queue *next;}queue;queue *queue_creat(){    queue *head = malloc(sizeof(queue));    head->next = NULL;    head->node = NULL;    return head;}queue *queue_out(queue *head){    if(head == NULL)    {        printf("the queue is empty!\n");        return NULL;    }    printf("%c", head->node->val);    free(head);    head = head->next;    return head;}queue *queue_in(queue *tail, tree *node){    if(tail == NULL)        return;    tail->node = node;    tail->next = malloc(sizeof(queue));    tail = tail->next;    tail->next = NULL;    return tail;}int  layer_print(tree *node){    queue *head = NULL;    queue *tail = NULL;    int child,parent;    if(node == NULL)    {        return -1;    }    tail = head = queue_creat();    tail = queue_in(tail, node);#if 1    child = 0;    parent = 1;#endif    while(head != tail)    {        if(head->node->left != NULL)        {                tail = queue_in(tail, head->node->left);            child++;        }        if(head->node->right != NULL)        {               tail = queue_in(tail, head->node->right);            child++;        }        head = queue_out(head);        parent--;        if(parent == 0)        {            parent = child;            child = 0;            printf("\n");        }    }    free(tail);    return 0;}


0 0
原创粉丝点击