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
    评论
可以参考下面的代码实现: ```cpp #include <iostream> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class BST { public: BST() : root(NULL) {} void insert(int val) { if (!root) { root = new TreeNode(val); return; } TreeNode* cur = root; while (cur) { if (val < cur->val) { if (cur->left) { cur = cur->left; } else { cur->left = new TreeNode(val); return; } } else { if (cur->right) { cur = cur->right; } else { cur->right = new TreeNode(val); return; } } } } bool search(int val) { TreeNode* cur = root; while (cur) { if (val == cur->val) { return true; } else if (val < cur->val) { cur = cur->left; } else { cur = cur->right; } } return false; } void inorderTraversal() { inorderTraversalHelper(root); cout << endl; } private: void inorderTraversalHelper(TreeNode* node) { if (!node) { return; } inorderTraversalHelper(node->left); cout << node->val << " "; inorderTraversalHelper(node->right); } private: TreeNode* root; }; int main() { int arr[] = {4, 5, 19, 23, 2, 8}; int n = sizeof(arr) / sizeof(arr[0]); BST bst; for (int i = 0; i < n; i++) { bst.insert(arr[i]); } bst.inorderTraversal(); cout << bst.search(5) << endl; cout << bst.search(7) << endl; return 0; } ``` 输出结果: ``` 2 4 5 8 19 23 1 0 ``` 其中,`insert` 方法用于向二叉搜索树中插入一个数;`search` 方法用于查找二叉搜索树中是否存在某个数;`inorderTraversal` 方法用于中序遍历二叉搜索树并输出每个节点的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值