树的相关操作
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050620460916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDAzMjE3OA==,size_16,color_FFFFFF,t_70#pic_center)
#include <iostream>
using namespace std;
int LeavesNumber=0;
int NodeNumber=0;
int SpecificNodeNumber=0;
typedef struct Node
{
struct Node *left;
struct Node *right;
char data;
}BinaryTreeNode,*BinaryTree;
void CreateBinaryTree(BinaryTree &B)
{
char ch;
cin>>ch;
if(ch == '#')
{
B = NULL;
}
else
{
B = new BinaryTreeNode;
B->data = ch;
CreateBinaryTree(B->left);
CreateBinaryTree(B->right);
}
}
int CountHeight(BinaryTree B)
{
if(!B)
{
return 0;
}
else
{
int LeftBinaryHeight = CountHeight(B->left);
int RightBinaryHeight = CountHeight(B->right);
if(LeftBinaryHeight>RightBinaryHeight)
{
return LeftBinaryHeight+1;
}
else
{
return RightBinaryHeight+1;
}
}
}
void PreTravel(BinaryTree B)
{
if(B)
{
cout<<B->data<<" ";
PreTravel(B->left);
PreTravel(B->right);
}
}
void MidTravel(BinaryTree B)
{
if(B)
{
MidTravel(B->left);
cout<<B->data<<" ";
MidTravel(B->right);
}
}
void PostTravel(BinaryTree B)
{
if(B)
{
PostTravel(B->left);
PostTravel(B->right);
cout<<B->data<<" ";
}
}
int CountLeaves(BinaryTree B)
{
if(B)
{
if(B->left == NULL&&B->right == NULL)
{
LeavesNumber++;
}
CountLeaves(B->left);
CountLeaves(B->right);
}
return LeavesNumber;
}
int CountNode(BinaryTree B)
{
if(B)
{
NodeNumber++;
CountNode(B->left);
CountNode(B->right);
}
return NodeNumber;
}
int CountSpecificNode(BinaryTree B,char c)
{
if(B)
{
if(B->data == c)
{
SpecificNodeNumber++;
}
CountSpecificNode(B->left,c);
CountSpecificNode(B->right,c);
}
return SpecificNodeNumber;
}
void PrintBinaryTree(BinaryTree B)
{
BinaryTree stack[100],p;
int level[100],top,n;
if (B)
{
top=1;
stack[top]=B;
level[top]=0;
while(top>0)
{
p=stack[top];
n=level[top];
for(int i=1; i<=n; i++)
cout<<" ";
cout<<p->data<<endl;
top--;
if (p->right!=NULL)
{
top++;
stack[top]=p->right;
level[top]=n+2;
}
if (p->left!=NULL)
{
top++;
stack[top]=p->left;
level[top]=n+2;
}
}
}
}
void FunctionSelect(BinaryTree &B,char ch)
{
if(ch == 'C')
{
cin.get();
CreateBinaryTree(B);
cout<<"Created success!"<<endl;
}
else if(ch == 'H')
{
cout<<"Height="<<CountHeight(B)<<"."<<endl;
}
else if(ch == 'L')
{
cout<<"Leaf="<<CountLeaves(B)<<"."<<endl;
}
else if(ch == 'N')
{
cout<<"Nodes="<<CountNode(B)<<"."<<endl;
}
else if(ch == '1')
{
cout<<"Preorder is:";
PreTravel(B);
cout<<"."<<endl;
}
else if(ch == '2')
{
cout<<"Inorder is:";
MidTravel(B);
cout<<"."<<endl;
}
else if(ch == '3')
{
cout<<"Postorder is:";
PostTravel(B);
cout<<"."<<endl;
}
else if(ch == 'F')
{
cin.get();
char q;
cin>>q;
cout<<"The count of "<<q<<" is "<<CountSpecificNode(B,q)<<"."<<endl;
}
else if(ch == 'P')
{
cout<<"The tree is:"<<endl;
PrintBinaryTree(B);
}
else
{
cout<<"输入不符合规定,请重新输入:"<<endl;
}
}
int main()
{
char a;
BinaryTree BT;
while(cin>>a)
{
FunctionSelect(BT,a);
}
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020050620484819.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDAzMjE3OA==,size_16,color_FFFFFF,t_70#pic_center)