假设二叉数的数据元素为字符,采用二叉链式存储结构。请编码实现二叉树ADT,其中包括创建二叉树、遍历二叉树(深度、广度)、求二叉树的深度(高度)、计算二叉树的元素个数、计算二叉树的叶子数、二叉树的格式输出等。
根据输入的符号,执行相应的操作。如下:
C:创建二叉树,创建成功输出 “Created success!”。要求实现两种创建算法。输入数字“1" ,是根据完全前序序列创建二叉树,#表示空结点(空子树);下一行输入二叉树的完全前序序列。 输入数字“2”,是根据二叉树的前序和中序序列创建二叉树,后面有三行,分别输入元素个数、前序序列和中序序列。
H:求二叉树的高度; 输出: Height=高度
L:计算二叉树的叶子数;输出:Leaves=叶子个数
N:计算二叉树中元素总个数;输出:Nodes=结点个数
1:先序遍历二叉树;输出:Preorder is:序列 .
2:中序遍历二叉树;输出:Inorder is:序列 .
3:后序遍历二叉树;输出:Postorder is:序列 .
4:广度遍历二叉树;输出:BFSorder is:序列 .
F:查找值为x的结点个数;输出:The count of x is 个数 .
P:以目录缩格文本形式输出所有节点。输出:The tree is:(换行,下面各行是输出的二叉树)
X:退出
输入
C
1
ABC##DE#G##F###
H
L
N
1
2
3
4
F
A
P
X
输出
Created success!
Height=5
Leaves=3
Nodes=7
Preorder is:A B C D E G F .
Inorder is:C B E G D F A .
Postorder is:C G E F D B A .
BFSorder is:A B C D E F G .
The count of A is 1
The tree is:
A
B
C
D
E
G
F
输入
C
2
6
abcdef
abcdef
H
L
N
1
2
3
4
F
h
P
X
输出
Created success!
Height=6
Leaves=1
Nodes=6
Preorder is:a b c d e f .
Inorder is:a b c d e f .
Postorder is:f e d c b a .
BFSorder is:a b c d e f .
The count of h is 0
The tree is:
a
b
c
d
e
f
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
const int maxSize=105;
typedef struct node//结点
{
char data;
struct node*leftchild,*rightchild;
}Node;
class Queue//队列
{
private:
Node* elements[maxSize];
int Left,Right;
public:
Queue(){
Left=0;Right=0;};
void Push(Node *x){
elements[Right]=x;Right=(Right+1)%maxSize;};
void Pop(){
Left=(Left+1)%maxSize;};
Node* Front(){
return elements[Left];};
Node* Back(){
return elements[Right];};
int Size(){
return (Right-Left+maxSize)%maxSize;};
bool Empty(){
return Left==Right?true:false;};
bool Full(){
return (Right+1)%maxSize==Left?true:false;};
};
class BinTree//二叉树类
{
public:
Node*root;
void pre_create(Node*&subtree);//前序创建
Node* pre_in_create(int n,string pre,string in);//前序+中序创建
int get_height(Node *subtree);//求高度
int get_leaves(Node *subtree);//求叶节点
int get_cnt_node(Node *subtree);//求结点数
void PreOrder(Node *subtree);//前序遍历
void InOrder(Node *subtree);//中序遍历
void PosOrder(Node *subtree);//后序遍历
void BFSOrder(Node *subtree);//广度遍历
int find_value_cnt(Node *subtree,char value);//寻找元素出现的次数
void show(Node*subtree,int len);//输出二叉树
};
void BinTree::pre_create(Node*&subtree)
{
char ch;
cin>>ch;
if(ch!='#')
{
subtree=new Node();
subtree->data=ch;
pre_create(subtree->leftchild);
pre_create(subtree->rightchild);
}
else subtree=NULL;
}
Node* BinTree::pre_in_create(int n,string pre,string in)
{
string l_pre,r_pre,l_in,r_in;
if(n==0)return NULL;
Node*t=new Node;
t->data=pre[0];
int index=in.find(t->data);
l_in=in.substr(0,index);//中序序列的左子树
int len_l_in=l_in.length();
r_in=in.substr(index+1);//中序序列的右子树
int len_r_in=r_in.length();
l_pre=pre.substr(1,len_l_in);//前序序列的左子树
r_pre=pre.substr(len_l_in+1);//前序序列的右子树
t->leftchild=pre_in_create(len_l_in,l_pre,l_in);//递归左儿子
t->rightchild=pre_in_create(len_r_in,r_pre,r_in);//递归右儿子
return t;
}
int BinTree::get_height(Node*subtree)
{
if(subtree==NULL)return 0;
else return 1+max(get_height(subtree->leftchild),get_height(subtree->rightchild));
}
int BinTree::get_leaves(Node *subtree)
{
if(subtree==NULL)return 0;
if(subtree->leftchild==NULL&&subtree->rightchild==NULL)return 1;
else return get_leaves(subtree->leftchild)+get_leaves(subtree->rightchild);
}
int BinTree::get_cnt_node(Node *subtree)
{
if(subtree==NULL)return 0;
else return 1+get_cnt_node(subtree->leftchild)+get_cnt_node(subtree->rightchild);
}
void BinTree::PreOrder(Node *subtree)
{
if(subtree!=NULL)
{
printf("%c ",subtree->data);
PreOrder(subtree->leftchild);
PreOrder(subtree->rightchild);
}
else return;
}
void BinTree::InOrder(Node *subtree)
{
if(subtree!=NULL)
{
InOrder(subtree->leftchild);
printf("%c ",subtree->data);
InOrder(subtree->rightchild);
}
else return;
}
void BinTree::PosOrder(Node *subtree)
{
if(subtree!=NULL)
{
PosOrder(subtree->leftchild);
PosOrder(subtree->rightchild);
printf("%c ",subtree->data);
}
else return;
}
void BinTree::BFSOrder(Node *subtree)
{
if(subtree==NULL)return;
Queue q;
Node*t=subtree;
q.Push(t);
while(!q.Empty())
{
printf("%c ",q.Front()->data);
if(q.Front()->leftchild!=NULL)q.Push(q.Front()->leftchild);
if(q.Front()->rightchild!=NULL)q.Push(q.Front()->rightchild);
q.Pop();
}
}
int BinTree::find_value_cnt(Node *subtree,char value)
{
if(subtree==NULL)return 0;
else
{
if(subtree->data==value)return 1+find_value_cnt(subtree->leftchild,value)+find_value_cnt(subtree->rightchild,value);
else return find_value_cnt(subtree->leftchild,value)+find_value_cnt(subtree->rightchild,value);
}
}
void BinTree::show(Node *subtree,int len)//len表示层数
{
if(subtree!=NULL)
{
for(int i=1;i<=2*len-2;i++)printf(" ");
printf("%c\n",subtree->data);
show(subtree->leftchild,len+1);
show(subtree->rightchild,len+1);
}
else return;
}
int main()
{
char op[5],x[5];
int ch,n;
BinTree btree;
string PreSeq,InSeq;
while(1)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d",&ch);
if(ch==1)
{
btree.pre_create(btree.root);
printf("Created success!\n");
}
else if(ch==2)
{
cin>>n>>PreSeq>>InSeq;
btree.root=btree.pre_in_create(n,PreSeq,InSeq);
printf("Created success!\n");
}
}
else if(op[0]=='H')printf("Height=%d\n",btree.get_height(btree.root));
else if(op[0]=='L')printf("Leaves=%d\n",btree.get_leaves(btree.root));
else if(op[0]=='N')printf("Nodes=%d\n",btree.get_cnt_node(btree.root));
else if(op[0]=='1')
{
printf("Preorder is:");
btree.PreOrder(btree.root);
printf(".\n");
}
else if(op[0]=='2')
{
printf("Inorder is:");
btree.InOrder(btree.root);
printf(".\n");
}
else if(op[0]=='3')
{
printf("Postorder is:");
btree.PosOrder(btree.root);
printf(".\n");
}
else if(op[0]=='4')
{
printf("BFSorder is:");
btree.BFSOrder(btree.root);
printf(".\n");
}
else if(op[0]=='F')
{
scanf("%s",x);
printf("The count of %c is %d\n",x[0],btree.find_value_cnt(btree.root,x[0]));
}
else if(op[0]=='P')
{
printf("The tree is:\n");
btree.show(btree.root,1);
}
else if(op[0]=='X')exit(0);
}
return 0;
}