数据结构中怎样根据中序先序后序画出树
答案:4 悬赏:70 手机版
解决时间 2021-02-09 10:35
- 提问者网友:浩歌待明月
- 2021-02-09 00:37
数据结构中怎样根据中序先序后序画出树
最佳答案
- 五星知识达人网友:毛毛
- 2021-02-09 01:39
#include<iostream>
using namespace std;
#include<malloc.h>
#include<stdio.h>
#include<math.h>
#define maxsize 20 //最大结点个数
//#define N 14 //必须输入结点个数(包含虚结点)
#define M 10 //最大深度
typedef struct node{
char data;
int m; //结点的深度
struct node*lchild,*rchild;
}Bitree;
Bitree*Q[maxsize];
Bitree*creatree()
{
char ch;
int front,rear;
// int i=1;
Bitree *T,*s;
T=NULL;
front=1;
rear=0;
cout<<"请输入数据"<<endl;
cin>>ch;
while(ch!='#')
{
// cin>>ch;
s=NULL;
if(ch!='@')
{
s=(Bitree*)malloc(sizeof(Bitree));
s->data =ch;
s->lchild =s->rchild =NULL;
}
rear++;
Q[rear]=s;
if(rear==1)
{
T=s;
T->m=1; //父结点深度为一
}
else{
if(s!=NULL&&Q[front]!=NULL)
if(rear%2==0)
{
Q[front]->lchild =s;
Q[front]->lchild ->m =Q[front]->m+1;
}
else
{
Q[front]->rchild =s;
Q[front]->rchild ->m =Q[front]->m+1;
}
if(rear%2==1)
front++;
}
//i++;
cin>>ch;
}
return T;
}
int countleaf(Bitree* T)
{
if(T==NULL)
return (0);
else if((T->lchild==NULL)&&(T->rchild==NULL))
return (1);
else
return (countleaf(T->lchild)+countleaf(T->rchild));
}
int treedepth(Bitree *T)
{
if(T==NULL)
return (0);
else
{
if(treedepth(T->lchild )>treedepth(T->rchild ))
return(treedepth(T->lchild )+1);
else
return (treedepth(T->rchild )+1);
}
}
void output(Bitree*T) //输出打印二叉数
{
int i;
if(T!=NULL)
{
output(T->rchild ); //右根左遍历二叉数,结果从上到下显示
for(i=1;i<=M;i++)
{
if(i!=T->m)
cout<<" ";
else
cout<<T->data ;
}
cout<<endl;
//cout<<T->data ;
output(T->lchild );
}
}
int menu_select( )
{
int sn;
printf(" 打印二叉树问题\n");
printf("==================\n");
printf(" 1 二叉树的建立\n");
printf(" 2 打印二叉树\n");
printf(" 3 求二叉树叶子结点个数\n");
printf(" 4 求二叉树的深度\n");
printf(" 0 退出系统\n");
printf("==================\n");
printf(" 请 选 择0-4:\n");
for( ; ; )
{
scanf( "%d", &sn);
if( sn <0||sn>4)
printf("\n\t输入错误,重选0-4:\n");
else
break;
}
return sn;
}
int main( )
{
Bitree*T;
for(; ;)
{
switch(menu_select())
{
case 1: T=creatree();
printf("\n");
break;
case 2: cout<<"打印结果:"<<endl;
output(T);
printf("\n");
break;
case 3: int i;
i=countleaf(T);
cout<<"所求二叉树叶子结点为"<<i;
cout<<endl;
break;
case 4: int j;
j=treedepth(T);
cout<<"所求二叉树深度为"<<j;
cout<<endl;
break;
case 0:printf("再见");
exit(0);
break;
}
}
return 0;
}
using namespace std;
#include<malloc.h>
#include<stdio.h>
#include<math.h>
#define maxsize 20 //最大结点个数
//#define N 14 //必须输入结点个数(包含虚结点)
#define M 10 //最大深度
typedef struct node{
char data;
int m; //结点的深度
struct node*lchild,*rchild;
}Bitree;
Bitree*Q[maxsize];
Bitree*creatree()
{
char ch;
int front,rear;
// int i=1;
Bitree *T,*s;
T=NULL;
front=1;
rear=0;
cout<<"请输入数据"<<endl;
cin>>ch;
while(ch!='#')
{
// cin>>ch;
s=NULL;
if(ch!='@')
{
s=(Bitree*)malloc(sizeof(Bitree));
s->data =ch;
s->lchild =s->rchild =NULL;
}
rear++;
Q[rear]=s;
if(rear==1)
{
T=s;
T->m=1; //父结点深度为一
}
else{
if(s!=NULL&&Q[front]!=NULL)
if(rear%2==0)
{
Q[front]->lchild =s;
Q[front]->lchild ->m =Q[front]->m+1;
}
else
{
Q[front]->rchild =s;
Q[front]->rchild ->m =Q[front]->m+1;
}
if(rear%2==1)
front++;
}
//i++;
cin>>ch;
}
return T;
}
int countleaf(Bitree* T)
{
if(T==NULL)
return (0);
else if((T->lchild==NULL)&&(T->rchild==NULL))
return (1);
else
return (countleaf(T->lchild)+countleaf(T->rchild));
}
int treedepth(Bitree *T)
{
if(T==NULL)
return (0);
else
{
if(treedepth(T->lchild )>treedepth(T->rchild ))
return(treedepth(T->lchild )+1);
else
return (treedepth(T->rchild )+1);
}
}
void output(Bitree*T) //输出打印二叉数
{
int i;
if(T!=NULL)
{
output(T->rchild ); //右根左遍历二叉数,结果从上到下显示
for(i=1;i<=M;i++)
{
if(i!=T->m)
cout<<" ";
else
cout<<T->data ;
}
cout<<endl;
//cout<<T->data ;
output(T->lchild );
}
}
int menu_select( )
{
int sn;
printf(" 打印二叉树问题\n");
printf("==================\n");
printf(" 1 二叉树的建立\n");
printf(" 2 打印二叉树\n");
printf(" 3 求二叉树叶子结点个数\n");
printf(" 4 求二叉树的深度\n");
printf(" 0 退出系统\n");
printf("==================\n");
printf(" 请 选 择0-4:\n");
for( ; ; )
{
scanf( "%d", &sn);
if( sn <0||sn>4)
printf("\n\t输入错误,重选0-4:\n");
else
break;
}
return sn;
}
int main( )
{
Bitree*T;
for(; ;)
{
switch(menu_select())
{
case 1: T=creatree();
printf("\n");
break;
case 2: cout<<"打印结果:"<<endl;
output(T);
printf("\n");
break;
case 3: int i;
i=countleaf(T);
cout<<"所求二叉树叶子结点为"<<i;
cout<<endl;
break;
case 4: int j;
j=treedepth(T);
cout<<"所求二叉树深度为"<<j;
cout<<endl;
break;
case 0:printf("再见");
exit(0);
break;
}
}
return 0;
}
全部回答
- 1楼网友:白昼之月
- 2021-02-09 04:05
先序遍历中第一个元素为根,根据此根把中序序列分为左右子树,确定左右子树中包含的元素后再分别在先序序列中确定左右子树的树根,依次找出左右子树的树根。。。
(先序中序可以,后序中序也可以,必须要有中序哟~)
不知道说的够清楚吗
- 2楼网友:动情书生
- 2021-02-09 03:22
#include
using namespace std;
class node
{
public:
node(){ this->plchild = this->prchild = null; }
char data;
node * plchild;
node * prchild;
};
node * createtree()
{
node * root = new node;
root->data = 'a';
node * pb = new node;
pb->data = 'b';
node * pc = new node;
pc->data = 'c';
node * pd = new node;
pd->data = 'd';
node * pe = new node;
pe->data = 'e';
root->plchild = pb;
root->prchild = pc;
pc->plchild = pd;
pd->prchild = pe;
return root;
}
void intraversetree(node * pt) //中序遍历
{
if (null == pt)
return;
else
{
intraversetree(pt->plchild);
cout << pt->data;
intraversetree(pt->prchild);
}
}
void posttraversetree(node * pt) //后序遍历
{
if (null == pt)
return;
else
{
posttraversetree(pt->plchild);
posttraversetree(pt->prchild);
cout << pt->data;
}
}
void pretraversetree(node * pt) //先序遍历
{
if (null==pt)
{
return;
}
else
{
cout << pt->data;
pretraversetree(pt->plchild);
pretraversetree(pt->prchild);
}
}
void destroytree(node * pt) //销毁二叉树
{
if (null == pt)
return;
else
{
destroytree(pt->plchild);
destroytree(pt->prchild);
delete pt;
}
}
int main()
{
node * pt = createtree();
cout << "先序遍历";
pretraversetree(pt);
destroytree(pt);
// pretraversetree(pt);
//cout << endl;
system("pause");
return 0;
}
用c++写的 不过跟c没什么区别
- 3楼网友:北方的南先生
- 2021-02-09 01:49
在先序遍历序列中找到第一个元素作为当前树的树根,这个元素的下一个元素只能是它的左孩子或者右孩子,那么在中序序列中如果这第二个元素出现在根元素的左边就是左孩子,否则是右孩子,重复这个过程可以处理所有元素
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯