PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 数据结构和算法 » 树的建立及三种遍历;
本页主题: 树的建立及三种遍历; 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

phpwhy

头衔:总管 总管
该用户目前不在线
级别: 管理员
精华: 3
发帖: 633
威望: 550 点
金钱: 5560 PYMB
贡献值: 0 点
在线时间:12(小时)
注册时间:2005-09-15
最后登录:2008-10-07

树的建立及三种遍历;


#include  <iostream.h>
typedef  struct  tree{ 
                    int data;
        struct tree *lchild,*rchild;
                          }TREE;
TREE *createtree();
void  preorder(TREE *p);
void  inorder(TREE *p);
void  postorder(TREE *p);
void  main()
{

    TREE *node;
    node=new TREE;
    cout<<"按前序顺序开始建树 "<<endl<<"依次输入新建树的相关节点值:"<<endl;
    node=createtree();
    cout<<"前序遍历结果为:  "<<endl;
    preorder(node);
    cout<<"中序遍历结果为:  "<<endl;
    inorder(node);
    cout<<"后序遍历结果为:  "<<endl;
    postorder(node);
return;
}
//////////////建树
TREE *createtree()
{
int x;
TREE *p;
cin>>x;
if(x==0)  p=NULL;
else
{
  p=new TREE;
  p->data=x;
  p->lchild=createtree();
  p->rchild=createtree();
}
return  p;
}
///////////先序遍历
void preorder (TREE *p)
{
if(p!=NULL)
{
  cout<<p->data<<"  ";
  preorder(p->lchild);
  preorder(p->rchild);
}
return;
}
//////////中序遍历
void inorder (TREE *p)
{
if(p!=NULL)
{
  inorder(p->lchild);
        cout<<p->data<<"  ";
  inorder(p->rchild);
}
return;
}
/////////////后序遍历
void postorder (TREE *p)
{
if(p!=NULL)
{
  postorder(p->lchild);
  postorder(p->rchild);
        cout<<p->data<<"  ";
}
return;
}
你适合当程序员吗?给想学编程的朋友
http://www.phpwhy.com/read.php?tid=5258&page=1&toread=1

  远程免费试听http://www.phpwhy.com/bbs/read.php?tid=4514
学校照片见 http://www.phpwhy.com/bbs/read.php?tid=4091


PHP培训,网站建设咨询
联系电话: 0571-85980046 ,0571-86704910
联系人:何老师
qq:310172
地址:杭州下沙4号路物美西子阳光星城1座501室智达电脑培训中心
顶端 Posted: 2007-06-01 20:20 | [楼 主]
phpwhy

头衔:总管 总管
该用户目前不在线
级别: 管理员
精华: 3
发帖: 633
威望: 550 点
金钱: 5560 PYMB
贡献值: 0 点
在线时间:12(小时)
注册时间:2005-09-15
最后登录:2008-10-07


#include <stdlib.h>
#include <stdio.h>

#define MAX_STACK_SIZE  ( 500 )

struct bintree;
typedef struct bintree* BinTree;
struct bintree
{
        char data;
        BinTree left;
        BinTree right;
};

void Preorder( Bintree T )  //先序遍历
{
        typedef BinTree StackElem;
        StackElem  stack[MAX_STACK_SIZE];
        StackElem* top = stack;

        while ( stack != top || NULL != T )
        {
                while ( NULL != T )
                {
                        *top = T->right;
                        ++top;
                        printf( "%c", T->data );
                        T = T->left;
                }
                --top;
                T = *top;
        }
        return;
}

void Inorder( Bintree T )  //中序遍历
{
        typedef BinTree StackElem;
        StackElem  stack[MAX_STACK_SIZE];
        StackElem* top = stack;

        while ( stack != top || NULL != T )
        {
                while ( NULL != T )
                {
                        *top = T;
                        ++top;
                        T = T->left;
                }
                --top;
                T = *top;
                printf( "%c", T->data );
                T = T->right;
        }
        return;
}

void Post( BinTree T )  //后序遍历
{
        BinTree p = NULL;
        typedef BinTree StackElem;
        StackElem  stack[MAX_STACK_SIZE];
        StackElem* top = stack;

        while ( ( stack != top ) || ( T != NULL ) )
        {
                while ( T != NULL )
                {
                        *++top = T;
                        T = T->left;
                }
                T = *top;
                if ( p == T->right || NULL == T->right )
                {
                        printf( "%c", T->data );
                        p = T;
                        --top;
                        T = NULL; /* get T from the top of stack by next while */
                }
                else
                {
                        T = T->right;
                }
        }
        return;
}
你适合当程序员吗?给想学编程的朋友
http://www.phpwhy.com/read.php?tid=5258&page=1&toread=1

  远程免费试听http://www.phpwhy.com/bbs/read.php?tid=4514
学校照片见 http://www.phpwhy.com/bbs/read.php?tid=4091


PHP培训,网站建设咨询
联系电话: 0571-85980046 ,0571-86704910
联系人:何老师
qq:310172
地址:杭州下沙4号路物美西子阳光星城1座501室智达电脑培训中心
顶端 Posted: 2007-06-01 20:21 | 1 楼
seoyou



该用户目前不在线
级别: 新手上路
精华: 0
发帖: 2
威望: 3 点
金钱: 20 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2007-07-19
最后登录:2007-07-20


先序,中序,后序
顶端 Posted: 2007-07-19 18:57 | 2 楼
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » 数据结构和算法

现在时间:10-14 06:04 Copyright © 2006 phpwhy.com 版权所有
浙ICP备05060669号

点击这里给我发消息关于我们 - 合作联系