树的建立 递归非递归遍历
来源:互联网 发布:青年网络公开课叶海林 编辑:程序博客网 时间:2024/05/16 03:05
//创建树
#include<iostream>
#include<malloc.h>
#define MAX 100
using namespace std;
typedef struct node
{
char data;
struct node *right,*left;
}btree;
void createTree(btree *&b,string s)
{
int top=-1;
btree *p,*stack[MAX];
int j=0;
int k;
b=NULL;
char c=s[j];
while(c!='/0')
{
switch(c)
{
case '(':top++;stack[top]=p;k=1;break;
case ',':k=2;break;
case ')':top--;break;
default:
p=(btree *)malloc(sizeof(btree));
p->data=c;
p->right=p->left=NULL;
if(b==NULL)
{
b=p;
}
else
{
switch(k)
{
case 1:stack[top]->left=p;break;
case 2:stack[top]->right=p;break;
}
}
}
j++;
c=s[j];
}
}
void printTree(btree *b)
{
if(b!=NULL)
{
cout<<b->data;
if(b->left!=NULL || b->right!=NULL)
{
cout<<"(";
printTree(b->left);
if(b->right!=NULL)
{
cout<<",";
printTree(b->right);
cout<<")";
}
}
}
}
void inorder_recursion(btree *b)
{
if(b!=NULL)
{
inorder_recursion(b->left);
cout<<b->data<<" ";
inorder_recursion(b->right);
}
}
void firsOrder(btree *b)
{
btree *stack[MAX],*p;
int top=-1;
if(b!=NULL)
{
top++;
stack[top]=b;
while(top>=0)
{
p=stack[top];
top--;
cout<<p->data<<" ";
if(p->right!=NULL)
{
top++;
stack[top]=p->right;
}
if(p->left!=NULL)
{
top++;
stack[top]=p->left;
}
}
}
}
void firsOrderS(btree *b)
{
if(b!=NULL)
{
cout<<b->data<<" ";
firsOrder(b->left);
firsOrder(b->right);
}
}
//我写的中序非递归遍历
void inorder(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag=0;
if(b!=NULL)
{
top++;
stack[top]=b;
while(top>=0)
{
p=stack[top];
while(p->left!=NULL && !flag)
{
top++;
p=p->left;
stack[top]=p;
}
flag=1;
p=stack[top];
top--;
cout<<p->data<<" ";
if(p->right!=NULL)
{
top++;
p=p->right;
stack[top]=p;
flag=0;
}
}
}
}
void psorder_recursion(btree *b)
{
if(b!=NULL)
{
psorder_recursion(b->left);
psorder_recursion(b->right);
cout<<b->data<<" ";
}
}
//这是我写的,不成功呀。
void psorder(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
top++;
stack[top]=b;
int flag=0;
while(top>=0)
{
p=stack[top];
while(p->left!=NULL && !flag)
{
top++;
p=p->left;
stack[top]=p;
}
if(flag==0)
{
p=stack[top];
top--;
cout<<p->data<<" ";
}
if(p->right!=NULL)
{
top++;
p=p->right;
stack[top]=p;
flag=0;
}
else
{
p=stack[top];
top--;
cout<<p->data<<" ";
flag=1;
}
}
}
/*
//写不出来了。
void psorder_b(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
top++;
stack[top]=b;
p=stack[top];
do{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
p=stack[top];
top--;
cout<<p->data<<" ";
if(b->right==p)
{
top++;
p=p->right;
stack[top]=p;
flag=1;
}
else
{
p=p->right;
flag=0;
}
}
}while(top!=-1);
}
}
*/
void psorder_b(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
do{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
b=stack[top];
if(b->right==p)//p为空或已被访问过。
{
cout<<b->data<<" ";
top--;
p=b;
}
else
{
b=b->right;
flag=0;
}
}
}while(top!=-1);
}
}
void psorder1(btree *b)
{
btree *p,*stack[MAX];
int top=-1;
int flag;
if(b!=NULL)
{
do
{
while(b)
{
top++;
stack[top]=b;
b=b->left;
}
p=NULL;
flag=1;
while(top!=-1 && flag)
{
b=stack[top];
if(b->right==p)
{
cout<<b->data<<" ";
top--;
flag=1;
p=b;
}
else
{
b=b->right;
flag=0;
}
}
}while(top!=-1);
}
}
int main()
{
btree *b;
createTree(b,"a(b(c),d(e(,f),g))");
printTree(b);
cout<<endl;
inorder_recursion(b);
cout<<endl;
firsOrderS(b);
cout<<endl;
firsOrder(b);
cout<<endl;
inorder(b);
cout<<endl;
psorder_recursion(b);
cout<<endl;
// psorder(b);
cout<<endl;
psorder_b(b);
cout<<endl;
psorder1(b);
cout<<endl;
}
- 树的建立 递归非递归遍历
- 二叉树建立、递归、非递归遍历
- 二叉树的建立和遍历(递归、非递归)
- c++二叉树的非递归建立和遍历
- 二叉树的建立及层次遍历及前中后序遍历递归与非递归的实现
- 二叉树的建立,前中后序遍历的递归版本和非递归版本,层序遍历
- JAVA二叉排序树的建立,遍历(递归,非递归)
- 二叉树的建立和递归遍历、非递归遍历操作
- 二叉树的建立 前序中序后序递归遍历及非递归遍历
- C++ 完全二叉树的建立、先序递归非递归遍历、层序遍历
- 递归非递归实现树的遍历
- 二叉树的递归,非递归遍历
- 二叉树的递归+非递归遍历
- 树的遍历----递归与非递归
- 树的递归和非递归遍历
- 二叉树的递归非递归遍历
- 二叉树的遍历--递归+非递归
- 二叉树的递归、非递归遍历
- 好累
- QTP中区分强制类型转换函数Cint()和Int()
- Dynamic Programming
- 【小阅读^大脑袋】0327 NO.345
- 1-Configuration Read
- 树的建立 递归非递归遍历
- 【时事观察】找寻城市发展的幸福之路
- 关于集合的性能问题
- 经典SQLSERVER语句
- 来自佛学的181条智慧, 心情不好的时候一定要看一下
- HTC Desire 机器出厂信息查询——看你的机器有没有问题
- fdfss
- tcp/ip(十二)
- 内存映射-IO空间-ioremap-iounremap