| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 794 人关注过本帖
标题:二叉树遍历~好像错很多哦,帮忙改下的嘎~~急呢
取消只看楼主 加入收藏
蓿荻_X
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-6-29
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
二叉树遍历~好像错很多哦,帮忙改下的嘎~~急呢
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
#define NULL 0
#define MAXsize 100
typedef struct Xnode
{
    char data;
    struct Xnode *lchild,*rchild;
}BTree;
typedef struct stack
{
    BTree data[MAXsize];
    int tag[MAXsize];
    int top;
}seqstack;
void push(seqstack* s,BTree bt) //进栈
{
    s->data[++s->top]=bt;
}
BTree pop(seqstack* s) //出栈
{
    if(s->top!=-1)
    {
        s->top--;
        return(s->data[s->top+1]);
    }
}
BTree CreateBTree()
{
    BTree bt;
        char ch;
    if((ch=getchar())=='')
    bt=NULL;
    else
    {
        bt=(BTree*)malloc(sizeof(BTree));
            bt->data =ch;
            bt->lchild=CreateBTree();
            bt->rchild=CreateBTree();
        }
        return bt;
}
void Preorder(BTree *bt)
{
    if(bt!=NULL)
    {
        printf("%c",bt->data);
        Preorder(bt->lchild);
        Preorder(bt->rchild);
    }
}
void Preorder1(BTree *bt)
{
    BTree *St[MAXsize],*p;
    int top=1;
    if(bt!=NULL)
    {
        top++;
        St[top]=bt;
        while(top>-1)
        {
            p=St[top];
            top--;
            printf("%c",p->data );
            if(p->rchild !=NULL)
            {
                top++;
                St[top]=p->data ;
            }
            if(p->lchild !=NULL)
            {
                top++;
                St[top]=p->data ;
            }
        }
    }
}
void inorder(BTree *bt)
{
    if(bt!=NULL)
    {
        inorder(bt->lchild );
        printf("%c",bt->data );
        inorder(bt->rchild );
    }
}
void inorder1(BTree bt)
{
    BTree St[MAXsize],p;
    int top=1;
    if(bt!=NULL)
    {
        top++;
        St[top]=bt;
        while(top>-1)
        {
            if(p->rchild !=NULL)
            {
                top++;
                St[top]=p->data ;
            }
            p=St[top];
            top--;
            printf("%c",p->data );
            if(p->lchild !=NULL)
            {
                top++;
                St[top]=p->data ;
            }
        }
    }
}
void postorder(BTree *bt)
{
    if(bt!=NULL)
    {
        postorder(bt->lchild );
        postorder(bt->rchild );
        printf("%c",bt->data );
    }
}
void postorder1(BTree bt)
{
    BTree St[MAXsize];
    BTree p;
        p=bt;
    int flag,top=-1;
    do
    {
        while(bt)
        {
            top++;
            St[top]=bt;
            bt=bt->lchild ;
        }
        p=NULL;
        flag=1;
        while(top!=-1&&flag)
        {
            bt=St[top];
            if(bt->rchild ==p)
            {
                printf("%c",bt->data );
                top--;
                p=bt;
            }
            else
            {
                bt=bt->rchild ;
                flag=0;
            }
        }
    }while(top!=-1);
}
void translevel(BTree *bt)
{
    struct node
    {
        BTree *vec[MAXsize];
        int f,r;
    }Qu;
    Qu.f=0;
    Qu.r=0;
    if(bt!=NULL)
        printf("%c",bt->data );
    Qu.vec[Qu.r]=bt;
    Qu.r=Qu.r=1;
    while(Qu.f<Qu.r)
    {
        bt=Qu.vec[Qu.f];
        Qu.f=Qu.f=1;
        if(bt->lchild!=NULL)
        {
            printf("%c",bt->lchild->data);
            Qu.vec[Qu.r]=bt->lchild;
            Qu.r=Qu.r+1;
        }
        if(bt->rchild!=NULL)
        {
            printf("%c",bt->rchild->data);
            Qu.vec[Qu.r]=bt->rchild;
            Qu.r=Qu.r+1;
        }
    }
    printf("\n");
}
void main()
{    BTree bt;
    printf("请输入一棵二叉树:\n");
    bt=CreateBTree();
    printf("递归的先序遍历为:");
    Preorder(bt);
    printf("\n");
    printf("非递归的先序遍历为:");
    Preorder1(bt);
    printf("\n");
    printf("递归的中序遍历为:");
    inorder(bt);
    printf("\n");
    printf("非递归的中序遍历为:");
    inorder1(bt);
    printf("\n");
    printf("递归的后续遍历为:");
    postorder(bt);
    printf("\n");
    printf("递归的后续遍历为:");
    postorder1(bt);
    printf("\n");
    printf("层次序的非递归遍历为:");
    translevel(bt);
    printf("\n");
}
搜索更多相关主题的帖子: 遍历 二叉树 
2010-06-29 18:39
蓿荻_X
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-6-29
收藏
得分:0 
回复 2楼 xichong
谢谢你~~我的程序是不是没得救了啊?
2010-06-30 22:43
蓿荻_X
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-6-29
收藏
得分:0 
额。。。看来我还得多写是吧~~~?
2010-07-02 08:27
快速回复:二叉树遍历~好像错很多哦,帮忙改下的嘎~~急呢
数据加载中...
 
   



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

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