#include<iostream>
#include<cmath>
using namespace std;
static int i;
static int ju;
static int ab;
typedef struct ErChanShuLianBiao{
char data;
ErChanShuLianBiao *lc;
ErChanShuLianBiao *rc;
int ceng;
int juli;
}Biao,*Biaoptr;
Biaoptr recurrence(Biaoptr prev,Biaoptr head,Biaoptr current){
current=new Biao;
cin>>current->data;
if(current->data =='\n')
return head;
if(current->data!='*'&&i==1){
if(head==NULL){
prev=current;
head=prev;
}
else{ prev->lc=current;
prev=prev->lc ;}
recurrence(prev,head,current);
recurrence(prev,head,current);}
else if(current->data !='*'&&i==2){
prev->rc=current;
prev=prev->rc;i=1;
recurrence(prev,head,current);
recurrence(prev,head,current);
}
if(current->data=='*'&&i==1){
i=i+1;
prev->lc=NULL;
}
else if(current->data=='*'&&i==2){
prev->rc =NULL;
}
}
void cengshu (Biaoptr head,int level){
if(ab<level)
ab=level;
if(head!=NULL){
head->ceng=level;head->juli=0;
cengshu(head->lc ,level+1);
cengshu(head->rc ,level+1);}
}
void weijuli(Biaoptr head,int high){
if(head!=NULL){
if(head->ceng ==high)
{head->juli =ju;
ju=ju+4;}
else
{weijuli(head->lc ,high);
weijuli(head->rc ,high);}
}}
void qitajuli(Biaoptr head){
if(head!=NULL){
if(head->juli ==0)
{if(head->lc ->juli ==0&&head->rc ->juli!=0)
qitajuli(head->lc );
if(head->lc ->juli !=0&&head->rc ->juli==0)
qitajuli(head->rc);
if(head->lc ->juli ==0&&head->rc ->juli==0)
{qitajuli(head->lc);
qitajuli(head->rc);}
if(head->lc ->juli !=0&&head->rc ->juli !=0)
head->juli =(head->lc ->juli +head->rc ->juli)/2;
}
}}
void cengxu(Biaoptr head,Biao abc[],int abd){
if(head!=NULL)
{abc[abd].lc=new Biao;
abc[abd].rc=new Biao;
abc[abd]=*head;
cengxu(head->lc ,abc,2*abd);
cengxu(head->rc,abc,2*abd+1);
}
}
void print1(Biao abc[],int high){
int ac=1,wei=1,kai=1,ceng=1,cha=0;
for(ceng=1;ceng<=high;ceng++){
cha=0;
for(ac=high;ac>1;ac--)
wei=wei*2;
for(kai=1;kai<=50;kai++)
if(abc[kai].ceng ==ceng){
for(ac=cha+1;ac<=(wei+high-1+high*3);ac++)
{
if(ac==abc[kai].juli&&ceng==abc[kai].ceng )
{if(abc[kai].data =='*')
{cout<<" ";cha=ac;ac=(wei+high-1+high*3);}
else{cout<<abc[kai].data;cha=ac;ac=(wei+high-1+high*3);}}
else if(ac>cha&&ac!=abc[kai].juli&&ceng==abc[kai].ceng)
cout<<" ";}}
cout<<endl;
cha=0;
for(kai=1;kai<=50;kai++)
if(abc[kai].ceng==ceng){
if(abc[kai].lc!=NULL)
for(ac=cha+1;ac<=(wei+high-1+high*3);ac++)
{if((ac==abc[kai].lc->juli+1 )&&abc[kai].lc->data!='*')
{cout<<'/' ;cha=ac;ac=(wei+high-1+high*3);}
else if((ac==abc[kai].lc->juli+1 )&&abc[kai].lc->data=='*')
{cout<<" " ;cha=ac;ac=(wei+high-1+high*3);}
else
cout<<" ";
}//
if(abc[kai].rc!=NULL)
for(ac=cha+1;ac<=(wei+high-1+high*3);ac++)
{if((ac==abc[kai].rc->juli-1 )&&abc[kai].rc->data!='*')
{cout<<"\\" ;cha=ac;ac=(wei+high-1+high*3);}
else if((ac==abc[kai].rc->juli-1 )&&abc[kai].rc->data=='*')
{cout<<" " ;cha=ac;ac=(wei+high-1+high*3);}
else
cout<<" ";
}
}
cout<<endl;
}
}
void print2(Biao abc[]){
int we=1;
for(we=1;we<=50;we++)
if(abc[we].data!='*')
cout<<we<<" "<<abc[we].data<<"\t"<<abc[we].ceng<<"\t"<<abc[we].juli<<"\n"; }
void buquan( int high,Biaoptr head2 ){
if(head2!=NULL){
if(head2->ceng<high)
{if(head2->lc==NULL)
{head2->lc=new Biao;
head2->lc->ceng =head2->ceng +1;
head2->lc->data='*';head2->lc->juli=0;
head2->lc->lc=NULL; head2->lc->rc=NULL;}
if(head2->rc==NULL)
{head2->rc=new Biao;
head2->rc->ceng =head2->ceng +1;
head2->rc->data='*';head2->rc->juli=0;
head2->rc->lc=NULL; head2->rc->rc=NULL;}
}
buquan(high,head2->lc );
buquan(high,head2->rc );
}}
int main()
{Biaoptr head,head1;i=1;ab=1; Biao abc[51];int high;ju=1;
cout<<"请输入先序遍历的序列,若某节点的左或右子树为空,则用*代替空"<<endl;
head=recurrence(NULL,NULL,NULL);head1=head;
for(high=0;high<=50;high++)
{abc[high].ceng=0;
abc[high].lc=NULL;
abc[high].rc=NULL;
abc[high].juli=0;
abc[high].data='*';}
cengshu ( head, 1);
high=ab-1;
buquan( high,head );
weijuli( head, high);
qitajuli(head);
cengxu(head,abc,1);
//print2( abc);
cout<<"该二叉树序列打印为:"<<endl;
print1(abc,high);
system("pause");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Preorder-binary-tree-(tree-print.zip (34个子文件)
先序二叉树(树状打印
先序二叉树.sdf 5.39MB
先序二叉树
先序二叉树.cpp 4KB
先序二叉树.vcxproj.filters 953B
先序二叉树.vcxproj 4KB
先序二叉树.vcxproj.user 143B
Debug
vc100.idb 427KB
先序二叉树.exe.embed.manifest 406B
CL.write.1.tlog 788B
CL.read.1.tlog 18KB
mt.read.1.tlog 1KB
rc.write.1.tlog 714B
先序二叉树.log 3KB
rc.read.1.tlog 1KB
先序二叉树.lastbuildstate 85B
先序二叉树.obj 69KB
mt.command.1.tlog 1KB
cl.command.1.tlog 2KB
link-cvtres.read.1.tlog 2B
link.write.1.tlog 2KB
link-cvtres.write.1.tlog 2B
link.command.1.tlog 4KB
rc.command.1.tlog 1KB
link.read.1.tlog 8KB
先序二叉树_manifest.rc 200B
mt.write.1.tlog 266B
先序二叉树.exe.embed.manifest.res 472B
先序二叉树.exe.intermediate.manifest 381B
vc100.pdb 236KB
先序二叉树.suo 16KB
先序二叉树.sln 912B
ipch
先序二叉树-fe703a6e
先序二叉树-714bef29.ipch 14.44MB
Debug
先序二叉树.exe 46KB
先序二叉树.ilk 392KB
先序二叉树.pdb 683KB
共 34 条
- 1
资源评论
weixin_42651887
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功