二叉搜索树实现简单字典

日常生活中我们经常使用字典来查找单词的中文意思,也会根据中文来查找所对应的汉语。前面我们知道二叉树实现可以迅速查找一个数据,而且可以插入和删除。
这里我们可以用搜索二叉树实现简单的字典,查找英文单词对应的汉语意思。
问题的描叙:
1:如何判断英文单词是否拼写正确;
2:如何统计单词出现的次数
这里我们可以用二叉树的K V算法;
v(value)是key的附属信息,这里我们可以理解为汉语意思。
一:字典的声明:

struct BinarySearchTreeNode
{
    K _key;
    V _value;//附加信息
    BinarySearchTreeNode<K, V>* _left;
    BinarySearchTreeNode<K, V>* _right;

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

我们可以通过查找算法来实现,来判断单词是否拼写正确:
统计单词出现的次数,我们也可以通过查找来实现。

Node* _Find(Node* root, const K&key)
    {
        Node*cur = root;
        while (cur)
        {
            if (cur->_key < key)//右边
            {
                return _Find(cur->_right, key);
            }
            else if (cur->_key>key)//左边
            {
                return _Find(cur->_left, key);
            }
            else
            {
                return cur;
            }
        }
        return NULL;
    }

二:字典的实现:

#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
template<class K,class V>
struct BinarySearchTreeNode
{
    K _key;
    V _value;//附加信息
    BinarySearchTreeNode<K, V>* _left;
    BinarySearchTreeNode<K, V>* _right;

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

};
template<class K,class V>
class BinarySearchTree
{
    typedef BinarySearchTreeNode<K, V> Node;
public:
    BinarySearchTree()
        :_root(NULL)
    {}
    BinarySearchTree(const BinarySearchTree<K,V>&tree)//拷贝构造
        :_root(NULL)
    {
        if (tree._root == NULL)
        {
            return;
        }
        queue<Node*>q;
        q.push(tree._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 =(BinarySearchTree<K, V>&tree)
    {
        BinarySearchTree<K,V> tmp(tree);
        swap(root, tmp._root);
        return *this;
    }
    ~BinarySearchTree()
    {
        _Destroy(_root);
    }
    Node* Find(const K&key)
    {
       return _Find(_root, key);
    }

    void Insert(const K&key,const V&value)
    {
        _Insert(_root, key, value);
    }

    void InOrder()
    {
          _InOrder(_root);
    }
    void RemoveR(const K&key)
    {
        _RemoveR(_root, key);
    }
protected:
    void _InOrder(Node*root)
    {
        Node*cur = root;
        if (root == NULL)
        {
            return ;
        }
        _InOrder(cur->_left);
        cout << cur->_key << " "<<cur->_value<<endl;
        _InOrder(cur->_right);
    }
    bool _Insert(Node*& root, const K&key,const V&value)
    {
        if (root == NULL)
        {
            root = new Node(key, value);
            return true;
        }
        if (root->_key < key)
        {
            return _Insert(root->_right, key, value);
        }
        else if (root->_key>key)
        {
            return _Insert(root->_left, key, value);
        }
        else
        {
            return NULL;//插入值相等

        }
    }
    Node* _Find(Node* root, const K&key)
    {
        Node*cur = root;
        while (cur)
        {
            if (cur->_key < key)//右边
            {
                return _Find(cur->_right, key);
            }
            else if (cur->_key>key)//左边
            {
                return _Find(cur->_left, key);
            }
            else
            {
                return cur;
            }
        }
        return NULL;
    }
    //递归实现
    bool RemoveR(Node*&root, const K&key)
    {
        if (root == NULL)
        {
            return NULL;
        }
        if (root->_key < key)
        {
            return RemoveR(root->_left, key);
        }
        else if (root->_key>key)
        {
            return RemoveR(root->_left, key);
        }
        else
        {
            if (root->_left == NULL)//左为空
            {
                root = root->_right;
            }
            else if (root->_right == NULL)//右为空
            {
                root = root->_left;
            }
            else
            {
                //左右都不为空
                Node*parent = cur;
                Node*subRight = cur->_right;
                while (subRight->_left)
                {
                    parent = subRight;
                    subRight = subRight->_left;
                }
                parent->_key = subRight->_key;
                if (parent->_right = subRight)
                {
                    parent->_right = subRight->_right;
                }
                else
                {
                    parent->_left = subRight->_right;
                }
                delete subRight;
            }
            return true;//删除成功;
        }
    }
    bool Remove(Node* &root, const K&key)
    {
        if (root == NULL)
        {
            return NULL;
        }
        Node*cur = root;
        Node*parent = NULL;
        while (cur)
        {
            if (cur->_key < key)//右边
            {
                return _Remove(cur->_right, key);
            }
            else if (cur->_key>key)//左边
            {
                return _Remove(cur->_left, key);
            }
            else
            {

                if (cur->_left ==NULL)//左为空
                {
                    if (parent == NULL)
                    {
                        _root = cur->_right;
                    }
                    else
                    {
                        if (parent->_left == cur)
                        {
                            parent->_left = cur->_left;
                        }
                        else
                        {
                            parent->_right = cur->_right;
                        }
                    }
                    delete cur;
                }

                else if (cur->_right == NULL)//右为空
                {
                    if (parent == NULL)
                    {
                        _root = cur->_left;
                    }
                    else
                    {
                        if (parent->_right == cur)
                        {
                            parent->_right = cur->_right;
                        }
                        else
                        {
                            parent->_left = cur->_left;
                        }
                    }
                    delete cur;
                }
                else
                {
                    //左右都不为空(替换法删除)
                    Node*subParent = cur;
                    Node*subRight = cur->_right;
                    while (subRight->_left)
                    {
                        subParent = subRight;
                        subRight = subRight->_left;
                    }
                    subParent->_key = subRight->_key;//交换两个值
                    if (subParent->_right == cur)
                    {
                        subRight->_right = subParent->_right;
                    }
                    else
                    {
                        subRight->_left = subParent->_left;
                    }
                    delete subRight;
                }
                return true;
            }
        }
        return false;//没找到
    }
    void _Destroy(Node*root)
    {
        Node*cur = root;
        while (cur)
        {
            _Destroy(cur->_left);
            _Destroy(cur->_right);
            delete cur;
        }   
    }
protected:
    Node*_root;
};
int main()
{
    BinarySearchTree<string, string> b;
    b.Insert("sort", "排序");
    b.Insert("tree", "树");
    b.Insert("string", "字符串");
    b.Insert("search", "搜索");
    b.InOrder();   
    //查找单词
    BinarySearchTreeNode<string, string> *ret =b.Find("sort");
    cout << ret->_value << endl;
    //统计单词出现的次数
    string a[] = { "string", "string", "string", "string" };
    BinarySearchTree<string,int>  CountTree;
    for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        BinarySearchTreeNode<string, int> *Node = CountTree.Find(a[i]);
        if (Node)
        {
            Node->_value += 1;
        }
        else
        {
            CountTree.Insert(a[i], 1);
        }
    }
    system("pause");
    return 0;
}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供有关二叉搜索树的构建与应用的一些基本知识,并提供一个基于C语言的插入操作来创建二叉搜索树的示例代码。 二叉搜索树(Binary Search Tree,BST)是一种特殊的二叉树,它的每个节点最多有两个子节点,并且每个节点的左子节点的值小于该节点的值,右子节点的值大于该节点的值。通过这种方式,BST的节点可以被方便地插入、删除和查找。 BST常用于实现一些基于有序数据的算法,例如二分查找、范围查找等。在实际应用中,BST也常用于实现字典、集合等数据结构,或者作为其他数据结构的底层实现。 下面是一个基于C语言的插入操作的示例代码,用于创建一个二叉搜索树: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; TreeNode *insert(TreeNode *root, int val) { if (root == NULL) { // 如果当前节点为空,则创建新节点并返回 TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } if (val < root->val) { // 如果插入值小于当前节点值,则插入到左子树中 root->left = insert(root->left, val); } else { // 如果插入值大于等于当前节点值,则插入到右子树中 root->right = insert(root->right, val); } return root; } void inorderTraversal(TreeNode *root) { // 中序遍历输出二叉搜索树 if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } } int main() { TreeNode *root = NULL; // 插入元素 root = insert(root, 5); insert(root, 3); insert(root, 7); insert(root, 2); insert(root, 4); insert(root, 6); insert(root, 8); // 中序遍历输出 inorderTraversal(root); return 0; } ``` 在这个示例代码中,我们定义了一个二叉树节点的结构体,并实现了一个名为`insert`的插入操作。在插入操作中,我们首先检查当前节点是否为空,如果为空则创建一个新节点并返回;如果不为空,则根据插入值与当前节点值的大小关系,递归地插入到左子树或右子树中。 最后,我们在`main`函数中调用`insert`操作插入了一些元素,并使用中序遍历遍历输出了整个二叉搜索树。 希望这个示例代码能够帮助您更好地理解二叉搜索树的构建与应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值