#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
using namespace std;
typedef char TElemType;
typedef bool Status;
typedef struct Tree{
TElemType Data;
struct Tree * lchild, * rchild;
}BiTNode, *BiTree;
BiTree CreatTree(){
char temp;
char data;
BiTree T;
cin>>data;
temp = getchar();
if(data < 65 || (data > 90 && data < 97) || data > 122){
return NULL;
}
else{
T = (BiTree)malloc(sizeof(BiTNode));
if(!T) exit(-1);
T->Data = data;
cout<<"请输入"<<data<<"左子树的值:";
T->lchild = CreatTree();
cout<<"请输入"<<data<<"右子树的值:";
T->rchild = CreatTree();
return T;
}
}
void PreShow(BiTree T){
if(T == NULL) return;
cout<<T->Data<<" ";
PreShow(T->lchild);
PreShow(T->rchild);
}
void InShow1(BiTree T){
if(T == NULL) return ;
InShow1(T->lchild);
cout<<T->Data<<" ";
InShow1(T->rchild);
}
Status InShow2(BiTree T){
stack<BiTree>st;
BiTree p = T;
while(p || !st.empty()){
if(p){
st.push(p);
p = p->lchild;
}
else{
p = st.top();
st.pop();
cout<<p->Data<<" ";
p = p->rchild;
}
}
return 1;
}
void HouShow(BiTree T){
if(T == NULL) return ;
HouShow(T->lchild);
HouShow(T->rchild);
cout<<T->Data<<" ";
}
void CShow(BiTree T){
queue<BiTree>q;
q.push(T);
BiTree p;
while(!q.empty()){
p = q.front();
q.pop();
cout<<p->Data<<" ";
if(p->lchild != NULL) q.push(p->lchild);
if(p->rchild != NULL) q.push(p->rchild);
}
}
int DepthBiTree(BiTree T){
if(T == NULL) return 0;
int l = DepthBiTree(T->lchild);
int r = DepthBiTree(T->rchild);
return l > r ? l + 1 : r + 1;
}
int SumYe(BiTree T){
int sum = 0;
if(T == NULL) sum = 0;
else if(T->lchild == NULL && T->rchild == NULL) sum = 1;
else sum = SumYe(T->lchild) + SumYe(T->rchild);
return sum;
}
int SizeBiTree(BiTree T){
if(T == NULL) return 0;
int sum = 1;
sum += SizeBiTree(T->lchild) + SizeBiTree(T->rchild);
return sum;
}
Status IsComplete(BiTree T){
if(T == NULL) return 1;
queue<BiTree>q;
q.push(T);
while(!q.empty()){
BiTree p = q.front();
q.pop();
if(p){
q.push(p->lchild);
q.push(p->rchild);
}
else{
while(!q.empty()){
p = q.front();
q.pop();
if(p != NULL) return 0;
}
}
}
return 1;
}
Status IsMan(BiTree T){
int n = SizeBiTree(T);
int k = DepthBiTree(T);
int sum = 1;
while(k -- ){
sum *= 2;
}
if(sum - 1 == n) return 1;
return 0;
}
int main()
{
BiTree T;
while(1){
cout<<"**********************************************"<<endl;
cout<<"******* 请选择你需要的功能: *******"<<endl;
cout<<"******* 1.创建二叉树 *******"<<endl;
cout<<"******* 2.先序遍历二叉树 *******"<<endl;
cout<<"******* 3.中序遍历二叉树(递归) *******"<<endl;
cout<<"******* 4.中序遍历二叉树(非递归) *******"<<endl;
cout<<"******* 5.后序遍历二叉树 *******"<<endl;
cout<<"******* 6.层序遍历二叉树 *******"<<endl;
cout<<"******* 7.求二叉树的深度 *******"<<endl;
cout<<"******* 8.求二叉树的叶子节点个数 *******"<<endl;
cout<<"******* 9.求二叉树的节点个数 *******"<<endl;
cout<<"******* 10.判断树是否为完全二叉树 *******"<<endl;
cout<<"******* 11.判断树是否为满二叉树 *******"<<endl;
cout<<"******* 提示:输入负数退出程序 *******"<<endl;
cout<<"**********************************************"<<endl;
int a;
cin>>a;
if(a < 0){
cout<<"感谢使用!"<<endl;
break;
}
else if(a == 1){
cout<<"输入非字母表示停止创建" <<endl;
cout<<"请输入根节点的值:";
T = CreatTree();
cout<<"树创建完成!"<<endl;
}
else if(a == 2){
cout<<"先序遍历的结果为:";
PreShow(T);
}
else if(a == 3){
cout<<"中序(递归)遍历的结果为:";
InShow1(T);
}
else if(a == 4){
cout<<"中序(非递归)遍历的结果为:";
InShow2(T);
}
else if(a == 5){
cout<<"后序遍历的结果为:";
HouShow(T);
}
else if(a == 6){
cout<<"层序遍历的结果为:";
CShow(T);
}
else if(a == 7){
cout<<"树的深度为:";
cout<<DepthBiTree(T);
}
else if(a == 8){
cout<<"树的叶节点个数为:";
cout<<SumYe(T);
}
else if(a == 9){
cout<<"树的节点个数为:";
cout<<SizeBiTree(T);
}
else if(a == 10){
if(IsComplete(T)) cout<<"这棵树是完全二叉树"<<endl;
else cout<<"这棵树不是完全二叉树"<<endl;
}
else if(a == 11){
if(IsMan(T)) cout<<"这棵树是满二叉树"<<endl;
else cout<<"这棵树不是满二叉树"<<endl;
}
else{
cout<<"请输入正确的指令";
}
cout<<endl;
}
return 0;
}
数据结构—二叉树的基本操作
最新推荐文章于 2024-09-05 14:36:31 发布