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>
using namespace std;
typedef struct BSTNode
{
int data;
struct BSTNode *lchild, *rchild;
} BSTNode, *BSTree;
void InsertBST(BSTree &T, int e) //(1) 插入新结点
{
if (!T)
{
BSTree S;
S = new BSTNode;
S->data = e;
S->lchild = S->rchild = NULL;
T = S;
}
else if (e < (T->data))
{
InsertBST(T->lchild, e);
}
else if (e > (T->data))
{
InsertBST(T->rchild, e);
}
}
void PreOrderTraverse(BSTree T)//(2)递归前序遍历
{
if (T == NULL) return;
cout << (T->data) << ' ';
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
void PreOrderTraverse2(BSTree T)//(3)非递归前序遍历
{
stack<BSTree> S;
BSTree p = T;
while (p)
{
cout << p->data << ' ';
S.push(p);
p = p->lchild;
}
while (!S.empty())
{
p = S.top();
S.pop();
p = p->rchild;
while (p)
{
cout << p->data << ' ';
S.push(p);
p = p->lchild;
}
}
cout << endl;
}
void InOrderTraverse(BSTree T)//(2)递归中序遍历
{
if (T == NULL) return;
InOrderTraverse(T->lchild);
cout << (T->data) << ' ';
InOrderTraverse(T->rchild);
}
void InOrderTraverse2(BSTree T)//(3)非递归中序遍历
{
stack<BSTree> S;
BSTree p = T;
while (p || !S.empty())
{
if (p)
{
S.push(p);
p = p->lchild;
}
else
{
p = S.top();
S.pop();
cout << p->data << ' ';
p = p->rchild;
}
}
cout << endl;
}
void PostOrderTraverse(BSTree T)//(2)递归后序遍历
{
if (T == NULL) return;
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout << (T->data) << ' ';
}
void PostOrderTraverse2(BSTree T)//(3)非递归后序遍历
{
stack<BSTree> S;
BSTree p = T, pre = NULL;
while (p)
{
S.push(p);
p = p->lchild;
}
while (!S.empty())
{
p = S.top();
if (p->lchild == NULL && p->rchild == NULL //p为叶子节点
|| p->lchild == pre && p->rchild == NULL //上一个节点为p的左子树,且p无右子树
|| p->rchild == pre) //上一个节点为p的右子树
{
cout << p->data << ' ';
pre=p;
S.pop();
}
else
{
p = p->rchild;
while (p)
{
S.push(p);
p = p->lchild;
}
}
}
cout << endl;
}
void BFS(BSTree T) //(4) 层次遍历二叉树
{
queue<BSTree> q;
BSTree p=T;
q.push(p);
while(!q.empty())
{
p=q.front();
cout<<p->data<<' ';
q.pop();
if(p->lchild!=NULL)
q.push(p->lchild);
if(p->rchild!=NULL)
q.push(p->rchild);
}
cout<<endl;
}
int SearchBST(BSTree T,int key) //(5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0)
{
if(!T)
return 0;
else if(key==T->data)
return 1;
else if(key<T->data)
return SearchBST(T->lchild,key);
else
return SearchBST(T->rchild,key);
}
void SwapChild(BSTree &T) //(6) 交换各结点的左右子树
{
if(!T) return;
BSTree tmp=T->lchild;
T->lchild=T->rchild;
T->rchild=tmp;
SwapChild(T->lchild);
SwapChild(T->rchild);
}
int Depth(BSTree T) //(7) 求二叉树的深度
{
if(!T) return 0;
return max(Depth(T->lchild),Depth(T->rchild))+1;
}
int LeafNodeCount(BSTree T) //(8) 叶子结点数
{
if(!T)
return 0;
else if(T->lchild==NULL&&T->rchild==NULL)
return 1;
else
return LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild);
}
int main()
{
BSTree T = NULL;
int n, e;
cin >> n; //输入第一行:准备建树的结点个数n
for (int i = 1; i <= n; ++i)
{
cin >> e; //输入第二行:输入n个整数,用空格分隔
InsertBST(T, e);
}
///输出第一行:二叉树的先序遍历序列
PreOrderTraverse(T);
cout << endl;
//PreOrderTraverse2(T);
///输出第二行:二叉树的中序遍历序列
InOrderTraverse(T);
cout << endl;
//InOrderTraverse2(T);
///输出第三行:二叉树的后序遍历序列
PostOrderTraverse(T);
cout << endl;
//PostOrderTraverse2(T);
int key;
cin>>key; //输入第三行:输入待查找的关键字
cout<<SearchBST(T,key)<<endl; ///输出第四行:查找结果
cin>>key; //输入第四行:输入待查找的关键字
cout<<SearchBST(T,key)<<endl; ///输出第五行:查找结果
cin>>e; //输入第五行:输入待插入的关键字
InsertBST(T,e);
///输出第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列
PreOrderTraverse(T);
cout << endl;
InOrderTraverse(T);
cout << endl;
PostOrderTraverse(T);
cout << endl;
///输出第九行:插入新结点后的二叉树的中序遍历序列(非递归算法)
InOrderTraverse2(T);
///输出第十行:插入新结点后的二叉树的层次遍历序列
BFS(T);
SwapChild(T);
///输出第十一行~第十三行:第一次交换各结点的左右子树后的先、中、后序遍历序列
PreOrderTraverse(T);
cout << endl;
InOrderTraverse(T);
cout << endl;
PostOrderTraverse(T);
cout << endl;
SwapChild(T);
///输出第十四行~第十六行:第二次交换各结点的左右子树后的先、中、后序遍历序列
PreOrderTraverse(T);
cout << endl;
InOrderTraverse(T);
cout << endl;
PostOrderTraverse(T);
cout << endl;
///输出第十七行:二叉树的深度
cout<<Depth(T)<<endl;
///输出第十八行:叶子结点数
cout<<LeafNodeCount(T)<<endl;
return 0;
}