当前位置: 代码迷 >> C语言 >> 哪个大哥帮下我!```
  详细解决方案

哪个大哥帮下我!```

热度:385   发布时间:2008-06-06 10:04:06.0
哪个大哥帮下我!```
二叉树的遍历
对任意给定的二叉树(顶点数自定)建立它的二叉链表存贮结构,并利用栈的五种基本运算(置空栈、进栈、出栈、取栈顶元素、判栈空)实现二叉树的先序、中序、后序三种遍历,输出三种遍历的结果。


用TC编写   我不会编程啊         要交课程设计     一个星期后就要交       93803021@163.com   本人邮箱  每次发的贴没一个人帮我解决  希望这次有个好心的帮我下忙
搜索更多相关的解决方案: 二叉树  遍历  课程  链表  

----------------解决方案--------------------------------------------------------
作业真多



[color=white]
----------------解决方案--------------------------------------------------------
/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
    char ch;
    struct bitnode *lchild,*rchild;
}bitnode,*bittree;
typedef struct stacknode
{
    bitnode *data;
    struct stacknode *next;
}linkstack;
void initstack(linkstack *s)
{ s->next=NULL; }
void push(linkstack *s,bittree x)
{
    linkstack *p;
    p=(linkstack *)malloc(sizeof(linkstack));
    p->data=x;
    p->next=s->next;
    s->next=p;
}
bittree pop(linkstack *s)
{
    bittree x;
    linkstack *p;
    p=s->next;
    if(s->next==0)return 0;
    x=p->data;
    s->next=p->next;
    free(p);
    return x;
}
int emptystack(linkstack *s)
{
    if(s->next)return 1;
    else return 0;
}
bitnode *gettop(linkstack *s)
{
    bittree x;
    if(s->next)
    x=s->next->data;
    return x;
}
bittree cre_tree()
{
    bittree root;
    char ch;
    ch=getchar();
    if(ch=='#')root=NULL;
    else
    {
        root=(bittree)malloc(sizeof(bitnode));
        root->ch=ch;
        root->lchild=cre_tree();
        root->rchild=cre_tree();
    }
    return root;
}
void preorder(bittree root)
{
    linkstack s;
    bittree p;
    initstack(&s);
    push(&s,root);
    while(emptystack(&s))
    {
        p=pop(&s);
        while(p)
        {
            printf("%c",p->ch);
            if(p->rchild)push(&s,p->rchild);
            p=p->lchild;
        }
    }
}
void inorder(bittree root)
{
    bittree p;
    linkstack s;
    initstack(&s);
    p=root;
    while(p||emptystack(&s))   //注意还有右子树也要判断
    {
        while(p)
        {
            push(&s,p);
            p=p->lchild;
        }
        p=pop(&s);
        printf("%c",p->ch);
        p=p->rchild;
    }
}
void postorder(bittree root)
{
    bittree p,q;
    linkstack s;
    initstack(&s);
    p=root;q=NULL;
    while(p||emptystack(&s))
    {
        if(p!=q)       //但p等于root时停止入栈
        {
            while(p)
            {
                push(&s,p);
                if(p->lchild)p=p->lchild;        //若q的右指针为空或指向刚刚访问过的结点
                else p=p->rchild;
            }
        }
        if(!emptystack(&s))break;
        q=gettop(&s);
        if(q->rchild==p)
        {
            p=pop(&s);
            printf("%c",p->ch);
        }
        else p=q->rchild;
    }
}
void main()
{
    bittree root;
    root=cre_tree();
    preorder(root);printf("\n\n");
    inorder(root);printf("\n\n");
    postorder(root);printf("\n\n");
}
----------------解决方案--------------------------------------------------------
LZ很强;
LS也很强;
----------------解决方案--------------------------------------------------------
我倒。佩服,佩服。
----------------解决方案--------------------------------------------------------
以前这个论坛里面有位仁兄发个贴子说。。有偿帮做作业。被我小鄙视了一下。。现在看看。我应该佩服他一下才对。。
----------------解决方案--------------------------------------------------------
  相关解决方案