广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >二叉搜索树(C++)
  • 769
分享到

二叉搜索树(C++)

c++ 2023-08-30 14:08:45 769人浏览 八月长安
摘要

二叉搜索树 概念二叉搜索树的应用二叉搜索树的实现K模型基本结构和函数声明接口实现①find——查找关键码②Insert——插入关键码③Erase——删除关键码(==重点==)时间复杂度 源码(整体)非递归递归

在使用C语言数据结构阶段时,对二叉树进行了讲解。本节内容是对二叉树的深入探索,也是二叉树部分的收尾

概念

二叉搜索树也称二叉排序树(BST,Binary Search Tree):

  1. 空树
  2. 非空树(要具有以下性质)
    • 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
    • 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
    • 它的左右子树也分别为二叉搜索树

注意: 二叉搜索树的key值互不相同

eg:下图就属于二叉搜索树
二叉搜索树

二叉搜索树的应用

  • K模型:即只有key作为关键码,结构中只需要存储key。关键码就是要搜索到的值

     eg: 给一个单词Word,判断该单词是否拼写正确?  具体步骤:        1. 以词库中所有单词为基础,每个单词都是key,构建一颗搜索二叉树        2. 在二叉树中搜索该单词是否存在,存在则返回true,否则返回false
  • KV模型:每一个关键码key,都有与之对应的value,即的键值对

     eg1: 英汉词典 - 就是中文与英文的对应关系。通过英文可以快速找到与其对应的中文,英文单词和与其对应的中文就构成键值对。 eg2: 统计单词次数,统计成功后,给定单词就可以找到单词出现得次数。单词与其出现次数就是就构成一种键值对

二叉搜索树的实现

注意: 通过二叉搜索树的应用,了解到其有两个模型,接下来对这两个模型分别进行实现。因为两个模型的接口都相似,所以仅对一个模型的接口进行详细介绍。

K模型

K模型的实现分为递归和非递归两种,但是接口的实现逻辑是一样的。为了便于理解把基本结构和函数声明先附上,然后对接口进行讲解,最后在贴上完整的源码

基本结构和函数声明

//二叉树节点//节点使用struct,默认public访问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://默认构造BSTree():_root(nullptr){}//拷贝构造BSTree(const BSTree<K>& t);//赋值运算符重载BSTree<K>& operator=(BSTree<K> t);//析构函数~BSTree();//插入bool Insert(const K& key);//查找bool Find(const K& key);//删除bool Erase(const K& key);//中序遍历void InOrder();private:Node* _root;};

接口实现

①find——查找关键码

功能:查找关键码是否在树中,是返回真,否则返回假。
原理:

  1. 从根开始比较,比根大则往右边查找,比根小往左边查找
  2. 最多查找高度次,走到nullptr,则这个值不存在

实现代码

  1. 非递归版本
template<class K>bool BSTree<K>::Find(const K& key){Node* cur = _root;while (cur){if (cur->_key > key){cur = cur->_left;}else if (cur->_key < key){cur = cur->_right;}else{return true;}}return false;}
  1. 递归版本
bool Find(const K& key){Node* cur = _root;while (cur){if (cur->_key > key){cur = cur->_left;}else if (cur->_key < key){cur = cur->_right;}else{return true;}}return false;}

②Insert——插入关键码

原理:

  1. 树为空,直接把要插入的节点赋值给root
  2. 树不为空,通过查找的思路,找到要插入的合适位置。插入成功返回true
    注意:如果要插入的值和树中的值冲突,则返回false

实现代码

  1. 非递归版本
//插入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->_left;}else if (cur->_key < key){parent = cur;cur = cur->_right;}else{return false;}}cur = new Node(key);if (parent->_key > key){parent->_left = cur;}else{parent->_right = cur;}return true;}
  1. 递归版本
bool InsertR(const K& key){return _InsertR(_root, key);}bool _FindR(Node* root, const K& key){if (root == nullptr)return false;if (root->_key > key)return _FindR(root->_left, key);else if (root->_key < key)return _FindR(root->_right, key);elsereturn true;}

③Erase——删除关键码(重点

原理:

  1. 查找关键码是否在二叉搜索树中,不存在直接返回false
  2. 存在,则要分为四种情况

情况:
在考虑树的所有情况时,要把根节点的情况和普通情况,分离开来。哪怕最后根节点的情况和普通情况一样。(仅代表博主个人观点)

  1. 要删除的节点无孩子节点
    第一种情况
  1. 要删除的节点无左孩子节点
    第二种情况
  1. 要删除的节点无右孩子节点
    第三种情况
  1. 要删除的节点左右都不为空 (采用的方法 一 替换法
    替换法:找到删除节点左子树的最大值节点(leftMax),然后与删除节点交换,再删除现在的leftMax。(也可以找删除节点右子树的最小值节点(rightMin),原理相同)
    第四种情况

实现代码

  1. 非递归版本
//删除bool Erase(const K& key){Node* parent = nullptr;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  //找到了要删除的值{//分情况//1.左为空if (cur->_left == nullptr){if (cur == _root)_root = cur->_right;else{if (parent->_right == cur){parent->_right = cur->_right;}else{parent->_left = cur->_right;}}}//2.右为空else if (cur->_right == nullptr){if (cur == _root)_root = cur->_left;else{if (parent->_right == cur){parent->_right = cur->_left;}else{parent->_left = cur->_left;}}} //3.左右都不为空else{parent = cur;Node* leftMax = cur->_left;while (leftMax->_right){parent = leftMax;leftMax = leftMax->_right;}swap(cur->_key, leftMax->_key);if (parent->_left == leftMax){parent->_left = leftMax->_left;}else{parent->_right = leftMax->_left;}cur = leftMax;}delete cur;cur = nullptr;return true;}}return false;}
  1. 递归版本
//删除bool EraseR(const K& key){return _EraseR(_root, key);}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* leftMax = root->_left;while (leftMax->_right){leftMax = leftMax->_right;}swap(root->_key, leftMax->_key);return _EraseR(root->_left, key);}delete del;del = nullptr;return true;}}

时间复杂度

二叉搜索树的操作时间复杂度:在O(logN)和O(N)之间
画图解释时间复杂度:
时间复杂度

源码(整体)

非递归

//非递归namespace key{//二叉树节点//节点使用struct,默认public访问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://默认构造BSTree():_root(nullptr){}//拷贝构造BSTree(const BSTree<K>& t){_root = Copy(t._root);}//赋值运算符重载BSTree<K>& operator=(BSTree<K> t){swap(_root, t._root);return *this;}//析构函数~BSTree(){Destroy(_root);}//插入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->_left;}else if (cur->_key < key){parent = cur;cur = cur->_right;}else{return false;}}cur = new Node(key);if (parent->_key > key){parent->_left = cur;}else{parent->_right = cur;}return true;}//查找bool Find(const K& key){Node* cur = _root;while (cur){if (cur->_key > key){cur = cur->_left;}else if (cur->_key < key){cur = cur->_right;}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->_left;}else if (cur->_key < key){parent = cur;cur = cur->_right;}else  //找到了要删除的值{//分情况//1.左为空if (cur->_left == nullptr){if (cur == _root)_root = cur->_right;else{if (parent->_right == cur){parent->_right = cur->_right;}else{parent->_left = cur->_right;}}}//2.右为空else if (cur->_right == nullptr){if (cur == _root)_root = cur->_left;else{if (parent->_right == cur){parent->_right = cur->_left;}else{parent->_left = cur->_left;}}} //3.左右都不为空else{parent = cur;Node* leftMax = cur->_left;while (leftMax->_right){parent = leftMax;leftMax = leftMax->_right;}swap(cur->_key, leftMax->_key);if (parent->_left == leftMax){parent->_left = leftMax->_left;}else{parent->_right = leftMax->_left;}cur = leftMax;}delete cur;cur = nullptr;return true;}}return false;}//中序遍历void InOrder(){_InOrder(_root);cout << endl;}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->_tight);return copyRoot;}void Destroy(Node*& root){if (root == nullptr)return;Destroy(root->_left);Destroy(root->_right);delete root;root = nullptr;}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);}private:Node* _root;};}

递归

//递归namespace keyR{//二叉树节点//节点使用struct,默认public访问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://默认构造BSTree():_root(nullptr){}//拷贝构造BSTree(const BSTree<K>& t){_root = Copy(t._root);}//赋值运算符重载BSTree<K>& operator=(BSTree<K> t){swap(_root, t._root);return *this;}//析构函数~BSTree(){Destroy(_root);}//插入bool InsertR(const K& key){return _InsertR(_root, key);}//查找bool FindR(const K& key){return _FindR(_root, key);}//删除bool EraseR(const K& key){return _EraseR(_root, key);}//中序遍历void InOrder(){_InOrder(_root);cout << endl;}private://在root添加引用很关键,要不然得是二级指针bool _InsertR(Node*& root, const K& key){if (root == nullptr){root = new Node(key);return true;}if (root->_key > key)return _InsertR(root->_left, key);else if (root->_key < key)return _InsertR(root->_right, key);elsereturn false;}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* leftMax = root->_left;while (leftMax->_right){leftMax = leftMax->_right;}swap(root->_key, leftMax->_key);return _EraseR(root->_left, key);}delete del;del = nullptr;return true;}}bool _FindR(Node* root, const K& key){if (root == nullptr)return false;if (root->_key > key)return _FindR(root->_left, key);else if (root->_key < key)return _FindR(root->_right, key);elsereturn true;}void Destroy(Node*& root){if (root == nullptr)return;Destroy(root->_left);Destroy(root->_right);delete root;root = nullptr;}Node* Copy(Node* root){if (root == nullptr)return nullptr;Node* copyRoot = new Node(root->_key);copyRoot->_left = CopyRoot(root->_left);copyRoot->_right = CopyRoot(root->_right);return copyRoot;}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);}private:Node* _root;};}

KV模型

直接贴出源码:

namespace key_value{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:BSTree():_root(nullptr){}BSTree(const BSTree<K, V>& t){_root = Copy(t._root);}BSTree<K, V>& operator=(BSTree<K, V> t){swap(_root, t._root);return *this;}~BSTree(){Destroy(_root);}void InOrder(){_InOrder(_root);cout << endl;}Node* FindR(const K& key){return _FindR(_root, key);}bool InsertR(const K& key, const V& value){return _InsertR(_root, key, value);}bool EraseR(const K& key){return _EraseR(_root, key);}private: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 = root->_right;}else if (root->_right == nullptr){root = root->_left;}else{Node* leftMax = root->_left;while (leftMax->_right){leftMax = leftMax->_right;}swap(root->_key, leftMax->_key);//删的值在左边return _EraseR(root->_left, key);}delete del;return true;}}void Destroy(Node*& root){if (root == nullptr)return;Destroy(root->_left);Destroy(root->_right);delete root;root = nullptr;}Node* Copy(Node* root){if (root == nullptr)return nullptr;Node* copyRoot = new Node(root->_key, root->_value);copyRoot->_left = Copy(root->_left);copyRoot->_right = Copy(root->_right);return copyRoot;}void _InOrder(Node* root){if (root == nullptr)return;_InOrder(root->_left);cout << root->_key << ":" << root->_value << endl;_InOrder(root->_right);}Node* _FindR(Node* root, const K& key){if (root == nullptr)return nullptr;if (root->_key > key){return _FindR(root->_left, key);}else if (root->_key < key){return _FindR(root->_right, key);}else{return root;}}bool _InsertR(Node*& root, const K& key, const V& value){if (root == nullptr){root = new Node(key, value);return true;}if (root->_key > key){return _InsertR(root->_left, key, value);}else if (root->_key < key){return _InsertR(root->_right, key, value);}else{return false;}}private:Node* _root;};}

来源地址:https://blog.csdn.net/kpl_20/article/details/132505424

--结束END--

本文标题: 二叉搜索树(C++)

本文链接: https://www.lsjlt.com/news/382979.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 二叉搜索树(C++)
    二叉搜索树 概念二叉搜索树的应用二叉搜索树的实现K模型基本结构和函数声明接口实现①find——查找关键码②Insert——插入关键码③Erase——删除关键码(==重点==)时间复杂度 源码(整体)非递归递归 ...
    99+
    2023-08-30
    c++
  • 二叉搜索树
    💕人生没有太晚的开始,所有的时刻都是七点。 —— 温妮 · 普赖弗曼💕 🐼作者:不能再留遗憾了 🎆专栏:Java学习 Ὡ...
    99+
    2023-09-01
    java 算法 数据结构
  • C++二叉搜索树实例分析
    本篇内容介绍了“C++二叉搜索树实例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!独一无二的二叉搜索树Given an integer&...
    99+
    2023-06-19
  • 何为二叉搜索树
    本篇内容主要讲解“何为二叉搜索树”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“何为二叉搜索树”吧!什么是树树是一种数据结构,它是由n(n>=1)个有限结点...
    99+
    2022-10-19
  • C++二叉搜索树BSTree如何使用
    这篇文章主要介绍“C++二叉搜索树BSTree如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C++二叉搜索树BSTree如何使用”文章能帮助大家解决问题。一、概念二叉搜索树又称二叉排序树,它...
    99+
    2023-07-05
  • C++二叉搜索树BSTree使用详解
    目录一、概念二、基础操作1.查找find2.插入Insert3.中序遍历InOrder4.删除erase三、递归写法1.递归查找2.递归插入3.递归删除四、应用五、题目练习一、概念 ...
    99+
    2023-03-09
    C++二叉搜索树BSTree C++二叉搜索树 C++ BSTree
  • C++怎么把二叉搜索树转换累加树
    今天小编给大家分享一下C++怎么把二叉搜索树转换累加树的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。把二叉搜索树转换为累加树...
    99+
    2023-07-04
  • C++LeetCode0538二叉搜索树转换累加树示例
    目录LeetCode 538把二叉搜索树转换为累加树方法一:DFS反向中序遍历AC代码C++LeetCode 538把二叉搜索树转换为累加树 力扣题目链接:lee...
    99+
    2022-12-16
    C++ 二叉搜索树转换累加树 C++ LeetCode
  • Python实现二叉搜索树
    二叉搜索树 我们已经知道了在一个集合中获取键值对的两种不同的方法。回忆一下这些集合是如何实现ADT(抽象数据类型)MAP的。我们讨论两种ADT MAP的实现方式,基于列表的二分查找和哈希表。在这一节中,我...
    99+
    2022-06-04
    Python
  • C++实现LeetCode(98.验证二叉搜索树)
    [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树 Given a binary tree, determine if it is ...
    99+
    2022-11-12
  • C++实现LeetCode(99.复原二叉搜索树)
    [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树 Two elements of a binary search tree (BST...
    99+
    2022-11-12
  • C++深入细致探究二叉搜索树
    目录1、二叉搜索树的概念2、二叉搜索树的操作二叉搜索树的查找二叉搜索树的插入二叉搜索树的删除3、二叉搜索树的实现4、二叉搜索树的性能分析1、二叉搜索树的概念  二叉搜索树又...
    99+
    2022-11-13
  • C++实现验证二叉搜索树代码
    本篇内容主要讲解“C++实现验证二叉搜索树代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++实现验证二叉搜索树代码”吧!验证二叉搜索树Given a binary tree, determ...
    99+
    2023-06-20
  • C++如何实现验证二叉搜索树
    本文小编为大家详细介绍“C++如何实现验证二叉搜索树”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++如何实现验证二叉搜索树”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。验证二叉搜索树Example 1:In...
    99+
    2023-06-19
  • C++二叉搜索树的操作有哪些
    本文小编为大家详细介绍“C++二叉搜索树的操作有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++二叉搜索树的操作有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。二叉搜索树概念与操作二叉搜索树的概念二...
    99+
    2023-06-29
  • C++实现LeetCode(173.二叉搜索树迭代器)
    [LeetCode] 173.Binary Search Tree Iterator 二叉搜索树迭代器 Implement an iterator over a binary sea...
    99+
    2022-11-12
  • 【C++】平衡二叉搜索树的模拟实现
    🌇个人主页:平凡的小苏 📚学习格言:命运给你一个低的起点,是想看你精彩的翻盘,而不是让你自甘堕落,脚下的路虽然难走,但我还能走,比起向阳而生,我更想尝试逆风翻盘。 ...
    99+
    2023-09-11
    c++ 开发语言
  • C++超详细快速掌握二叉搜索树
    目录二叉搜索树概念与操作二叉搜索树的概念二叉搜索树的操作查找插入删除二叉搜索树的应用二叉树的性能分析二叉搜索树概念与操作 二叉搜索树的概念 二叉搜索树又称二叉排序树,若它的左子树不为...
    99+
    2022-11-13
  • C++实现LeetCode(96.独一无二的二叉搜索树)
    [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树 Given n, how many structurally un...
    99+
    2022-11-12
  • C++实现LeetCode(95.独一无二的二叉搜索树之二)
    [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二 Given an integer n, generate...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作