二叉搜索树(BST)的常用操作:建树、插入、查询、删除、排序打印

/*
Author:Ibsen
Data:2015.12.21
*/
//二叉搜索树:建树,插入,删除,排序输出.
#include <iostream>
#include <cstdlib>
using namespace std;
const int M=1000; //数据大小
int A[M]={5,2,1,6,7,4,8,3,9},n=9;
typedef struct node
{
    int key; //关键字
    struct node *lc,*rc;
}BST;
int Insret_BST(BST * &p,int k)
{//在树p中插入关键字k
    if(p==NULL)
    {
        p=new BST();
        p->key=k;
        p->lc=p->rc=NULL;
        return 1;
    }
    else if(p->key==k) return 0;
    else if(p->key>k) return Insret_BST(p->lc,k);
    else return Insret_BST(p->rc,k);
}
BST * Create_BST(int A[],int n)
{//建树
    BST *bt=NULL;
    for(int i=0;i<n;i++)
      Insret_BST(bt,A[i]);
    return bt;
}
BST * Search_BST(BST *bt,int k)
{//查找关键字k并返回其指针
    if(bt==NULL||bt->key==k) return bt;
    if(bt->key>k) return Search_BST(bt->lc,k);
    else return Search_BST(bt->rc,k);
}
void In_Order_BST(BST *bt)
{//中序遍历二叉树:打印排序后结果
    if(bt!=NULL)
    {
        In_Order_BST(bt->lc);
        cout<<bt->key<<" ";
        In_Order_BST(bt->rc);
    }
}
void Delete_BST_Key1(BST *p,BST *&r)
{//当被删除的节点既有左孩子又有有孩子的情况
    BST *tmp;
    if(r->rc!=NULL) Delete_BST_Key1(p,r->rc);//p的左子树中的最大关键字
    else
    {
        p->key=r->key;
        tmp=r; //到这里,删除节点p就等于删除最右下的节点r
        r=r->lc;
        free(tmp);
    }
}
void Delete_BST_Key(BST * &p)
{//删除关键字为k的节点p
    BST *tmp;
    if(p->rc==NULL)
    {//p的右儿子为空,直接将其左儿子放在被删除的节点的位置
        tmp=p;
        p=p->lc;
        free(tmp);
    }
    else if(p->lc==NULL)
    {//p的左儿子为空,直接将其右儿子放在被删除的节点的位置
        tmp=p;
        p=p->rc;
        free(tmp);
    }
    else Delete_BST_Key1(p,p->lc);
    //p既有左孩子又有右孩子的情况,可以用左子树的最大关键字来代替p,也可以用右子树的最小关键字来代替p
}
int Delete_BST(BST * &bt,int k)
{//删除关键字k
    if(bt==NULL) return 0;
    else
    {
        if(bt->key>k) return Delete_BST(bt->lc,k);
        else if(bt->key<k) return Delete_BST(bt->rc,k);
        else
        {
            Delete_BST_Key(bt); //找到要删除的节点并删除
            return 1;
        }
    }
}
int main()
{
    BST *bt=NULL;
    bt=Create_BST(A,n);
    cout<<"The Init Order :";
    for(int i=0;i<n;i++)
        cout<<A[i]<<" ";
    cout<<endl;
    cout<<"The Ascending Order of BSTree: ";
    In_Order_BST(bt); cout<<endl;
    cout<<"Search keys in BSTree:"<<endl;
    int k;
    while(cin>>k&&k)
    {
        if(Search_BST(bt,k)==NULL) cout<<"Search Fail!"<<endl;
        else cout<<"Search Succeed!"<<endl;
    }
    cout<<"Insert keys in BSTree:"<<endl;
    while(cin>>k&&k)
    {
        if(Insret_BST(bt,k))
        {
            cout<<"Insert Succeed!"<<endl<<"The New Order:";
            In_Order_BST(bt); cout<<endl;
        }
        else cout<<"Insert Fail(keys exit)!"<<endl;
    }
    cout<<"Delete keys from BSTree:"<<endl;
    while(cin>>k)
    {
        if(Delete_BST(bt,k))
        {
            cout<<"Delete Succeed!"<<endl<<"The New Order:";
            In_Order_BST(bt); cout<<endl;
        }
        else cout<<"Delete Fail(No keys found)!"<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/AC_Gibson/article/details/50370640
今日推荐