与
二叉树相关问题的求解:包括前中后,层次遍历,树的高度和深度,叶子结点的个数,度为1、2的结点个数,二叉树中数据的查找等相关问题的实现。
具体实现代码:
运行结果:
具体实现代码:
#include<iostream>
using namespace std;
struct tree //存储数据的二叉树的节点;
{
int data;
tree *left,*right;
};
struct Queue //层次遍历的时候的队列;
{
tree *tr;
Queue *next;
};
class Btree
{
private:
static int n;
static int m;
public:
tree *root;
Queue *qu;
Queue *pHead;
Queue *pEnd;
int flag;
Btree()
{
root=NULL;
qu=NULL;
pHead=NULL;
pEnd=NULL;
flag=0;
}
void create_Btree(int);
void Preorder(tree *); //先序遍历
void inorder(tree *); //中序遍历
void Postorder(tree *); //后序遍历
void Ceng(tree *); //层次遍历
int Hight_Length(tree *); //计算树的高度和深度;
int count(tree *); //计算二叉树的个数;
int findleaf(tree *); //求二叉树叶子的个数
int findnode(tree *); //求二叉树中度数为的结点数量;
void Destory(tree *); //销毁二叉树;
void Push(tree *);
int print_at_level(tree* T, int level);
void print_by_level_1(tree* T,int level);
int Find1(tree *,int value);
int Find2(tree *,int value);
tree* Pop();
};
int Btree::n=0;
int Btree::m=0;
int Btree::Find1(tree *trr,int findvalue) //递归查找,需要注意函数的调用;
{
//当查找的节点不为空;
if(trr)
{
//找到则返回该值;
if(trr->data==findvalue)
{
flag=1;//递归调用的函数返回不是全部结束,而是结束当前继续执行其他;
return findvalue;
}
//看在左子树还是右子树;
if(trr->data > findvalue&& flag==0)
{
Find1(trr->left,findvalue);
}
if(trr->data < findvalue&& flag==0)
{
Find1(trr->right,findvalue);
}
}
else
return -1;
}
int Btree::Find2(tree *trr,int findvalue) //查找结点;
{
//当查找的节点不为空;
while(trr)
{
//找到则返回该值;
if(trr->data==findvalue)
{
return findvalue;
}
//看在左子树还是右子树;
if(trr->data > findvalue)
{
trr=trr->left;
}
if(trr->data < findvalue)
{
trr=trr->right;
}
}
return -1;
}
int Btree::print_at_level(tree*T, int level)
{
if (!T|| level < 0) //当根节点为空或者是层次小于时,退出;
return 0;
if (0 == level) //当LEVEL为时,t->left,每当减一次,就下一层;
{
cout << T->data<< " ";
return 1;
}
return print_at_level(T->left, level - 1) + print_at_level(T->right, level - 1);
}
void Btree::print_by_level_1(tree*T,int level) //按层打印;
{
int i= 0;
for (i= 0; i<level;i++)
{
if (!print_at_level(T, i))
break;
}
cout << endl;
}
void Btree::Push(tree *p)
{
Queue *pp=new Queue;
pp->tr=p;
pp->next=NULL;
if(pHead==NULL)
{
pHead=pp;
pEnd=pHead;
}
else
{
pEnd->next=pp;
pEnd=pp;
}
}
tree* Btree::Pop()
{
if(!pHead)
{
return NULL;
}
else
{
tree *trr=pHead->tr;
Queue *temp=pHead;
pHead=pHead->next;
delete temp;
return trr;
}
}
void Btree::Ceng(tree *temp) //层次遍历;
{
tree *p=temp;
while(p!=NULL)
{
cout<<p->data<<" ";
if(p->left)
{
Push(p->left);
}
if(p->right)
{
Push(p->right);
}
p=Pop();
}
}
void Btree::Destory(tree *p) //销毁二叉树;
{
if(NULL==p)
{
return;
}
else
{
Destory(p->left);
Destory(p->right);
delete p;
p=NULL;
}
}
int Btree::Hight_Length(tree*p) //返回二叉树的高;
{
int Hleft,Hright;
if(p==NULL)
return 0;
else
{
Hright=Hight_Length(p->right);
Hleft=Hight_Length(p->left);
return 1+(Hright>Hleft? Hright:Hleft);
}
}
void Btree::create_Btree(int x) //循环创建二叉树;
{
tree *newnode=new tree;
newnode->data=x;
newnode->right=newnode->left=NULL;
if(root==NULL)
root=newnode;
else
{
tree *back;
tree *current=root;
while(current!=NULL)
{
back=current;
if(current->data>x)
current=current->left;
else
current=current->right;
}
if(back->data>x)
back->left=newnode;
else
back->right=newnode;
}
}
int Btree::count(tree *p) //统计结点的个数;
{
if(p==NULL)
return 0;
else
return count(p->left)+count(p->right)+1; //这是运用了函数嵌套即递归的方法。
}
void Btree::Preorder(tree *temp) //这是先序遍历二叉树,采用了递归的方法。
{
if(temp!=NULL)
{
cout<<temp->data<<" ";
Preorder(temp->left);
Preorder(temp->right);
}
}
void Btree::inorder(tree *temp) //这是中序遍历二叉树,采用了递归的方法。
{
if(temp!=NULL)
{
inorder(temp->left);
cout<<temp->data<<" ";
inorder(temp->right);
}
}
void Btree::Postorder(tree*temp) //这是后序遍历二叉树,采用了递归的方法。
{
if(temp!=NULL)
{
Postorder(temp->left);
Postorder(temp->right);
cout<<temp->data<<" ";
}
}
int Btree::findleaf(tree *temp) //统计叶子结点;
{
if(temp==NULL)
return 0;
else
{
if(temp->left==NULL&& temp->right==NULL)
return n+=1;
else
{
findleaf(temp->left);
findleaf(temp->right);
}
return n;
}
}
int Btree::findnode(tree *temp) //度为1的结点
{
if(temp==NULL)
return 0;
else
{
if(temp->left!=NULL&& temp->right!=NULL)
{
findnode(temp->left);
findnode(temp->right);
}
if(temp->left!=NULL&& temp->right==NULL)
{
m+=1;
findnode(temp->left);
}
if(temp->left==NULL&& temp->right!=NULL)
{
m+=1;
findnode(temp->right);
}
}
return m;
}
void main()
{
Btree A;
// intarray[]={7,4,2,3,15,35,6,45,55,20,1,14,56,57,58};
// int k; //15
// k=sizeof(array)/sizeof(array[0]);
int k;
cout<<"please input the number of your data: "<<endl;
cin>>k;
int *array=new int[k];
cout<<"please input "<<k<<"numbers:"<<endl;
for(int i=0;i<k;i++)
cin>>array[i];
cout<<"建立排序二叉树顺序: "<<endl;
for(int i=0;i<k;i++)
{
cout<<array[i]<<" ";
A.create_Btree(array[i]);
}
cout<<endl;
cout<<"二叉树节点个数:"<<A.count(A.root)<<endl;
cout<<"二叉树叶子个数:"<<A.findleaf(A.root)<<endl;
cout<<"二叉树中度数为1的结点的数量为:"<<A.findnode(A.root)<<endl;
cout<<"二叉树的高度--高度为:"<<A.Hight_Length(A.root)<<endl;
cout<<"查找数字8的结果是:"<<A.Find1(A.root,8)<<endl;
A.flag=0;
cout<<"查找数字8的结果是:"<<A.Find2(A.root,8)<<endl;
cout<<endl<<"先序遍历序列: "<<endl;
A.Preorder(A.root);
cout<<endl<<"中序遍历序列: "<<endl;
A.inorder(A.root);
cout<<endl<<"后序遍历序列: "<<endl;
A.Postorder(A.root);
cout<<endl<<"层次遍历序列: "<<endl;
A.Ceng(A.root);
cout<<endl<<"递归层次遍历序列: "<<endl;
A.print_by_level_1(A.root,A.Hight_Length(A.root));
cout<<endl;
A.Destory(A.root);
system("pause");
}
二叉树的结构图:
运行结果: