| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 413 人关注过本帖
标题:哪个大哥帮下我!```
收藏  订阅  推荐  打印 
daibenlong
Rank: 1
等级:新手上路
帖子:19
积分:298
注册:2008-5-15
哪个大哥帮下我!```

二叉树的遍历
对任意给定的二叉树(顶点数自定)建立它的二叉链表存贮结构,并利用栈的五种基本运算(置空栈、进栈、出栈、取栈顶元素、判栈空)实现二叉树的先序、中序、后序三种遍历,输出三种遍历的结果。


用TC编写   我不会编程啊         要交课程设计     一个星期后就要交       93803021@163.com   本人邮箱  每次发的贴没一个人帮我解决  希望这次有个好心的帮我下忙
搜索更多相关主题的帖子: 二叉树  遍历  课程  链表  
2008-6-6 10:04
Loli
Rank: 3Rank: 3
来自:飞燕算法群46520219
等级:中级会员
帖子:348
积分:3862
注册:2008-5-27

作业真多



[color=white]

论坛http://yzfy.org 我的QQ号1007665007
Loli算法讨论QQ群: 3996098(每周有教学课) 46520219 57909089
2008-6-6 10:13
wangyinshiwo
Rank: 2
等级:注册会员
帖子:72
积分:1146
注册:2007-11-9

/* 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");
}

抽刀断水水更流,举杯消愁愁更愁。
2008-6-6 22:59
now
Rank: 4
来自:广州
等级:高级会员
帖子:544
积分:5872
注册:2007-11-9

LZ很强;
LS也很强;

GIS
Geographic Information System
你在哪里?——》你的坐标?
2008-6-7 22:44
计算机的流氓
Rank: 2
等级:注册会员
帖子:92
积分:1036
注册:2008-6-6

我倒。佩服,佩服。
2008-6-16 19:58
lingluoz
Rank: 4
来自:苏州科技学院
等级:高级会员
威望:3
帖子:623
积分:6680
注册:2008-2-2

以前这个论坛里面有位仁兄发个贴子说。。有偿帮做作业。被我小鄙视了一下。。现在看看。我应该佩服他一下才对。。

MAY 1, 1964 -- The Birth of BASIC
Over 40 years later, it still enables ANYONE to write their own programs.
P.S. 革命尚未成功 同志仍需努力
2008-6-16 20:03
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.103625 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved