无图版
» 您尚未
登录
注册
|
搜索
|
朋友圈
|
帮助
社区服务
银行
朋友圈
搜索工具
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训
»
数据结构和算法
»
树的建立及三种遍历;
交 易
投 票
本页主题:
树的建立及三种遍历;
打印
|
加为IE收藏
|
收藏主题
|
上一主题
|
下一主题
phpwhy
总管
级别:
管理员
精华:
3
发帖:
633
威望:
550 点
金钱:
5560 PYMB
贡献值:
0 点
在线时间:12(小时)
注册时间:2005-09-15
最后登录:2008-06-09
树的建立及三种遍历;
#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-06-09
#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培训
|- 培训咨询和报名
>> 学员专区
|- 学员交流区
>> 开源项目
|- 项目开发
|- OSC技术交流
|- CMS开源项目研究
|- 下载
>> PHP和web开发
|- 入门和基础知识
|- PHP中高级
|- 网站SEO
|- PHP资源共享
|- YUI,ext JS
|- 数据库应用
|- 操作系统(Windows、Linux)
|- 英语学习
|- 数据结构和算法
|- web2.0
>> 信息发布
|- 招聘和求职
>> 娱乐论坛
|- BT软件&教程发布
|- BT影视&音乐
|- 娱乐乐园
>> PHP世纪
|- 站务管理
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训
»
数据结构和算法
今日推荐
隐藏
显示
关闭
现在时间:07-04 07:08
Copyright © 2006 phpwhy.com 版权所有
浙ICP备05060669号
关于我们 -
合作联系