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

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

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的性能可能变得不稳定,因此为了确保性能稳定,可以使用平衡二叉搜索树或其他更复杂的数据结构。选择适当的数据结构取决于应用的需求和数据的特点。

相关文章
|
机器学习/深度学习 存储 TensorFlow
TensorFlow 基础实战
TensorFlow 基础实战
109 0
|
JavaScript Java 测试技术
基于ssm+vue.js+uniapp小程序的高校实验室信息化综合管理平台附带文章和源代码部署视频讲解等
基于ssm+vue.js+uniapp小程序的高校实验室信息化综合管理平台附带文章和源代码部署视频讲解等
214 6
|
Java Linux 编译器
C++这么难,为什么我们还要用C++?C++ Core Guidelines解析给了我答案
C++这么难,为什么我们还要用C++?C++ Core Guidelines解析给了我答案
102 0
|
数据库 数据安全/隐私保护 Python
property、魔法方法和继承
property、魔法方法和继承
|
存储 安全 搜索推荐
JAVA8实战 - 日期API
日期和时间的组合表示:合并表示时,要在时间前面加一大写字母T,如要表示北京时间2004年5月3日下午5点30分8秒,可以写成2004-05-03T17:30:08+08:00或20040503T173008+08。
459 0
leetcode-32. 最长有效括号(堆栈+贪心)
左边出现多余的括号, ‘ ) ’我们不用担心,我们已经算匹配完,用mark记录就行,‘ ( ’的话我们只需要先记录在堆栈里,再循环结束后,将多余的没有匹配的’ ( '一一用mark记录
186 0
leetcode-32. 最长有效括号(堆栈+贪心)
|
监控 架构师 Java
详解ElasticAPM实现微服务的链路追踪(二)
Elastic APM实现链路追踪,首先要引用开源的APMAgent(APM代理),然后将监控的信息发送到APMServer,然后在转存入ElasticSearch,最后有Kibana展示;具体流程如下图所示:
详解ElasticAPM实现微服务的链路追踪(二)
|
Java 程序员 Python
《Python 快速入门》一千个程序员有一千套编码规范
《Python 快速入门》一千个程序员有一千套编码规范
|
缓存 资源调度 JavaScript
Markdown 拓展-使用 vue.press 生成网站
介绍 VuePress V2 是一个以 Markdown 为中心的静态网站生成器。你可以使用 Markdown在新窗口打开 来书写内容(如文档、博客等),然后 VuePress 会帮助你生成一个静态网站来展示它们。
265 0
|
开发工具
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问