Description
用函数实现如下二叉排序树算法:
(1) 插入新结点
(2) 前序、中序、后序遍历二叉树
(3) 中序遍历的非递归算法
(4) 层次遍历二叉树
(5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0)
(6) 交换各结点的左右子树
(7) 求二叉树的深度
(8) 叶子结点数
输入格式
第一行:准备建树的结点个数n
第二行:输入n个整数,用空格分隔
第三行:输入待查找的关键字
第四行:输入待查找的关键字
第五行:输入待插入的关键字
输出格式
第一行:二叉树的先序遍历序列
第二行:二叉树的中序遍历序列
第三行:二叉树的后序遍历序列
第四行:查找结果
第五行:查找结果
第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列
第九行:插入新结点后的二叉树的中序遍历序列(非递归算法)
第十行:插入新结点后的二叉树的层次遍历序列
第十一行~第十三行:第一次交换各结点的左右子树后的先、中、后序遍历序列
第十四行~第十六行:第二次交换各结点的左右子树后的先、中、后序遍历序列
第十七行:二叉树的深度
第十八行:叶子结点数
输入样例
7
40 20 60 18 50 56 90
18
35
30
输出样例
40 20 18 60 50 56 90
18 20 40 50 56 60 90
18 20 56 50 90 60 40
1
0
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
18 20 30 40 50 56 60 90
40 20 60 18 30 50 90 56
40 60 90 50 56 20 30 18
90 60 56 50 40 30 20 18
90 56 50 60 30 18 20 40
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
4
4
注意事项和思路在代码中
代码如下:
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
typedef struct BSTNode
{
int data;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
void Pre(BSTree T)//先序遍历
{
if(T)
{
cout<<T->data<<" ";
Pre(T->lchild);
Pre(T->rchild);
}
}
void In(BSTree T)//中序遍历
{
if(T)
{
In(T->lchild);
cout<<T->data<<" ";
In(T->rchild);
}
}
void Post(BSTree T)//后序遍历
{
if(T)
{
Post(T->lchild);
Post(T->rchild);
cout<<T->data<<" ";
}
}
void Insert(BSTree &T,int e)//二叉排序树的插入操作
{
if(!T)//找到插入位置,递归结束
{
BSTree S;
S=new BSTNode; //生成新节点
S->data=e;
S->lchild=S->rchild=NULL;
T=S;//把新节点*S链接到已找到的插入位置
}
else if(e<T->data) Insert(T->lchild,e);
else if(e>T->data) Insert(T->rchild,e);
}
void CreateBSTree(BSTree &T,int n)//创建二叉排序树
{
T=NULL;
int e;
for(int i=0;i<n;i++)
{
cin>>e;
Insert(T,e);
}
}
//根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看作一根结点
//然后继续访问其左孩子,直到遇到左孩子为空的结点才进行访问结点,
//然后按相同的规则访问其右子树
void Inf(BSTree T)//中序遍历的非递归算法
{ //对任一结点p
//若其左孩子不为空,则将p入栈并将p的左孩子置为当前的p,然后对当前的p进行相同的处理
//若其左孩子为空,取栈顶元素并作出栈操作,访问该栈顶结点,然后将当前的p置为栈顶结点的右孩子
//直到p为空且栈为空则遍历结束
stack < BSTree > s;
BSTree p;
p=T;
while(p||!s.empty())
{
while(p)
{
s.push(p);//根指针进栈
p=p->lchild;//遍历左子树
}
if(!s.empty()) //直到左孩子为空
{
p=s.top();
cout<<p->data<<" ";//访问根节点
s.pop();//退栈
p=p->rchild;//遍历右子树
}
}
}
void Floor(BSTree T)//层次遍历
{
queue <BSTree> q;
if(T) q.push(T);//根节点入队列
while(!q.empty())//队列不为空
{
cout<<q.front()->data<<" ";
if(q.front()->lchild)
q.push(q.front()->lchild);//如果左子树不为空,左孩子入队
if(q.front()->rchild)
q.push(q.front()->rchild);//如果右子树不为空,右孩子入队
q.pop();//已经遍历过的节点出队
}
}
BSTree Search(BSTree T,int e)//二叉排序树的查找
{
if(!T||T->data==e) return T;
else if(e< T->data) return Search(T->lchild,e);
else if(e> T->data) return Search(T->rchild,e);
}
int Depth(BSTree T)//求二叉树的深度
{
if(T==NULL) return 0;
else
{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
return m>n?m+1:n+1;
}
}
int Count(BSTree T)//叶子结点数
{
if(!T) return 0;//边界条件不能漏
else
{
if(T->lchild||T->rchild)
return Count(T->lchild)+Count(T->rchild);
if(T->lchild==NULL&&T->rchild==NULL) return 1;
}
}
BSTree Change(BSTree &T)//交换各结点的左右子树
{
if(T)
{
if(T->lchild||T->rchild)
{
BSTree p,q;
p=Change(T->lchild);
q=Change(T->rchild);
T->lchild=q;
T->rchild=p;
}
}
return T;
}
int main()
{
//freopen("data.txt","r",stdin);
int n;
cin>>n;
BSTree T;
int x,y,z;
CreateBSTree(T,n);
Pre(T);cout<<endl;
In(T);cout<<endl;
Post(T);cout<<endl;
cin>>x>>y>>z;
if(Search(T,x)) cout<<"1"<<endl;
else cout<<"0"<<endl;
if(Search(T,y)) cout<<"1"<<endl;
else cout<<"0"<<endl;
Insert(T,z);
Pre(T);cout<<endl;
In(T);cout<<endl;
Post(T);cout<<endl;
Inf(T);cout<<endl;
Floor(T);cout<<endl;
Change(T);
Pre(T);cout<<endl;
In(T);cout<<endl;
Post(T);cout<<endl;
Change(T);
Pre(T);cout<<endl;
In(T);cout<<endl;
Post(T);cout<<endl;
cout<<Depth(T)<<endl;
cout<<Count(T)<<endl;
return 0;
}
/*
输入:
5
7 4 9 5 8
5
6
2
输出:
7 4 5 9 8
4 5 7 8 9
5 4 8 9 7
1
0
7 4 2 5 9 8
2 4 5 7 8 9
2 5 4 8 9 7
2 4 5 7 8 9
7 4 9 2 5 8
7 9 8 4 5 2
9 8 7 5 4 2
8 9 5 2 4 7
7 4 2 5 9 8
2 4 5 7 8 9
2 5 4 8 9 7
3
3
*/