一篇文章教会你什么是二叉搜索树(下)

简介: .二叉搜索树所有代码

9.二叉搜索树所有代码

namespace Key
{
  template<class K>
  struct BSTreeNode
  {
    BSTreeNode<K>* _left;
    BSTreeNode<K>* _right;
    K _key;
    BSTreeNode(const K& key)
      :_left(nullptr)
      , _right(nullptr)
      , _key(key)
    {}
  };
  template<class K>
  class BSTree
  {
    typedef BSTreeNode<K> Node;
  public:
    bool Insert(const K& key)
    {
      if (_root == nullptr)
      {
        _root = new Node(key);
        return true;
      }
      Node* parent = nullptr;
      Node* cur = _root;
      while (cur)
      {
        if (cur->_key < key)
        {
          parent = cur;
          cur = cur->_right;
        }
        else if (cur->_key > key)
        {
          parent = cur;
          cur = cur->_left;
        }
        else
        {
          return false;
        }
      }
      cur = new Node(key);
      if (parent->_key < key)
      {
        parent->_right = cur;
      }
      else
      {
        parent->_left = cur;
      }
      return true;
    }
    bool Find(const K& key)
    {
      Node* cur = _root;
      while (cur)
      {
        if (cur->_key < key)
        {
          cur = cur->_right;
        }
        else if (cur->_key > key)
        {
          cur = cur->_left;
        }
        else
        {
          return true;
        }
      }
      return false;
    }
    bool Erase(const K& key)
    {
      Node* parent = nullptr;
      Node* cur = _root;
      while (cur)
      {
        if (cur->_key < key)
        {
          parent = cur;
          cur = cur->_right;
        }
        else if (cur->_key > key)
        {
          parent = cur;
          cur = cur->_left;
        }
        else
        {
          // 1、左为空
          // 2、右为空
          // 3、左右都不为空
          if (cur->_left == nullptr)
          {
            if (cur == _root)
            {
              _root = cur->_right;
            }
            else
            {
              if (cur == parent->_left)
              {
                parent->_left = cur->_right;
              }
              else
              {
                parent->_right = cur->_right;
              }
            }
            delete cur;
            cur = nullptr;
          }
          else if (cur->_right == nullptr)
          {
            if (_root == cur)
            {
              _root = cur->_left;
            }
            else
            {
              if (cur == parent->_left)
              {
                parent->_left = cur->_left;
              }
              else
              {
                parent->_right = cur->_left;
              }
            }
            delete cur;
            cur = nullptr;
          }
          else
          {
            // 找到右子树最小节点进行替换
            Node* minParent = cur;
            Node* min = cur->_right;
            while (min->_left)
            {
              minParent = min;
              min = min->_left;
            }
            swap(cur->_key, min->_key);
            if (minParent->_left == min)
              minParent->_left = min->_right;
            else
              minParent->_right = min->_right;
            delete min;
          }
          return true;
        }
      }
      return false;
    }
    void InOrder()
    {
      _InOrder(_root);
      cout << endl;
    }
    bool FindR(const K& key)
    {
      return _FindR(_root, key);
    }
    bool InsertR(const K& key)
    {
      return _InsertR(_root, key);
    }
    bool EraseR(const K& key)
    {
      return _EraseR(_root, key);
    }
    ~BSTree()
    {
      _Destory(_root);
    }
    BSTree() = default;
    BSTree(const BSTree<K>& t)
    {
      _root = _Copy(t._root);
    }
    // t2 = t1
    BSTree<K>& operator=(BSTree<K> t)
    {
      swap(_root, t._root);
      return *this;
    }
  private:
    Node* _Copy(Node* root)
    {
      if (root == nullptr)
      {
        return nullptr;
      }
      Node* copyRoot = new Node(root->_key);
      copyRoot->_left = _Copy(root->_left);
      copyRoot->_right = _Copy(root->_right);
      return copyRoot;
    }
    void _Destory(Node*& root)
    {
      if (root == nullptr)
      {
        return;
      }
      _Destory(root->_left);
      _Destory(root->_right);
      delete root;
      root = nullptr;
    }
    bool _EraseR(Node*& root, const K& key)
    {
      if (root == nullptr)
        return false;
      if (root->_key < key)
        return _EraseR(root->_right, key);
      else if (root->_key > key)
        return _EraseR(root->_left, key);
      else
      {
        Node* del = root;
        if (root->_left == nullptr)
        {
          root = root->_right;
        }
        else if (root->_right == nullptr)
        {
          root = root->_left;
        }
        else
        {
          // 找右树的最左节点替换删除
          Node* min = root->_right;
          while (min->_left)
          {
            min = min->_left;
          }
          swap(root->_key, min->_key);
          return _EraseR(root->_right, key);
        }
        delete del;
        return true;
      }
    }
    bool _InsertR(Node*& root, const K& key)
    {
      if (root == nullptr)
      {
        root = new Node(key);
        return true;
      }
      if (root->_key < key)
        return _InsertR(root->_right, key);
      else if (root->_key > key)
        return _InsertR(root->_left, key);
      else
        return false;
    }
    bool _FindR(Node* root, const K& key)
    {
      if (root == nullptr)
        return false;
      if (root->_key < key)
        return _FindR(root->_right, key);
      else if (root->_key > key)
        return _FindR(root->_left, key);
      else
        return true;
    }
    void _InOrder(Node* root)
    {
      if (root == nullptr)
      {
        return;
      }
      _InOrder(root->_left);
      cout << root->_key << " ";
      _InOrder(root->_right);
    }
  private:
    Node* _root = nullptr;
  };
}

二叉搜索树的应用

  1. K模型:K模型即只有key作为关键码,结构中只需要存储Key即可,关键码即为需要搜索到的值
    比如:给一个单词word,判断该单词是否拼写正确,具体方式如下:

以词库中所有单词集合中的每个单词作为key,构建一棵二叉搜索树

在二叉搜索树中检索该单词是否存在,存在则拼写正确,不存在则拼写错误

  1. KV模型:每一个关键码key,都有与之对应的值Value,即<Key, Value>的键值对。该种方式在现实生活中非常常见

比如英汉词典就是英文与中文的对应关系,通过英文可以快速找到与其对应的中文,英文单词与其对应的中文<word, chinese>就构成一种键值对

再比如统计单词次数,统计成功后,给定单词就可快速找到其出现的次数,单词与其出现次数就是<word, count>就构成一种键值对

例如

namespace KeyValue
{
  template<class K, class V>
  struct BSTreeNode
  {
    BSTreeNode<K, V>* _left;
    BSTreeNode<K, V>* _right;
    K _key;
    V _value;
    BSTreeNode(const K& key, const V& value)
      :_left(nullptr)
      , _right(nullptr)
      , _key(key)
      , _value(value)
    {}
  };
  template<class K, class V>
  class BSTree
  {
    typedef BSTreeNode<K, V> Node;
  public:
    bool Insert(const K& key, const V& value)
    {
      if (_root == nullptr)
      {
        _root = new Node(key, value);
        return true;
      }
      Node* parent = nullptr;
      Node* cur = _root;
      while (cur)
      {
        if (cur->_key < key)
        {
          parent = cur;
          cur = cur->_right;
        }
        else if (cur->_key > key)
        {
          parent = cur;
          cur = cur->_left;
        }
        else
        {
          return false;
        }
      }
      cur = new Node(key, value);
      if (parent->_key < key)
      {
        parent->_right = cur;
      }
      else
      {
        parent->_left = cur;
      }
      return true;
    }
    Node* Find(const K& key)
    {
      Node* cur = _root;
      while (cur)
      {
        if (cur->_key < key)
        {
          cur = cur->_right;
        }
        else if (cur->_key > key)
        {
          cur = cur->_left;
        }
        else
        {
          return cur;
        }
      }
      return nullptr;
    }
    bool Erase(const K& key)
    {
      //...
      return true;
    }
    void InOrder()
    {
      _InOrder(_root);
      cout << endl;
    }
  private:
    void _InOrder(Node* root)
    {
      if (root == nullptr)
      {
        return;
      }
      _InOrder(root->_left);
      cout << root->_key << ":" << root->_value << endl;
      _InOrder(root->_right);
    }
  private:
    Node* _root = nullptr;
  };
  void TestBSTree1()
  {
    BSTree<string, string> dict;
    dict.Insert("sort", "排序");
    dict.Insert("left", "左");
    dict.Insert("right", "右");
    dict.Insert("vector", "向量");
    dict.Insert("insert", "插入");
    string str;
    while (cin >> str)
    {
      BSTreeNode<string, string>* ret = dict.Find(str);
      if (ret)
      {
        cout << "对应的中文:" << ret->_value << endl;
      }
      else
      {
        cout << "对应的中文->无此单词" << endl;
      }
    }
  }
  void TestBSTree2()
  {
    string arr[] = {"苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉"};
    BSTree<string, int> countTree;
    for (auto& str : arr)
    {
      auto ret = countTree.Find(str);
      if (ret)
      {
        ret->_value++;
      }
      else
      {
        countTree.Insert(str, 1);
      }
    }
    countTree.InOrder();
  }
}

二叉搜索树的性能分析

插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。

对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多。但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树:

最优情况下,二叉搜索树为完全二叉树(或者接近完全二叉树),其平均比较次数为log_2 N

最差情况下,二叉搜索树退化为单支树(或者类似单支),其平均比较次数为frac{N}{2}

优点:

  1. 快速的查找操作: BST的主要优点是在查找操作上具有较快的平均时间复杂度。在平均情况下,查找一个元素的时间复杂度是O(log n),其中n是树中节点的数量。这使得BST非常适合实现高效的搜索操作。
  2. 有序性: BST保持节点按照键的顺序排列。这意味着你可以轻松地执行范围查询,找到最小值和最大值,或者执行有序的遍历。
  3. 插入和删除操作: 插入和删除元素的平均时间复杂度也是O(log n)。这使得BST对于需要频繁插入和删除元素的应用非常有效。

缺点:

  1. 性能不稳定: 最坏情况下,BST的性能可能会下降到O(n),其中n是树中节点的数量。这种情况发生在BST变得非常不平衡时,例如,如果将有序数据插入BST,将导致树的高度为n,从而导致O(n)的查找时间。
  2. 不平衡性: BST的性能高度依赖于树的平衡性。如果树不平衡,例如,变成了链表,那么查找、插入和删除的性能都会下降到O(n)。为了解决这个问题,出现了平衡二叉搜索树(AVL树、红黑树等),它们确保树的高度保持在较小的范围内,以维持O(log n)的性能。
  3. 内存需求: BST通常需要额外的内存来存储左子树和右子树的指针,这可能会导致内存消耗较大。

总结来说,BST在平均情况下具有较好的性能,特别适用于有序数据的查找和动态数据的插入/删除操作。然而,在最坏情况下,BST的性能可能变得不稳定,因此为了确保性能稳定,可以使用平衡二叉搜索树或其他更复杂的数据结构。选择适当的数据结构取决于应用的需求和数据的特点。

相关文章
|
7月前
|
存储 算法 编译器
一篇文章教会你什么是二叉搜索树(上)
二叉搜索树概念 二叉搜索树(Binary Search Tree,BST)是一种二叉树的特殊形式,它具有以下关键性质:
|
2月前
|
存储 C++
【C++练级之路】【Lv.14】二叉搜索树(进化的二叉树——BST)
【C++练级之路】【Lv.14】二叉搜索树(进化的二叉树——BST)
【C++练级之路】【Lv.14】二叉搜索树(进化的二叉树——BST)
|
4月前
|
存储 算法 关系型数据库
【面试普通人VS高手系列】b树和b+树的理解
【面试普通人VS高手系列】b树和b+树的理解
|
9月前
|
算法
当二叉树的树叶飘落:深入探究后序遍历
后序遍历是一种深度优先遍历(Depth-First Traversal)方法,它的特点是对于每个节点的访问顺序是从左子节点到右子节点,最后再访问节点本身。具体来说,后序遍历按照以下顺序访问节点:
47 0
|
9月前
|
算法 程序员
程序员怎能不会二叉树系列(一)简单了解二叉树
程序员怎能不会二叉树系列(一)简单了解二叉树
|
9月前
|
程序员
程序员怎能不会二叉树系列(二)二叉树的四种遍历
程序员怎能不会二叉树系列(二)二叉树的四种遍历
|
存储 算法
二叉树-题型总结与归纳
二叉树-题型总结与归纳
59 0
二叉树-题型总结与归纳
|
存储 算法
在家练练基础算法 | 链表篇
在家练练基础算法 | 链表篇
在家练练基础算法 | 链表篇
|
算法
重温算法之最大二叉树
二叉树的题目还是很抽象,之前记得有个动态二叉树的生成网站现在已经关闭了,试用了好几款生成二叉树的开源框架感觉不是很好,不然有动态图理解起来会更加容易一些。做算法题其实空想是没用的而且效率不高,要有图协助才能事半功倍,另外题解的技巧也需要慢慢积累的,不要急着想一天两天就能完美解题的。
99 0
重温算法之最大二叉树