(49条消息) SCAU------8608 实现二叉排序树的各种算法(2)_是zg啊!的博客-CSDN博客 照着打的,记录用的。 用函数实现如下二叉排序树算法: (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<algorithm>
using namespace std;
typedef struct BSTNode{
int data;
BSTNode*lchild,*rchild;
}*BSTree;
void Insert(BSTree&T,int e){
if(!T){
BSTree S=new BSTNode;
S->data=e;
S->lchild=S->rchild=NULL;
T=S;
return;
}else{
if(e<T->data){
Insert(T->lchild,e);
}else{
Insert(T->rchild,e);
}
}
}
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 Inf(BSTree T){
BSTree p=T;
stack<BSTree>s;
while(p||!s.empty()){
while(p){
s.push(p);
p=p->lchild;
}
if(!s.empty()){
p=s.top();
s.pop();
cout<<p->data<<' ';
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();
}
}
int Search(BSTree T,int e){
if(!T){
return 0;
}else if(T->data==e){
return 1;
}else{
if(e< T->data){
return Search(T->lchild,e);
}else{
return Search(T->rchild,e);
}
}
}
int Depth(BSTree T){
if(!T){
return 0;
}else{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
return m>=n?m+1:n+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 Count(BSTree T){
if(!T){
return 0;
}else if(T->lchild==NULL&&T->rchild==NULL){
return 1;
}else{
return Count(T->lchild)+Count(T->rchild);
}
}
int main() {
int n,e;
BSTree T;
T=NULL;
cin>>n;
for(int i=1;i<=n;++i){
cin>>e;
Insert(T,e);
}
Pre(T);
cout<<endl;
In(T);
cout<<endl;
Post(T);
cout<<endl;
cin>>e;
cout<<Search(T,e)<<endl;
cin>>e;
cout<<Search(T,e)<<endl;
cin>>e;
Insert(T,e);
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;
}