数据结构实验 二叉树的基本操作
数据结构实验 二叉树的基本操作实验环境:Visual C++实验目的:1.掌握二叉树的定义;2.掌握二叉树的基本操作,如二叉树的建立、遍历、结点个数统计、树的深度计算等。实验内容:用递归的方法实现以下算法:1.以二叉链表表示二叉树,建立一棵二叉树;2.输出二叉树的中序遍历结果;3.输出二叉树的前序遍历结果;4.输出二叉树的后序遍历结果;5.计算二叉树的深度...
·
数据结构实验 二叉树的基本操作
运行软件:Dev-C++
环境:Visual C++
内容:
用递归的方法实现以下算法:
1.以二叉链表表示二叉树,建立一棵二叉树;
2.输出二叉树的中序遍历结果;
3.输出二叉树的前序遍历结果;
4.输出二叉树的后序遍历结果;
5.计算二叉树的深度;
6.统计二叉树的结点个数;
7.统计二叉树的叶结点个数;
8.统计二叉树的度为1的结点个数;
9. 交换左右子树
代码:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#define MAXTSIZE 1000
using namespace std;
typedef struct BiTNode
{
char data; // 结点数据域
struct BiTNode *lchild,*rchild; // 左右孩子指针
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T) // 先序遍历建立二叉链表
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void travel1(BiTree T) // 先序遍历
{
if(T)
{
printf("%c",T->data);
travel1(T->lchild);
travel1(T->rchild);
}
}
void travel2(BiTree T) // 中序遍历
{
if(T)
{
travel2(T->lchild);
printf("%c",T->data);
travel2(T->rchild);
}
}
void travel3(BiTree T) // 后序遍历
{
if(T)
{
travel3(T->lchild);
travel3(T->rchild);
printf("%c",T->data);
}
}
int NodeCount(BiTree T)//统计二叉树中结点的个数
{
if(T==NULL) return 0; //如果是空树,则结点个数为0, 递归结束
else return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}
int count(BiTree T) // 计算叶子结点的个数
{
if(T==NULL) return 0;
int cnt=0;
if((!T->lchild)&&(!T->rchild))
{
cnt++;
}
int leftcnt=count(T->lchild);
int rightcnt=count(T->rchild);
cnt+=leftcnt+rightcnt;
return cnt;
}
int Depth(BiTree T) // 计算二叉树的深度
{
if(T==NULL) return 0;
else
{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
return m>n?(m+1):(n+1);
}
}
void exchange(BiTree T,BiTree &NewT) // 交换左右子树
{
if(T==NULL)
{
NewT=NULL;
return ;
}
else
{
NewT=(BiTNode *)malloc(sizeof(BiTNode));
NewT->data=T->data;
exchange(T->lchild,NewT->rchild); // 复制原树的左子树给新树的右子树
exchange(T->rchild,NewT->lchild); // 复制原树的右子树给新树的左子树
}
}
int NodeNumber_1(BiTree T) //统计二叉树的度为1的结点个数;
{
int i=0;
if(T)
{
if( (T->lchild==NULL&&T->rchild!=NULL) ||(T->lchild!=NULL&&T->rchild==NULL))
{
i=1+NodeNumber_1(T->lchild)+NodeNumber_1(T->rchild);
}
else
{
i=NodeNumber_1(T->lchild)+NodeNumber_1(T->rchild);
}
}
return i;
}
int main()
{
puts("**************************");
puts("1. 建立二叉树");
puts("2. 先序遍历二叉树");
puts("3. 中序遍历二叉树");
puts("4. 后序遍历二叉树");
puts("5. 计算结点个数");
puts("6. 计算叶子结点个数");
puts("7. 计算二叉树的深度");
puts("8. 统计二叉树的度为1的结点个数;");
puts("9. 交换二叉树的左右子树");
puts("0. 退出");
puts("**************************");
BiTree Tree,NewTree;
int choose;
while(~scanf("%d",&choose),choose)
{
switch(choose)
{
case 1:
puts("以 '#' 为左/右子树空的标志!");
CreateBiTree(Tree);
break;
case 2:
printf("先序遍历结果为:");
travel1(Tree);
puts("");
break;
case 3:
printf("中序遍历结果为:");
travel2(Tree);
puts("");
break;
case 4:
printf("后序遍历结果为:");
travel3(Tree);
puts("");
break;
case 5:
printf("结点个数为:%d\n",NodeCount(Tree));
break;
case 6:
printf("叶子结点个数为:%d\n",count(Tree));
break;
case 7:
printf("二叉树的深度为:%d\n",Depth(Tree));
break;
case 8:
printf("度为1的结点个数:%d\n",NodeNumber_1(Tree));
break;
case 9:
exchange(Tree,NewTree);
Tree=NewTree;
puts("交换成功!\n");
break;
}
}
system("pause");
return 0;
}
运行结果:
点击阅读全文
更多推荐
目录
所有评论(0)