用二叉搜索树实现简单字典

在上一篇博客中,我们介绍了二叉搜索树的基本概念和结构,那么二叉搜索树有哪些应用呢?我们可以用二叉搜索树的K V模型实现一个简单字典模型。
http://blog.csdn.net/chaseraod/article/details/78323622

#include<iostream>
#include<queue>
#include<string>
using namespace std;

template<class K,class V>
class BinaraySearchNode
{
public:
    BinaraySearchNode<K,V>* _left;
    BinaraySearchNode<K,V>* _right;
    BinaraySearchNode<K,V>* _parent;
    K  _key;
    V _value;

    BinaraySearchNode(const K& key,const V& value)
        :_left(NULL)
        , _right(NULL)
        , _parent(NULL)
        , _key(key)
        , _value(value)
    {}
};

template<class K,class V>
class BinarySearchTree
{
    typedef BinaraySearchNode<K, V> Node;
public:
    BinarySearchTree()
        :_root(NULL)
    {}

    //拷贝构造
    BinarySearchTree(const BinarySearchTree<K, V>& bs)
        :_root(NULL)
    {
        if (bs._root == NULL)
        {
            return;
        }
        else
        {
            queue<Node*> q;
            q.push(bs._root);
            while (!q.empty())
            {
                Node* cur = q.front();
                InSert(cur->_key);
                q.pop();
                if (cur->_left)
                    q.push(cur->_left);
                if (cur->_right)
                    q.push(cur->_right);
            }
        }
    }

    //赋值运算符
    BinarySearchTree<K, V>& operator = (const BinarySearchTree<K, V>& bs)
    {
        BinarySearchTree<K, V> tmp(bs);
        swap(_root, bs._root);
        return *this;
    }

    ~BinarySearchTree()
    {
        _Destroy(_root);
    }

    void _Destroy(Node* root)
    {
        if (root == NULL)
            return;
        _Destroy(root->_left);
        _Destroy(root->_right);
        delete root;
    }

    //插入
    bool InSert(const K& key,const V& value)
    {
        return _InSert(_root, key, value);
    }

    bool _InSert(Node*& root, const K&key, const V& value)
    {
        if (root == NULL)
        {
            root = new Node(key, value);
            return true;
        }
        Node* parent = NULL;
        Node* cur = _root;
        while (cur)
        {
            if (cur->_key > key)
            {
                parent = cur;
                cur = cur->_left;
            }
            else if (cur->_key < key)
            {
                parent = cur;
                cur = cur->_right;
            }
            else
                return false;
        }
        Node* tmp = new Node(key, value);
        if (parent->_key > key)
            parent->_left = tmp;
        else if (parent->_key < key)
            parent->_right = tmp;
        return true;
    }

    //查找
    Node* Find(const K& key)
    {
        Node* cur = _root;
        while (cur)
        {
            if (cur->_key > key)
                cur = cur->_left;
            else if (cur->_key < key)
                cur = cur->_right;
            return cur;
        }
        return NULL;
    }

    //删除节点
    bool Remove(const K& key)
    {
        if (_root == NULL)
            return false;
        Node* parent = NULL;
        Node* cur = _root;
        while (cur)
        {
            if (key > cur->_key)
            {
                parent = cur;
                cur = cur->_right;
            }
            else if (key < cur->_key)
            {
                parent = cur;
                cur = cur->_left;
            }
            else//等于
            {
                if (cur->_left == NULL)
                {
                    parent = cur->_parent;
                    if (parent == NULL)
                    {
                        _root = cur->_right;
                        delete cur;
                        cur = NULL;
                        return true;
                    }
                    if (cur == parent->_left)
                    {
                        parent->_left = cur->_right;
                    }
                    else
                    {
                        parent->_right = cur->_right;
                    }
                    delete cur;
                    cur = NULL;
                }
                if (cur->_right == NULL)
                {
                    parent = cur->_parent;
                    if (parent == NULL)
                    {
                        _root = cur->_left;
                        delete cur;
                        cur = NULL;
                        return true;
                    }
                    if (cur == parent->_left)
                    {
                        parent->_left = cur->_left;
                    }
                    else
                    {
                        parent->_right = cur->_left;
                    }
                    delete cur;
                    cur = NULL;
                }
                if (cur->_left != NULL && cur->_right != NULL)
                {
                    //替换法(注意根节点为空的情况)
                    Node* subRight = cur->_right;
                    Node* subParent = cur;
                    while (subRight->_left)
                    {
                        subParent = subRight;
                        subRight = subRight->_left;
                    }

                    cur->_key = subRight->_key;
                    if (subParent->_right == subRight)
                        subParent->_right = subRight->_right;
                    else
                        subParent->_left = subRight->_left;
                    delete subRight;
                    subRight = NULL;
                }
                return  true;
            }
        }
        return false;
    }

    //递归插入
    bool _InSertR(Node*& root, const K& key)//递归插入
    {
        if (root == NULL)
            return root = new Node(key);
        if (root->_key < key)
            return _InSertR(root->right, key);
        if (root->_key > key)
            return _InSertR(root->_left, key);
    }


    //递归删除
    bool _RemoveR(Node*& root, const K& key)//注意:这里要加引用
    {
        if (root == NULL)
            return false;
        if (root->_left == NULL && root->_right == NULL)
        {
            if (root->_key == key)
            {
                delete root;
                return true;
            }
            else
                return false;
        }
        if (root->_key > key)
            RemoveR(root->_left, key);
        if (root->_key < key)
            RemoveR(root->_right, key);
        else
        {
            Node* cur = root;
            if (cur->_left == NULL)
            {
                parent = cur->_parent;
                if (parent == NULL)
                {
                    root = root->_right;
                    delete cur;
                    cur = NULL;
                }
                if (cur == parent->_left)
                {
                    parent->_left = cur->_right;
                }
                else
                {
                    parent->_right = cur->_right;
                }
                delete cur;
                cur = NULL;
            }
            if (cur->_right == NULL)
            {
                parent = cur->_parent;
                if (parent == NULL)
                {
                    _root = cur->_left;
                    delete cur;
                    cur = NULL;
                    return true;
                }
                if (cur == parent->_left)
                {
                    parent->_left = cur->_left;
                }
                else
                {
                    parent->_right = cur->_left;
                }
                delete cur;
                cur = NULL;
            }
            if (cur->_left != NULL && cur->_right != NULL)
            {
                //替换法
                Node* subRight = cur->_right;
                Node* subParent = cur;
                while (subRight->_left)
                {
                    subParent = subRight;
                    subRight = subRight->_left;
                }

                cur->_key = subRight->_key;
                if (subParent->_right == subRight)
                    subParent->_right = subRight->_right;
                else
                    subParent->_left = subRight->_left;
                delete subRight;
                subRight = NULL;
            }
            return  true;
        }
    }

    //中序遍历
    void InOrder()
    {
        _InOrder(_root);
    }
protected:
    void _InOrder(Node* root)
    {
        if (root == NULL)
            return;
        else
        {
            _InOrder(root->_left);
            cout << root->_key << ":"<<root->_value<< " ";
            _InOrder(root->_right);
        }
    }
private:
    Node* _root;
};

void Test()
{
    BinarySearchTree<string, string> b;
    b.InSert("sort", "排序");
    b.InSert("tree", "树");
    b.InSert("string", "字符串");
    b.InSert("search", "搜索");
    b.InOrder();
    //查找单词
    BinaraySearchNode<string, string> *ret = b.Find("sort");
    cout << ret->_value << endl;

    //int a[] = { 5, 3,1,0,4,7,6,8,9,2};
    /*for (size_t i = 0; i < (sizeof(a) / sizeof(a[0])); ++i)
    {
        bs.InSert(a[i],);
    }*/

    //统计单词出现的次数
    string arr[] = { "nihao", "hello", "good", "good", "good", "hello" };
    BinarySearchTree<string, int> CountTree;
    for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i)
    {
        BinaraySearchNode<string, int> *Node = CountTree.Find(arr[i]);
        if (Node)
        {
            Node->_value += 1;
        }
        else
        {
            CountTree.InSert(arr[i],1);
        }
    }

    //bs.InOrder();
    //cout << endl;
    //bs.Remove(5);
    //bs.InOrder();
};
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值