实用数据结构基础.docx
- 文档编号:4621905
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:7
- 大小:15.73KB
实用数据结构基础.docx
《实用数据结构基础.docx》由会员分享,可在线阅读,更多相关《实用数据结构基础.docx(7页珍藏版)》请在冰豆网上搜索。
实用数据结构基础
实用数据结构基础
(第三版谭浩强)
验证性实验7:
二叉树子系统
#include
#defineTREEMAX100
typedefstructBT
{chardata;
BT*lchild;
BT*rchild;
}BT;
BT*CreateTree();
voidShowTree(BT*T);
voidPreorder(BT*T);
voidPostorder(BT*T);
voidLevelorder(BT*T);
voidInorder(BT*T);
voidLeafnum(BT*T);
voidNodenum(BT*T);
intTreeDepth(BT*T);
intcount=0;
voidmain()
{BT*T=NULL;
charch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='Y')
{printf("\n");
printf("\n\t\t二叉树子系统");
printf("\n\t\t************************************");
printf("\n\t\t*1------建二叉树*");
printf("\n\t\t*2------凹入显示*");
printf("\n\t\t*3------先序遍历*");
printf("\n\t\t*4------中序遍历*");
printf("\n\t\t*5------后序遍历*");
printf("\n\t\t*6------层次遍历*");
printf("\n\t\t*7------求叶子数*");
printf("\n\t\t*8------求结点数*");
printf("\n\t\t*9------求树深度*");
printf("\n\t\t*0------返回*");
printf("\n\t\t************************************");
printf("\n\t\t请选择菜单号(0--9):
");
scanf("%c",&ch2);
getchar();
printf("\n");
switch(ch2)
{case'1':
printf("\n\t\t请按先序序列输入二叉树的结点:
\n");
printf("\n\t\t说明:
输入结点('0'表示后继结点为空)后按回车.\n");
printf("\n\t\t请输入根结点:
");
T=CreateTree();
printf("\n\t\t二叉树成功建立!
\n");break;
case'2':
ShowTree(T);break;
case'3':
printf("\n\t\t该二叉树的先序遍历序列为:
");
Preorder(T);break;
case'4':
printf("\n\t\t该二叉树的中序遍历序列为:
");
Inorder(T);break;
case'5':
printf("\n\t\t该二叉树的后序遍历序列为:
");
Postorder(T);break;
case'6':
printf("\n\t\t该二叉树的层次遍历序列为:
");
Levelorder(T);break;
case'7':
count=0;Leafnum(T);
printf("\n\t\t该二叉树有%d个叶子。
\n",count);break;
case'8':
count=0;Nodenum(T);
printf("\n\t\t该二叉树总共有%d个结点。
\n",count);break;
case'9':
printf("\n\t\t该树的深度是:
%d",TreeDepth(T));break;
case'0':
ch1='n';break;
default:
printf("\n\t\t***请注意:
输入有误!
***");
}
if(ch2!
='0')
{printf("\n\t\t按【Enter】键继续,按任意键返回主菜单!
\n");
a=getchar();
if(a!
='\xA')
{getchar();ch1='n';}
}
}
}
BT*CreateTree()
{BT*t;
charx;
scanf("%c",&x);
getchar();
if(x=='0')
t=NULL;
else
{t=newBT;
t->data=x;
printf("\n\t\t请输入%结点的左子结点:
",t->data);
t->lchild=CreateTree();
printf("\n\t\t请输入%结点的右子结点:
",t->data);
t->rchild=CreateTree();
}
returnt;
}
voidPreorder(BT*T)
{if(T)
{printf("%3c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
voidInorder(BT*T)
{if(T)
{Inorder(T->lchild);
printf("%3c",T->data);
Inorder(T->lchild);
}
}
voidPostorder(BT*T)
{if(T)
{Postorder(T->lchild);
Postorder(T->rchild);
printf("%3c",T->data);
}
}
voidLevelorder(BT*T)
{inti,j;
BT*q[100],*p;
p=T;
if(p!
=NULL)
{i=1;q[i]=p;j=2;}
while(i!
=j)
{p=q[i];printf("%3c",p->data);
if(p->lchild!
=NULL)
{q[j]=p->lchild;j++;}
if(p->rchild!
=NULL)
{q[j]=p->rchild;j++;}
i++;
}
}
voidLeafnum(BT*T)
{if(T)
{if(T->lchild==NULL&&T->rchild==NULL)
count++;
Leafnum(T->lchild);
Leafnum(T->rchild);
}
}
voidNodenum(BT*T)
{if(T)
{count++;
Nodenum(T->lchild);
Nodenum(T->rchild);
}
}
intTreeDepth(BT*T)
{intldep,rdep;
if(T==NULL)
return0;
else
{ldep=TreeDepth(T->lchild);
rdep=TreeDepth(T->rchild);
if(ldep>rdep)
returnldep+1;
else
returnrdep+1;
}
}
voidShowTree(BT*T)
{BT*stack[TREEMAX],*p;
intlevel[TREEMAX][2],top,n,i,width=4;
if(T!
=NULL)
{printf("\n\t\t凹入表示法:
\n\t\t");
top=1;
stack[top]=T;
level[top][0]=width;
while(top>0)
{p=stack[top];
n=level[top][0];
for(i=1;i<=n;i++)
printf("");
printf("%c",p->data);
for(i=n+1;i<30;i+=2)
printf("▃");
printf("\n\t\t");
top--;
if(p->rchild!
=NULL)
{top++;
stack[top]=p->rchild;
level[top][0]=n+width;
level[top][1]=2;
}
if(p->lchild!
=NULL)
{top++;
stack[top]=p->lchild;
level[top][0]=n+width;
level[top][1]=1;
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实用 数据结构 基础
![提示](https://static.bdocx.com/images/bang_tan.gif)