| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 349 人关注过本帖
标题:二叉树的层次遍历
只看楼主 加入收藏
若为初见
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
二叉树的层次遍历
急!!!
我的下面的程序只有层次遍历有问题
请大家帮帮忙吧……
急!!!
明天就要交啊!!!
谢谢!
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT   10
#define QUEUE_LENGTH 100

struct BiTree
{
    char data;
    struct BiTree *lchild;
    struct BiTree *rchild;
};
typedef struct BiTree BiTree;

struct SqStack
{
    BiTree *base;
    BiTree *top;
    int stacksize;
};
typedef struct SqStack SqStack;

struct Queue
{
    BiTree *data[QUEUE_LENGTH+1];
    int front;
    int rear;
};
typedef struct Queue Queue;

BiTree *CreateBiTree();
char Menu();
void PreOrderTraverse_Re(BiTree *p0);
void InOrderTraverse_Re(BiTree *p0);
void PostOrderTraverse_Re(BiTree *p0);
SqStack *InitStack();
void Push(SqStack *S,BiTree e);
int StackEmpty(SqStack *S);
BiTree Pop(SqStack *S);
void PreOrder(BiTree *p0);
void InOrder(BiTree *p0);
void PostOrder(BiTree *p0);
void InitQueue(Queue &q);
void EnQueue(Queue &q,BiTree *x);
int EmptyQueue(Queue &q);
int DeQueue(Queue &q,BiTree &p);
void LevelOrd(BiTree *r);

void main()
{
    char ch;
    BiTree *Tree;
    while(1)
    {
        ch=Menu();
        switch(ch)
        {
            case'1':
            {
                Tree=CreateBiTree();
                printf("The binary tree has been created!\n");
                break;
            }
            case'2':
            {
                PreOrderTraverse_Re(Tree);
                printf("That is the result by Preord with recursion!\n");
                break;
            }
            case'3':
            {
                InOrderTraverse_Re(Tree);
                printf("That is the result by Inord with recursion!\n");
                break;
            }
            case'4':
            {
                PostOrderTraverse_Re(Tree);
                printf("That is the result by Postord with recursion!\n");
                break;
            }
            case'5':
            {
                PreOrder(Tree);
                printf("That is the result by Preord without recursion!\n");
                break;
            }
            case'6':
            {
                InOrder(Tree);
                printf("That is the result by Inord without recursion!\n");
                break;
            }
            case'7':
            {
                PostOrder(Tree);
                printf("That is the result by Postord without recursion!\n");
                break;
            }
            case'8':
            {
                LevelOrd(Tree);
                printf("That is the result by Levelord!\n");
                break;
            }
            default:exit(0);
        }
    }
}

char Menu()
{
    char ch;
    printf("\nThe operation of traverse BiTree!\n");
    printf("1.Creat binary tree\n");
    printf("2.Traverse binary tree by Preord with recursion\n");
    printf("3.Traverse binary tree by Inord with recursion\n");
    printf("4.Traverse binary tree by Postord with recursion\n");
    printf("5.Traverse binary tree by Preord without recursion\n");
    printf("6.Traverse binary tree by Inord without recursion\n");
    printf("7.Traverse binary tree by Postord without recursion\n");
    printf("8.Traverse binary tree by Levelord\n");
    printf("9.Destory binary tree\n");
    printf("Please input your option:");
    scanf(" %c",&ch);
    getchar();
    return ch;
}

BiTree *CreateBiTree()
{
    char ch;
    BiTree *T;
    scanf("%c",&ch);   
    if(ch==' ')
        T=NULL;
    else
    {
        T=(BiTree *)malloc(sizeof(BiTree));
        T->data=ch;
        T->lchild=CreateBiTree();
        T->rchild=CreateBiTree();
    }
    return (T);
}

void PreOrderTraverse_Re(BiTree *p0)
{
    if(p0)
    {
        printf("%c\n",p0->data);
        PreOrderTraverse_Re(p0->lchild);
        PreOrderTraverse_Re(p0->rchild);
    }
}

void InOrderTraverse_Re(BiTree *p0)
{
    if(p0)
    {
        InOrderTraverse_Re(p0->lchild);
        printf("%c\n",p0->data);
        InOrderTraverse_Re(p0->rchild);
    }
}

void PostOrderTraverse_Re(BiTree *p0)
{
    if(p0)
    {
        PostOrderTraverse_Re(p0->lchild);
        PostOrderTraverse_Re(p0->rchild);
        printf("%c\n",p0->data);
    }
}

SqStack *InitStack()
{
    SqStack *S;
    S=(SqStack *)malloc(sizeof(SqStack));
    S->base=(BiTree*)malloc(STACK_INIT_SIZE*sizeof(BiTree));
    S->top=S->base;
    S->stacksize=STACK_INIT_SIZE;
    return S;
}

void Push(SqStack *S,BiTree e)
{
    if(S->top-S->base>=S->stacksize)
    {
        S->base=(BiTree*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(BiTree));
        S->top=S->base+S->stacksize;
        S->stacksize+=STACKINCREMENT;
    }
    *(S->top)=e;
    S->top++;
}

int StackEmpty(SqStack *S)
{
    if(S->top==S->base)
        return 1;
    else
        return 0;
}

BiTree Pop(SqStack *S)
{
    S->top --;
    return *S->top;

}

void PreOrder(BiTree *p0)
{
    SqStack *S;
    BiTree *p=p0;
    S=InitStack();
    if(p0)
        Push(S,*p);
    while(!StackEmpty(S))
    {
        p=(BiTree *)malloc(sizeof(BiTree));
        *p=Pop(S);
        printf("%c\n",p->data);
        if(p->rchild)
            Push(S,*p->rchild);
        if(p->lchild)
            Push(S,*p->lchild);
    }
}


void InOrder(BiTree *p0)
{
    SqStack *S;
    BiTree *p=p0;
    S=InitStack();
    while(p||!StackEmpty(S))
    {
        if(p)
        {
            Push(S,*p);
            p=p->lchild;
        }
        else
        {
            p=(BiTree *)malloc(sizeof(BiTree));
            *p=Pop(S);
            printf("%c\n",p->data);
            p=p->rchild;
        }
    }
}


void PostOrder(BiTree *p0)
{
    SqStack *S;
    BiTree p;
    BiTree *l,*r;
    S=InitStack();
    Push(S,*p0);
    while(!StackEmpty(S))
    {
        p=Pop(S);
        l=p.lchild;
        r=p.rchild;
        if(l==NULL&&r==NULL)
            printf("%c\n", p.data);
        else
        {
            p.lchild=NULL;
            p.rchild=NULL;
            Push(S,p);
            if(r!=NULL)
                Push(S,*r);
            if(l!=NULL)
                Push(S,*l);
        }
     }
}

void InitQueue(Queue &q)
{
    q.front=0;
    q.rear=0;
}

void EnQueue(Queue &q,BiTree *x)
{
    if(q.rear==QUEUE_LENGTH)
        printf("overflow");
    q.rear++;
    q.data[q.rear]=x;
}

int EmptyQueue(Queue &q)
{
    if(q.rear==q.front)
        return 1;
    else
        return 0;
}

int DeQueue(Queue &q,BiTree *p)
{
    if(EmptyQueue(q))
    {
        printf("underflow");
        return 0;
    }
    else
    {
        p=q.data[q.front+1];// printf("%c",x);
        q.front++;
    }
    if(EmptyQueue(q))
    {
        q.front=0;
        q.rear=0;
    }
    return 1;
}

void LevelOrd(BiTree *r)
{
    Queue q;
    InitQueue(q);
    BiTree *p,*p1,*p2;
    p=r;//(Tree)malloc(sizeof(TreeNode));
    if(p)
        EnQueue(q,p);
    while(!EmptyQueue(q))
    {
        DeQueue(q,p);
        printf("%c\n",p->data);
        p1=p->lchild;
        p2=p->rchild;
        if(p1)
            EnQueue(q,p1);
        if(p2)
            EnQueue(q,p2);
    }
}
搜索更多相关主题的帖子: 二叉树 
2011-05-05 23:48
若为初见
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-4-6
收藏
得分:0 
其实我就是想问我的这个层次遍历为什么输出的一直都是根结点的元素的值……要怎样修改?
谢谢大家了!!!
2011-05-07 01:39
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:20 
路过,。。。

[ 本帖最后由 BlueGuy 于 2011-5-7 08:58 编辑 ]

我就是真命天子,顺我者生,逆我者死!
2011-05-07 08:50
快速回复:二叉树的层次遍历
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020199 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved