二叉树的创建和操作
while (rear!=front) //次循环通过层次遍历求每个节点的层次
{
front++;
b=Qu[front].p; //对头出对
lnum=Qu[front].lno;
if (b- lchild!=NULL) //左孩子入队
{
rear++;
Qu[rear].p=b- lchild;
Qu[rear].lno=lnum+1;
}
if (b- rchild!=NULL) //右孩子入队
{
rear++;
Qu[rear].p=b- rchild;
Qu[rear].lno=lnum+1;
}
}
max=0;lnum=1;i=1; //lnum从第一层开始
while (i =rear) //通过比较相同层次的结点数求树的宽度
{
n=0;
while (i =rear Qu[i].lno==lnum)
{
n++;
i++;
}
lnum=Qu[i].lno;
if (n max)
{
max=n;
}
}
return max;
}
else
return 0;
}
//int system(const char *string); //自动清屏代码
//char Check()
//{
// printf("是否清屏:Y|N");
// char a;
// scanf("%c",
// if (a==y||a==Y)
// {
// return a;
// }
// else
// return N;
//}
//void clear()
//{
// printf("是否清屏(Y|N):");
// char a;
// scanf("%c",
// if (a==y||a==Y)
// {
// system("pause");
// system("cls");
// }
//}
//菜单函数
void shoumenu(){
printf("\n\n\n");
printf(" --二叉树的基本运算--\n");
printf("*****************************************\n");
printf("* 1------建二叉树 *\n");
printf("* 2------先序遍历 *\n");
printf("* 3------中序遍历 *\n");
printf("* 4------后序遍历 *\n");
printf("* 5------统计叶子数 *\n");
printf("* 6------统计结点数 *\n");
printf("* 7------求二叉树深度 *\n");
printf("* 8------求二叉树宽度 *\n");
printf("* *\n");
printf("* 0------退出 *\n");
printf("*****************************************\n");
printf("请选择菜单号(0--8):");
}
//选择功能函数
void binaryOP(){
NODE *bt = NULL;
int count = 0;
char choice = N;
int x;
while(choice != 0){
shoumenu();
flushall();
scanf("%c", choice);
switch(choice){
case 1:
printf("\n\t请输入按先序建立二叉树的结点序列:");
printf("\n\t 说明:逐个输入,0代表后继结点为空,按回车输入下一个结点");
printf("\n\t请输入根结点:");
bt = crt_bt_pre();/*调用创建二叉树的函数*/
printf("\n\t二叉树成功建立!\n");
//clear();
break;
case 2:
if (bt == NULL){
printf("\n\t空树");
}
else{
printf("\n\t该二叉树的先序遍历的序列为:");
Preorder(bt);/*调用先序遍历函数*/
}
printf("\n");
//clear();
break;
case 3:
if (bt == NULL){
printf("\n\t空树");
}
else{
printf("\n\t该二叉树的中序遍历序列:");
Inorder(bt);/*调用中序遍历函数*/
}
printf("\n");
//clear();
break;
case 4:
if (bt == NULL){
printf("\n\t空树");
}
else{
printf("\n\t该二叉树的后序遍历序列为:");
Postorder(bt);/*调用后序遍历函数*/
}
printf("\n");
//clear();
break;
case 5:
count = CountLeaf(bt);/*调用统计叶子结点个数的函数*/
printf("\n\t该二叉树有%d个叶子结点。\n",count);
printf("\n");
//clear();
break;
case 6:
count = CountNode(bt);/*调用统计结点总数的函数*/
printf("\n\t该二叉树共有%d个结点 \n",count);
printf("\n");
//clear();
break;
case 7:
x = TreeDepth(bt);/*调用求二叉树深度的函数*/
printf("\n\t 该二叉树的深度为%d",x);
printf("\n");
//clear();
break;
case8:
int n;
n=TreeWidth(bt);
printf("\n\t 该二叉树的宽度为%d\n",n);
//clear();
break;
case 0:
printf("\n\t 程序结束!\n");
printf("\n");
//clear();
break;
default :
printf("\n\t输入有误,请重新输入!\n");
printf("\n");
//clear();
}
}
}
void main()
{
binaryOP();
}
总结了一些二叉树操作的干货…… 导读:二叉树是一种经典的数据结构,其概念本身不难理解,但因其结构的特殊性,许多操作都有着非常精妙的技巧。结合最近LeetCode中的一些相关题目,简要记录一些个人觉得比较巧妙的编程实现。
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
相关文章
- 017-平衡二叉树(三)
- 24由遍历序列构造二叉树
- 一颗完全二叉树,求其结点个数
- LeetCode_二叉树_中等_654.最大二叉树
- 判断完全二叉树
- 94. 二叉树的中序遍历
- 102.二叉树的层次遍历
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
- 算法:二叉树的三种遍历 & 二叉搜索树的遍历
- 一步一步写算法(之排序二叉树删除-1)
- 算法4-10:BST平衡二叉树的删除操作
- 二叉树的各种操作
- 交互二叉树的所有左子树和右子树.
- 加分二叉树
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历