#include"iostream"
using namespace std;
struct BST
{
int v;
BST *Ls,*Rs;
};
BST *Find(BST *u,int v)
{
if(u==NULL)return NULL;
if(u->v>v)return Find(u->Ls,v);
else if(u->v<v)return Find(u->Rs,v);
else return u;
}
bool Insert(BST *&u,int v)
{
if(u==NULL)
{
u=new BST;
u->v=v;
u->Ls=u->Rs=NULL;
return true;
}
else if(u->v>v)return Insert(u->Ls,v);
else if(u->v<v)return Insert(u->Rs,v);
return false;
}
BST *findmin(BST *u)
{
if(u==NULL)return NULL;
else if(u->Ls==NULL)return u;
else return findmin(u->Ls);
}
BST *findmax(BST *u)
{
if(u==NULL)return NULL;
else if(u->Rs==NULL)return u;
else return findmax(u->Rs);
}
bool Delete(BST *&u,int v)
{
if(u==NULL)return false;
else if(u->v>v)return Delete(u->Ls,v);
else if(u->v<v)return Delete(u->Rs,v);
else
{
if(u->Ls&&u->Rs)
{
BST *temp=findmax(u->Ls);
u->v=temp->v;
return Delete(temp,temp->v);
}
else
{
if(u->Ls)u=u->Ls;
else if(u->Rs)u=u->Rs;
return true;
}
}
}
void PreOrderTraverse(BST *T) //递归前序
{
if(T==NULL)
return;
printf("%d ",T->v);
PreOrderTraverse(T->Ls);
PreOrderTraverse(T->Rs);
}
int main()
{
BST *root=NULL;
Insert(root,5);
Insert(root,6);
Insert(root,7);
Insert(root,8);
Insert(root,9);
Insert(root,10);
// BST *&p=root;
// p=p->Rs;
// BST *pp=root;
// cout<<pp->v<<"\n";
BST *p=root;
p->v=9;
cout<<root->v<<"\n";
}
BST
最新推荐文章于 2024-05-13 21:13:38 发布