C++ 实现二叉搜索树

#ifndef Dictionary_h
#define Dictionary_h


template<typename Key,typename E>
class Dictionary
{
public:
    Dictionary(){}
    ~Dictionary(){}
    virtual void clear()=0;
    virtual void insert(Key& k,E& e)=0;
    virtual E    remove(Key k)=0;
    virtual E    find(Key k)=0;
    virtual E    removeAny()=0;
    virtual int  size()=0;
};


#endif /* Dictionary_h */


#ifndef BinNode_h
#define BinNode_h


template<typename E>
class BinNode
{
public:
    BinNode(){}
    ~BinNode(){}
    virtual BinNode* left()=0;
    virtual void     setLeft(BinNode* it)=0;
    virtual BinNode* right()=0;
    virtual void     setRight(BinNode* it)=0;
    virtual E&       element()=0;
    virtual void     setElement(E& it)=0;
    virtual bool     isLeaf()=0;
};


#endif /* BinNode_h */


#include"Dictionary.h"
#include"BinNode.h"
#include<iostream>
#include<cstring>
#include<cstdlib>


using namespace std;


template<typename Key,typename E>
class BSTnode: public BinNode<E>
{
private:
    BSTnode* lp;
    BSTnode* rp;
    E it;
    Key k;
public:
    BSTnode(Key& kk,E& e,BSTnode* l,BSTnode* r)
    {
        lp=l;
        rp=r;
        it=e;
        k=kk;
    }
    ~BSTnode(){}
    BSTnode* left()
    {
        return lp;
    }
    void setLeft(BinNode<E>* it)
    {
        lp=(BSTnode*)it;//强制类型转换
    }
    BSTnode* right()
    {
        return rp;
    }
    void setRight(BinNode<E>* it)
    {
        rp=(BSTnode*)it;
    }
    E& element()
    {
        return it;
    }
    void setElement(E& e)
    {
        it=e;
    }
    bool isLeaf()
    {
        if(lp==NULL&&rp==NULL)
            return true;
        return false;
    }
    Key& key()
    {
        return k;
    }
    void setKey(Key& kk)
    {
        k=kk;
    }
};


template<typename Key,typename E>
class BST: public Dictionary<Key,E>
{
private:
    BSTnode<Key,E>* root;
    int length;
    void clearHelp(BSTnode<Key,E>* root)
    {
        if(root==NULL)
            return;
        clearHelp(root->left());
        clearHelp(root->right());
        delete root;
    }
    BSTnode<Key,E>* insertHelp(BSTnode<Key,E>* root,Key& k,E& e)
    {
        if(root==NULL)
        {
            return (new BSTnode<Key,E>(k,e,NULL,NULL));
        }
        if(k<root->key())
        {
            root->setLeft(insertHelp(root->left(),k,e));
        }
        if(k>root->key())
        {
            root->setRight(insertHelp(root->right(),k,e));
        }
        return root;
    }
    BSTnode<Key,E>* deleteMin(BSTnode<Key,E>* root)
    {
        if(root==NULL)
        {
            return NULL;
        }
        if(root->left()!=NULL)
        {
            root->setLeft(deleteMin(root->left()));
        }
        else
            return root->right();
        return root;
    }
    BSTnode<Key,E>* getMin(BSTnode<Key,E>* root)
    {
        if(root==NULL)
        {
            return root;
        }
        if(root->left()!=NULL)
        {
            return getMin(root->left());
        }
        else
            return root;
    }
    E& findHelp(BSTnode<Key,E>* root,Key k)
    {
        static E temp;
        if(root==NULL)
        {
            return temp;
        }
        if(k<root->key())
        {
            findHelp(root->left(),k);
        }
        else if(k>root->key())
        {
            findHelp(root->right(),k);
        }
        else
            return root->element();
        return temp;
    }
    void printHelp(BSTnode<Key,E>* root)
    {
        if(root==NULL)
            return;
        printHelp(root->left());
        printHelp(root->right());
        cout<<root->key()<<" ";
        
    }
    BSTnode<Key,E>* removeHelp(BSTnode<Key,E>* root,Key k)
    {
        if(root==NULL)
            return root;
        if(k<root->key())
        {
            root->setLeft(removeHelp(root->left(),k));
        }
        else if(k>root->key())
        {
            root->setRight(removeHelp(root->right(),k));
        }
        else
        {
            BSTnode<Key,E>* temp=root;
            if(root->left()==NULL)
            {
                return root->right();
                delete temp;
            }
            else if(root->right()==NULL)
            {
                return root->left();
                delete temp;
            }
            else
            {
                BSTnode<Key,E>* min=getMin(root->right());
                root->setKey(min->key());
                root->setElement(min->element());
                root->setRight(deleteMin(root->right()));
                min=NULL;
                return root;
            }
        }
        return root;
    }
    
public:
    BST()
    {
        root=NULL;
        length=0;
    }
    ~BST()
    {
        clear();
    }
    void clear()
    {
        clearHelp(root);
    }
    void insert(Key& k,E& e)
    {
        root=insertHelp(root,k,e);
        length++;
    }
    E remove(Key k)//这个删除任意与k匹配的节点
    {
        E temp=find(k);
        if(root!=NULL)
        {
        root=removeHelp(root,k);
        length--;
            return temp;
        }
        else
            return 0;
    }
    E find(Key k)
    {
        return findHelp(root,k);
    }
    E removeAny()//这个是没有给k值删除根节点,它没有参数
    {
        Key k=root->key();
        E temp=root->element();
        root=removeHelp(root,k);
        return temp;
    }
    int size()
    {
        return length;
    }
    void print()
    {
        printHelp(root);
    }
};


int main()
{
    int array[]={54,76,23,78,44,35,2,567,24};
    int N=sizeof(array)/sizeof(array[0]);
    BST<int,int> a;
    for(int i=0;i<N;i++)
    {
        a.insert(array[i],array[i]);
    }
    a.print();
    cout<<endl;
    a.removeAny();
    a.print();
    cout<<endl;
    a.remove(44);
    a.print();
    cout<<endl;
    int n=55;
    a.insert(n,n);
    a.print();
    cout<<endl;
    return 0;
}
    





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值