您的位置:首页 > 其它

BST(Binary Search Tree,二叉查找树,二叉排序树)c的实现(部分函数不知如何调用)

2013-07-30 10:33 726 查看
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct node
{
int key;

struct node *LChild,*RChild,*Parent;
}BSTNode,*BSTree;

void CreatBST(BSTree *bst);
BSTree SearchBST(BSTree bst,int key) ;
void InsertBST(BSTree *bst,int key) ;
void InsertBST2(BSTree *bst,int key) ;
BSTNode * DelBST(BSTree t,int k) ;
BSTNode * DelBST2(BSTree t,BSTNode *z) ;//主函数如何调用?
BSTNode * TreeSuccessor(BSTNode *x);//寻找后继 主函数如何调用?

void print_bst(BSTree t) {
if (t!=NULL)
{
print_bst(t->LChild);
printf(" %d ", t->key);
print_bst(t->RChild);
}
}

const int n = 10;

/*Creat new tree*/
void CreatBST(BSTree *bst)
{
int i;
int key;
*bst=NULL;
cout<<"请输入n(n=10)个数:"<<endl;
scanf("%d",&key);
InsertBST2(bst,key);

for(i=2;i <=n;i++)
{
scanf("%d",&key);
InsertBST2(bst,key);
}
cout<<"创建成功!"<<endl;
//return bst;
}

/*Search*/ //递归版本
BSTree SearchBST(BSTree bst,int key)
{
if(bst==NULL) {
cout<<"cannot find it"<<endl;
return NULL;
}
else
if(bst->key==key)
{
cout<<"find it:\t"<<bst->key<<endl;
return bst;
}
else
if(key <bst->key)
return SearchBST(bst->LChild, key);
else
return SearchBST(bst->RChild, key);
}

/*Search*/ //非递归版本
BSTree SearchBST2(BSTree bst,int key) {
while(bst!=NULL&&bst->key!=key){
if(key<bst->key)
bst=bst->LChild;
else
bst=bst->RChild;
}
return bst;
}

/*Insert*/ //递归版本
void InsertBST(BSTree *bst,int key)
{
BSTree t;
if(*bst==NULL)
{
t=(BSTree)malloc(sizeof(BSTNode));
t->key=key;
t->LChild=NULL;
t->RChild=NULL;
//t->Parent=NULL;
*bst=t;
}
else
if(key <(*bst)->key) {
InsertBST(&((*bst)->LChild),key);
//(*bst)->LChild->Parent=*bst;
}
else
if(key>(*bst)->key){
InsertBST(&(*bst)->RChild,key);
//(*bst)->RChild=*bst;
}
}
//insert 非递归版本

void InsertBST2(BSTree *bst,int key) {
BSTNode *y=NULL;
BSTNode *x=*bst;
BSTree t;

t=(BSTree)malloc(sizeof(BSTNode));
t->key=key;
t->LChild=NULL;
t->RChild=NULL;
t->Parent=NULL;
while(x!=NULL){
y=x;//y指向x的前驱
if(key<x->key)
x=x->LChild;
else
x=x->RChild;
}
t->Parent=y;
//永远不执行,与上面的if重复
if(y==NULL)
*bst=t;
else
if(key<y->key)
y->LChild=t;
else
y->RChild=t;
//}

}

/*Delet*/ //递归版本
BSTNode * DelBST(BSTree t,int k)
{
BSTNode *p,*f,*s,*q;
p=t;
f=NULL;
while(p)
{
if(p->key==k)
break;
f=p;
if(p->key>k)
p=p->LChild;
else
p=p->RChild;
}
if(p==NULL)
return t;
if(p->LChild==NULL)
{
if(f==NULL)
t=p->RChild;
else
if(f->LChild==p)
f->LChild=p->RChild;
else
f->RChild=p->LChild;
free(p);
}
else
{
q=p;
s=s->LChild;
while(s->RChild)
{
q=s;
s=s->RChild;
}
if(q==p)
q->LChild=s->LChild;
else
q->RChild=s->LChild;
p->key=s->key;
free(s);
}
return t;
}

/*Delet*/ //非递归版本
BSTNode * DelBST2(BSTree t,BSTNode *z) {
BSTNode *x=NULL;//x被置为y的非NULL子女,若y无子女时,x被置为NULL
BSTNode *y=NULL;//y或者是输入结点z(若z至多只有1个子女),或者是z的后继(若z有2个子女)
if(z->LChild==NULL||z->RChild==NULL)//z至多有一个子女
y=z;
else
y=TreeSuccessor(z);//z有2个子女

if(y->LChild!=NULL)
x=y->LChild;
else
x=y->RChild;
if(x!=NULL)
x->Parent=y->Parent;
if(y->Parent==NULL)
t=x;
else if(y==y->Parent->LChild)
y->Parent->LChild=x;
else
y->Parent->RChild=x;
if(y!=z)
z->key=y->key;
return y;
}

//return 一个指向以结点t为根的子树中最小元素的指针
BSTNode * TreeMin(BSTree t){
while(t->LChild!=NULL)
t=t->LChild;
return t;
}

//return 一个指向以结点t为根的子树中最大元素的指针
BSTNode * TreeMax(BSTree t){
while(t->RChild!=NULL)
t=t->RChild;
return t;
}

BSTNode * TreeSuccessor(BSTNode *x){//寻找后继,返回指向后继的指针
if(x->RChild!=NULL)
return TreeMin(x->RChild);
BSTNode *y=x->Parent;
while(y!=NULL&&x==y->RChild){
x=y;
y=y->Parent;
}
return y;
}
/*Main*/ // 0 1 3 2 6 4 5 9 7 8
int main()
{
BSTNode * root; //定义指向任意结点的
BSTree  temp;//一般用来定义头指针变量
//BSTNode * temp;
int loop,i,data;
loop=true;
while(loop)
{
printf("\n\n\n");
printf(" 1.Creat\n");
printf(" 2.Search\n");
printf(" 3.Insert\n");
printf(" 4.Delete\n");
printf(" 5.Print\n");
printf(" 6.Min\n");
printf(" 7.Max\n");
printf(" 0.exit  main\n");
scanf("%d",&i);
switch(i)
{
case 0: {
loop=false;
break;
}
case 1:
{
CreatBST(&root);

}break;
case 2:
{
printf("Please input the data you want search.\n");
scanf("%d",&data);
temp=SearchBST2(root,data);
if(temp!=NULL){
cout<<"find it:"<<temp->key<<endl;
}
else
cout<<"cannot find it"<<endl;

}break;
case 3:
{
printf("Please input the data you want insert.\n");
scanf("%d",&data);
InsertBST(&root,data);
}break;
case 4:
{
printf("Please input the data you want delete.\n");
scanf("%d",&data);
root=DelBST(root,data);
}break;
case 5:{
printf("\n");
if (root!=NULL)
printf("The BSTree's root is:%d\n",root->key);
print_bst(root);
break;

}
case 6:{
temp=TreeMin(root);
cout<<"最小值:"<<temp->key<<endl;
break;

}
case 7:{
temp=TreeMax(root);
cout<<"最大值:"<<temp->key<<endl;
break;

}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: