#include<iostream>
#include<malloc.h>
#include<math.h>
#define keytype int
#define MAX_NUMBER -999999
#define MIN_NUMBER 999999
using namespace std;
typedef struct BiTNode{
keytype data;
struct BiTNode * lchild;
struct BiTNode * rchild;
}BiTNode,*BiTree;
class BinarySortTree{
public:
BiTree SearchBSF(BiTree T,keytype key){
if(T==NULL) return NULL;
if(T->data==key) return T;
if(T->data < key) return SearchBSF(T->rchild,key);
if(T->data > key) return SearchBSF(T->lchild,key);
};
//----------find the max element in the tree and return it--------------
int SearchMax(BiTree T){
if(T==NULL) return MAX_NUMBER;
if(T->lchild==NULL && T->rchild==NULL)
return T->data;
//-----------T!=NULL and its children are not null,we have to compare the three number-
return max(T->data,SearchMax(T->rchild));
}
//----------find the minimum element in the tree and return it-----------
int SearchMin(BiTree T){
if(T==NULL) return MIN_NUMBER;
if(T->lchild==NULL && T->rchild==NULL)
return T->data;
//-----------T!=NULL and its children are not null,we have to compare the three number-
return min(T->data,SearchMin(T->lchild));
}
//--------we have to return p, so we use BiTree -----------------
//if key is in the tree,then return the node.else return the pre-node-
BiTree SearchBSF_info(BiTree T,keytype key,BiTree f){
BiTree p;
if(T==NULL) {
p=f;
return NULL;
}
if(T->data==key){
p=T;
return p;
}
if(T->data < key) return SearchBSF_info(T->rchild,key,T);
if(T->data > key) return SearchBSF_info(T->lchild,key,T);
}
int SearchBSF_info_version2(BiTree T,keytype key,BiTree f,BiTree * p){
if(T==NULL) {
(*p)=f;
return 0;
}
if(T->data==key){
(*p)=T;
return 1;
}
if(T->data < key) return SearchBSF_info_version2(T->rchild,key,T,p);
if(T->data > key) return SearchBSF_info_version2(T->lchild,key,T,p);
}
int InsertBSF(BiTree *T,keytype key){//we have to return T,so it should be the pointer of the pointer-----------
BiTree * p=new BiTree;//p should be freed
if(!SearchBSF_info_version2(*T,key,NULL,p)){
cout<<"this number is not in the tree, and now we insert it"<<endl;
BiTree s=new BiTNode;//surely,you can create it by malloc in C;
s->data=key;s->lchild=NULL;s->rchild=NULL;
if((*p)==NULL) (*T)=s;
if((*p)->data < key) (*p)->rchild=s;
if((*p)->data > key) (*p)->lchild=s;
return 1;
}else{
cout<<"the number is already in the tree"<<endl;
return 0;
}
}
int DeleteBSF(BiTree * T,keytype key){
if(!(*T)) return 0;
if((*T)->data==key) return Delete(T);
int result;
BiTree * temp=new BiTree;
if((*T)->data < key){
temp=&(*T)->lchild;
result=DeleteBSF(temp,key);
}
if((*T)->data > key){
temp=&(*T)->rchild;
result=DeleteBSF(temp,key);
}
delete temp;//free the memory
return result;
}
int Delete(BiTree *p){
if((*p)->lchild==NULL && (*p)->rchild==NULL);
if((*p)->rchild==NULL){
BiTree q=new BiTNode;
q=(*p);
(*p)=(*p)->lchild;
delete q;
}
else if((*p)->lchild==NULL){
BiTree q=new BiTNode;
q=(*p);
(*p)=(*p)->rchild;
delete q;
}
else{
BiTree q=new BiTNode;BiTree s=new BiTNode;
q=(*p);s=(*p)->lchild;
while(s->rchild){
q=s;
s=s->rchild;
}
(*p)->data=s->data;
if(q!=(*p)) q->rchild=s->lchild;
else q->lchild=s->lchild;
delete s;
}
return 1;
}
};
int main(){
//----------------build a tree, 9.7----------------
BiTree a,b,c,d,e,f,g,h,i,j;
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
h=(BiTree)malloc(sizeof(BiTNode));
i=(BiTree)malloc(sizeof(BiTNode));
j=(BiTree)malloc(sizeof(BiTNode));
a->data=45;a->lchild=b;a->rchild=c;
b->data=12;b->lchild=d;b->rchild=e;
c->data=53;c->lchild=NULL;c->rchild=f;
d->data=3;d->lchild=NULL;d->rchild=NULL;
e->data=37;e->lchild=g;e->rchild=NULL;
f->data=100;f->lchild=h;f->rchild=NULL;
g->data=24;g->lchild=NULL;g->rchild=NULL;
h->data=61;h->lchild=NULL;h->rchild=i;
i->data=90;i->lchild=j;i->rchild=NULL;
j->data=78;j->lchild=NULL;j->rchild=NULL;
//----------------build a tree, 9.7----------------
/* BiTNode a,b,c,d,e,f,g,h,i,j;
a.data=45;a.lchild=&b;a.rchild=&c;
b.data=12;b.lchild=&d;b.rchild=&e;
c.data=53;c.lchild=NULL;c.rchild=&f;
d.data=3;d.lchild=NULL;d.rchild=NULL;
e.data=37;e.lchild=&g;e.rchild=NULL;
f.data=100;f.lchild=&h;f.rchild=NULL;
g.data=24;g.lchild=NULL;g.rchild=NULL;
h.data=61;h.lchild=NULL;h.rchild=&i;
i.data=90;i.lchild=&j;i.rchild=NULL;
j.data=78;j.lchild=NULL;j.rchild=NULL;*/
// pre_order_traverse(a);//output should be 45 12 3 37 24 53 100 61 90 78
BinarySortTree bsf;
// BiTree temp=bsf.SearchBSF(a,90);
// cout<< temp->data << endl;
// BiTree temp=bsf.SearchBSF_info(a,53,NULL);
// cout<<" "<<temp->data<<endl;
// BiTree * p=new BiTree;//here is very import,due p is pointer,so don't forget to allocate
// //memory for it
// int temp1=bsf.SearchBSF_info_version2(a,98,NULL,p);
// if(temp1==1)
// cout<<"this number is in the tree"<<endl;
// else{
// cout<<"it's not in the tree"<<endl;
// cout<<"the parent node's value is :"<<(*p)->data<<endl;
// }
// delete p;//free this pointer
/* int max=bsf.SearchMax(a);
int min=bsf.SearchMin(a);
cout<<"the max number is:"<<max<<endl;
cout<<"the min number is:"<<min<<endl;*/
/* BiTree * T=&a;
bsf.InsertBSF(T,98);*/
free(a);free(b);free(c);free(d);free(e);free(f);
free(g);free(h);free(i);free(j);
return 1;
}