二叉树的遍历(递归方法)
来源:互联网 发布:sql数据库连接 编辑:程序博客网 时间:2024/05/16 18:43
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct np{
int date;
struct np *left,*right;
} node;
node *create(void)
{
return ((node*)malloc(sizeof(node)));
}
node *t(node *a,int d)/*建立二叉树*/
{
if (a==NULL) {
a=create();
a->left =a->right =NULL;
a->date=d;
}
else if (d>=a->date) {
a->right =t(a->right,d);
}
else if (d<a->date) {
a->left =t(a->left ,d);
}
return a;
}
void prt(node *r)
{
if(r) {
cout<<r->date<<" ";
prt(r->left );
prt(r->right );
}
}
void mid(node *r)
{
if(r)
{
mid(r->left);
cout<<r->date<<" ";
mid(r->right);
}
}
void beh(node *r)
{
if(r)
{
beh(r->left);
beh(r->right);
cout<<r->date<<" ";
}
}
int main(void)
{
node *bst=NULL;
int i;
cout<<"从键盘输入整数,以-24结束输入:";
while (scanf("%d",&i),i!=-24)
{
bst=t(bst,i); /*生成二叉排序数*/
}
cout<<"前序遍历:";
prt(bst);
cout<<endl;
cout<<"中序遍历:";
mid(bst);
cout<<endl;
cout<<"后序遍历:";
beh(bst);
cout<<endl;
system("pause");
return 0;
}
- 二叉树的遍历(递归方法)
- 遍历二叉树的全部方法(递归+非递归)
- 二叉树的非递归遍历方法
- 非递归的方法遍历二叉树
- 非递归遍历二叉树的方法
- 二叉树的非递归遍历方法
- 二叉树的遍历(递归遍历)
- 二叉树的三种遍历方法(递归)
- 层次遍历二叉树的方法(递归,队列,指针)
- 二叉树的遍历方法(递归,非递归)
- 二叉树递归与非递归遍历的方法
- 二叉树的遍历 -- 递归和非递归方法
- 二叉树的递归和非递归遍历方法
- java二叉树的遍历,递归与非递归方法
- 二叉树的递归和非递归遍历方法
- 二叉树的遍历(递归+非递归+层次遍历)
- 二叉树遍历的应用(递归!递归!递归!)
- 【算法设计-二叉树遍历】二叉树的递归与非递归遍历方法
- TCP实现P2P通信、TCP穿越NAT的方法、TCP打洞(转载)
- POJ 4052 金华邀请赛I题
- mongodb 副本集+分片
- mongodb 应用
- Java多线程之同步与死锁
- 二叉树的遍历(递归方法)
- iOS: About app icon and splash image
- mongodb 系统 副本集
- JAVA6新特性
- Android获取屏幕宽高要注意的问题
- 数据读写SD卡下半部分析
- cassandra
- 《Composing UIs》学习笔记
- 东莞惠州楼盘又来深抢客 推盘量居历史高位 2012.5.12