äºåæ éå代ç
#include"iostream.h"
#include"stdlib.h"
#include"stdio.h"
#include<stack>
using namespace std;
#define NULL 0
#define OK 1
#define OVERFLOW -1
typedef int Status;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}*bitree;
int k=0;
int depth(bitree T)//æ çé«åº¦
{
if(!T)return 0;
else
{
int m=depth(T->lchild); int n=depth(T->rchild); return (m>n?m:n)+1;
}
}
//å
åºï¼ä¸åº 建æ
struct node *create(char *pre,char *ord,int n) {
struct node * T;
int m;
T=NULL;
if(n<=0)
{
return NULL;
}
else
{
m=0;
T=new(struct node);
T->data=*pre;
T->lchild=T->rchild=NULL; while(ord[m]!=*pre)
m++;
T->lchild=create(pre+1,ord,m);
T->rchild=create (pre+m+1,ord+m+1,n-m-1); return T;
}
}
//ä¸åºéå½éå
void inorder(struct node *T)
{
if(!T)
return;
else
{
inorder(T->lchild );
cout<<T->data;
inorder(T->rchild );
}
}
void inpre(struct node *T)
{
if(!T)
return;
else
{
cout<<T->data;
inpre(T->lchild );
inpre(T->rchild );
}
}
void postorder(struct node *T)
{
if(!T)
return;
else
{
postorder (T->lchild );
postorder (T->rchild );
cout<<T->data;
}
}
//å
åºééå½éå
void inpre1(struct node *T)
第2/4页
{
struct node *p;
struct node *stack[20];
int top=0;
p=T;
cout<<"ééå½å
åºä¸º:"<<endl;
while(p||top!=0)
{
while (p)
{
stack[top++]=p;
cout<<p->data;
p=p->lchild;
}
p=stack[--top];
p=p->rchild ;
}
}
//ä¸åºééå½éå
void inorder1(struct node *T)
{
struct node *p;
struct node *stack[20];
int top=0;
p=T;
cout<<"ééå½ä¸åºä¸º:"<<endl;
while(p||top!=0)
{
while (p)
{
stack[top++]=p;
p=p->lchild ;
}
p=stack[--top];
cout<<p->data;
p=p->rchild ;
}
}
//主å½æ°
int main()
{
bitree T;
char pre[30],ord[30];
第3/4页
int n,m;
cout<<"请è¾å
¥å
åºä¸º-+a*b%cd/efçäºåæ :"<<endl; gets(pre);
cout<<"请è¾å
¥ä¸åºä¸ºa+b*c%d-e/fçäºåæ :"<<endl; gets(ord);
n=strlen(pre);
T=create(pre,ord,n);
cout<< "ååºéå为:"<<endl;
postorder (T);
cout<<endl;
inpre1(T);
cout<<endl;
inorder1(T);
cout<<endl;
m=depth(T);
cout<<"äºåæ é«åº¦ä¸ºï¼"<<m<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考