二叉搜索树

  1. 根结点比左子树中所有结点都大
  2. 根结点比右子树所有结点都小
  3. 最小的元素在最左侧
  4. 最大的元素在最右侧
  5. 中序遍历有序

具有以上的特征的二叉树就是二叉搜索树也叫二叉排序数

二叉搜索树的操作

查找



要保存查找值的双亲,以便于后续执行插入操作

Node* Find(const T& data){Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){pParent = pCur;  //保存双亲if (data == pCur->_data)return pCur;else if (data < pCur->_data)pCur = pCur->_pLeft;elsepCur = pCur->_pRight;}//走到这个位置,当前元素一定不存在return nullptr;}

插入

  1. 树为空,则直接插入
  2. 树不空,按二叉搜索树性质查找插入位置,插入新节点

bool Insert(const T& data){//空树--->直接插入if (_pRoot == nullptr){_pRoot = new Node(data);return true;}//非空//找到待插入元素在二叉搜索树中的位置Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){pParent = pCur;  //记录cur变化前的位置,就是记录cur的双亲if (data < pCur->_data){pCur = pCur->_pLeft;}else if (data > pCur->_data)pCur = pCur->_pRight;elsereturn false;}//插入结点pCur = new Node(data);if (data < pParent->_data)pParent->_pLeft = pCur;elsepParent->_pRight = pCur;return true;}

删除

首先查找元素是否在二叉搜索树中,如果不存在,则返回, 否则要删除的结点可能分下面四种情况:

  • 要删除的结点无孩子结点
  • 要删除的结点只有左孩子结点
  • 要删除的结点只有右孩子结点
  • 要删除的结点有左、右孩子结点

总结一下,实际情况要删除的话,分为三步

  1. 删除该结点且使被删除节点的双亲结点指向被删除节点的左孩子结点
  2. 删除该结点且使被删除节点的双亲结点指向被删除结点的右孩子结点
  3. :在它的右子树中寻找中序下的第一个结点(关键码最小),用它的值填补到被删除节点中, 再来处理该结点的删除问题


//删除
bool Delete(const T& data)
{if (nullptr == _pRoot)return false;//找到待删除元素再二叉搜索树中的位置
Node *pCur = _pRoot;
Node *pParent = nullptr;
while (pCur)
{if (data < pCur->_data){pParent = pCur;pCur = pCur->_pLeft;}else if (data>pCur->_data){pParent = pCur;pCur = pCur->_pRight;}elsebreak;
}//结点不存在
if (nullptr == pCur)return false;//结点已经找到----分情况删除
//1.左右孩子都不存在
//2.只有左孩子
//3.只有右孩子
//4.左右孩子均存在
Node *pDelNode = pCur;
if (nullptr == pCur->_pRight)
{// 叶子结点 || 只有左孩子if (nullptr == pParent){_pRoot = pCur->_pLeft;}else{if (pCur == pParent->_pLeft)pParent->_pLeft = pCur->_pLeft;elsepParent->_pRight = pCur->_pLeft;}
}
else if (nullptr == pCur->_pLeft)
{// 只有右孩子if (nullptr == pParent)_pRoot = pCur->_pRight;else{if (pCur == pParent->_pLeft)pParent->_pLeft = pCur->_pRight;elsepParent->_pRight = pCur->_pRight;}
}
else
{//左右孩子均存在,不能直接删除,必须在其子树中找一个替代结点进行删除//方式一:在其左子树中找最大的结点---->最右侧的结点//方式二:在其右子树中找最小的结点---->最左侧的结点//在右子树中查找替代结点Node *pMostLeft = pCur->_pRight;pParent = pCur;while (pMostLeft->_pLeft){pParent = pMostLeft;  //每次变动前保存双亲pMostLeft = pMostLeft->_pLeft;}pCur->_data = pMostLeft->_data;//删除替代结点if (pMostLeft == pParent->_pLeft)pParent->_pLeft = pMostLeft->_pRight;elsepParent->_pRight = pMostLeft->_pLeft;pDelNode = pMostLeft;
}delete pDelNode;
return true;
}

时间复杂度

插入和删除操作都必须先查找,查找效率代表了二叉搜索树中各个操作的性能。
对有n个结点的二叉搜索树,若每个元素查找的概率相等,则二叉搜索树平均查找长度是结点在二叉搜索树的深度的函数,即结点越深,则比较次数越多
但对于同一个关键码集合,如果各关键码插入的次序不同,可能得到不同结构的二叉搜索树

最优情况


最差情况


总体代码

#include<iostream>
using namespace std;template<class T>
struct BSTreeNode
{BSTreeNode(const T& data = T())  //初始化   T()默认值:_pLeft(nullptr), _pRight(nullptr), _data(data){}BSTreeNode<T>* _pLeft;  //指向左子树BSTreeNode<T>* _pRight; //指向右子树T _data;
};template<class T>
class BsTree
{typedef BSTreeNode<T>  Node;public:BsTree():_pRoot(nullptr)  //空树就是一颗二叉搜索树{}~BsTree(){_Destroy(_pRoot);}bool Insert(const T& data){//空树--->直接插入if (_pRoot == nullptr){_pRoot = new Node(data);return true;}//非空//找到待插入元素在二叉搜索树中的位置Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){pParent = pCur;  //记录cur变化前的位置,就是记录cur的双亲if (data < pCur->_data){pCur = pCur->_pLeft;}else if (data > pCur->_data)pCur = pCur->_pRight;elsereturn false;}//插入结点pCur = new Node(data);if (data < pParent->_data)pParent->_pLeft = pCur;elsepParent->_pRight = pCur;return true;}//获取最左侧结点Node *LeftMost(){if (nullptr == _pRoot)return nullptr;Node *pCur = _pRoot;while (pCur->_pLeft)pCur = pCur->_pLeft;return pCur;}//获取最右侧结点Node *RightMost(){if(nullptr == _pRoot)return nullptr;Node *pCur = _pRoot;while (pCur->_pRight)pCur = pCur->_pRight;return pCur;}void InOrder()            //再封装一层是为了让用户方便,尽量不要让用户传参数{_InOrder(_pRoot);}//删除bool Delete(const T& data){if (nullptr == _pRoot)return false;//找到待删除元素再二叉搜索树中的位置Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){if (data < pCur->_data){pParent = pCur;pCur = pCur->_pLeft;}else if (data>pCur->_data){pParent = pCur;pCur = pCur->_pRight;}elsebreak;}//结点不存在if (nullptr == pCur)return false;//结点已经找到----分情况删除//1.左右孩子都不存在//2.只有左孩子//3.只有右孩子//4.左右孩子均存在Node *pDelNode = pCur;if (nullptr == pCur->_pRight){// 叶子结点 || 只有左孩子if (nullptr == pParent){_pRoot = pCur->_pLeft;}else{if (pCur == pParent->_pLeft)pParent->_pLeft = pCur->_pLeft;elsepParent->_pRight = pCur->_pLeft;}}else if (nullptr == pCur->_pLeft){// 只有右孩子if (nullptr == pParent)_pRoot = pCur->_pRight;else{if (pCur == pParent->_pLeft)pParent->_pLeft = pCur->_pRight;elsepParent->_pRight = pCur->_pRight;}}else{//左右孩子均存在,不能直接删除,必须在其子树中找一个替代结点进行删除//方式一:在其左子树中找最大的结点---->最右侧的结点//方式二:在其右子树中找最小的结点---->最左侧的结点//在右子树中查找替代结点Node *pMostLeft = pCur->_pRight;pParent = pCur;while (pMostLeft->_pLeft){pParent = pMostLeft;  //每次变动前保存双亲pMostLeft = pMostLeft->_pLeft;}pCur->_data = pMostLeft->_data;//删除替代结点if (pMostLeft == pParent->_pLeft)pParent->_pLeft = pMostLeft->_pRight;elsepParent->_pRight = pMostLeft->_pLeft;pDelNode = pMostLeft;}delete pDelNode;return true;}Node* Find(const T& data){Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){pParent = pCur;  //保存双亲if (data == pCur->_data)return pCur;else if (data < pCur->_data)pCur = pCur->_pLeft;elsepCur = pCur->_pRight;}//走到这个位置,当前元素一定不存在return nullptr;}private:void _InOrder(Node * pRoot){if (pRoot){_InOrder(pRoot->_pLeft);cout << pRoot->_data << " ";_InOrder(pRoot->_pRight);}}void _Destroy(Node*& pRoot){if (pRoot){_Destroy(pRoot->_pLeft);_Destroy(pRoot->_pRight);delete pRoot;pRoot = nullptr;}}
private:Node *_pRoot;  //记录根结点 就是保存了整颗树
};void TestBSTree()
{int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };BsTree<int>t;for (auto e : a)t.Insert(e);cout << t.LeftMost() << endl;cout << t.RightMost() << endl;t.InOrder();printf("\n");t.Delete(8);t.InOrder();printf("\n");t.Delete(0);t.InOrder();printf("\n");t.Delete(1);t.InOrder();printf("\n");t.Delete(5);t.InOrder();printf("\n");
}

如何让二叉搜索树不出现最差情况

把单支树变的平衡那就是AVL树

二叉搜索树转化为双向链表

AVL树

二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当 于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii和E.M.Landis在1962年 发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之 差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度

AVL树的性质

  • 它的左右子树都是AVL树
  • 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
  • 也可以为空树

如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在log2N ,搜索时间复杂度O( log2N)。

AVL树结点定义

template<class T>
struct AVLTreeNode{AVLTreeNode(const T& data = T()):_pLeft(nullptr), _pRight(nullptr), _pParent(nullptr), _data(data), _bf(0){}AVLTreeNode<T>* _pLeft;AVLTreeNode<T>* _pRight;AVLTreeNode<T>* _pParent;T _data;int _bf;  //当前结点的平衡因子
};

AVL树的插入

AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么AVL树的插入 过程可以分为两步:

  1. 按照二叉搜索树的方式插入新节点
  2. 调整节点的平衡因子

平衡因子的判定

  1. 先按照二叉搜索树的规则将节点插入到AVL树中
  2. 新节点插入后,AVL树的平衡性可能会遭到破坏,此时就需要更新平衡因子,并检测是否破坏了AVL树的平衡性
  3. pCur插入后,pParent的平衡因子一定需要调整,在插入之前,pParent的平衡因子分为三种情况:-1,0, 1, 分以下两种情况:
 1. 如果pCur插入到pParent的左侧,只需给pParent的平衡因子-1即可      2. 如果pCur插入到pParent的右侧,只需给pParent的平衡因子+1即可
  1. 此时:pParent的平衡因子可能有三种情况:0,正负1, 正负2
1. 如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整成0,此时满 足         AVL树的性质,插入成功
2. 如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更新成正负1, 此         时以pParent为根的树的高度增加,需要继续向上更新
3. 如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进行旋转处理

旋转处理

如果parent的平衡因子是2或者-2,需要对以parent为根的二叉树进行旋转处理:

  • 左单旋
  • 右单旋
  • 左右双旋
  • 右左双旋

左单旋

新结点插入到较高右子树的右侧----右右---->左单旋

上图在插入前,AVL树是平衡的,新节点插入到30的左子树(注意:此处不是左孩子)中,30左子树增加 了一层,导致以60为根的二叉树不平衡,要让60平衡,只能将60左子树的高度减少一层,右子树增加一 层
即将左子树往上提,这样60转下来,因为60比30大,只能将其放在30的右子树,而如果30有右子树,右 子树根的值一定大于30,小于60,只能将其放在60的左子树,旋转完成后,更新节点的平衡因子即可。在旋转过程中,有以下几种情况需要考虑:

  1. 30节点的右孩子可能存在,也可能不存在
  2. 60可能是根节点,也可能是子树 如果是根节点,旋转完成后,要更新根节点 如果是子树,可能是某个节点的左子树,也可能是右子树

左单旋实现

//左单旋void _RotateLeft(Node * pParent){Node * pSubR = pParent->_pRight;Node * pSubRL = pSubR->_pLeft;//第一步pParent->_pRight = pSubRL;if (pSubRL)pSubRL->_pParent = pParent;//第一步完//第二步pSubR->_pLeft = pParent;Node * pPParent = pParent->_pParent;pSubR->_pParent = pPParent;pParent->_pParent = pSubR;//第二步完//第三步if (nullptr == pPParent)_pRoot = pSubR;else{if (pParent == pPParent->_pLeft)pPParent->_pLeft = pSubR;elsepPParent->_pRight = pSubR;}//第三步完pParent->_bf = pSubR->_bf = 0;}

右单旋

新节点插入较高左子树的左侧—左左:右单旋

右单旋实现

//右单旋void _RotateLeft(Node * pParent){//第一步Node *pSubL = parent->_pLeft; // pParent的左孩子 Node*pSubLR = pSubL->_pRight; //左子树的右孩子//旋转完成之后//更新孩子指针域pParent->_pLeft = pSubLR;if (pSubLR)pSubLR->_pParent = pParent;//第一步完//第二步pSubL->_pRight = pParent;//更新双亲指针域Node* pPParent = pParent->_pParent;pParent->_pParent = pSubL;pSubL->_pParent = pPParent;//第二步完//第三步//对pParent分情况:根结点||非根结点(pParent可能为其双亲的左||右孩子)if (nullptr == pPParent)_pRoot = pSubL;else{if (pParent == pPParent->_pLeft)pPParent->_pLeft = pSubL;elsepPParent->_pRight = pSubL;}//第三步完//更新平衡因子pParent->_bf = pSubL->_bf = 0;}

左右双旋

新节点插入较高左子树的右侧—左右:先左单旋再右单旋

左右双旋实现

void _RotateLR(Node *pParent){Node* pSubL = pParent->_pLeft;Node* pSubLR = pSubL->_pRight;int bf = pSubLR->_bf;_RotateL(pParent->_pLeft);_RotateR(pParent);if (-1 == bf)pParent->_bf = 1;else if (1 == bf)pSubL->_bf = -1;}

右左双旋

新节点插入较高右子树的左侧—右左:先右单旋再左单旋

右左双旋实现

//右左双旋void _RotateRL(Node * pParent){Node *pSubR = pParent->_pRight;Node*pSubL = pSubR->_pLeft;int bf = pSubL->_bf;_RotateR(pParent->_pRight);_RotateL(pParent);if (-1 == bf)pSubR->_bf = 1;else if (1 == bf)pParent->_bf = -1;}

总结

假如以pParent为根的子树不平衡,即pParent的平衡因子为2或者-2,分以下情况考虑

  1. pParent的平衡因子为2,说明pParent的右子树高,设pParent的右子树的根为pSubR
 1.当pSubR的平衡因子为1时,执行左单旋 2.当pSubR的平衡因子为-1时,执行右左双旋
  1. pParent的平衡因子为-2,说明pParent的左子树高,设pParent的左子树的根为pSubL
 1.当pSubL的平衡因子为-1是,执行右单旋 2.当pSubL的平衡因子为1时,执行左右双旋

AVL树的验证

AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:

  1. 验证其为二叉搜索树
如果中序遍历可得到一个有序的序列,就说明为二叉搜索树
  1. 验证其为平衡树
 1.每个结点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子) 2.结点的平衡因子是否计算正确
bool _IsVaildAVLTree(Node *pRoot){if (nullptr = pRoot)return true;size_t leftHeight = _Height(pRoot->_pLeft);size_t rightHeight = _Height(pRoot->_pRight);if (!(pRoot->_bf >= -1 && pRoot->_bf <= 1 && pRoot->_bf == rightHeight - leftHeight))return false;return _IsVaildAVLTree(pRoot->_pLeft) && (pRoot->_pRight);}size_t _Height(Node *pRoot){if (nullptr == pRoot)return 0;size_t leftHeight = _Height(pRoot->_pLeft);size_t rightHeight = _Height(pRoot->_pRight);return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;}

AVL树的删除

因为AVL树也是二叉搜索树,可按照二叉搜索树的方式将节点删除,然后再更新平衡因子,只不过与删除不同的是,删除节点后的平衡因子更新,差情况下一直要调整到根结点的位置。
参考 《算法导论》或《数据结构-用面向对象方法与C++描述》殷人昆版。

AVL树的性能

AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证 查询时高效的时间复杂度,即 。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如: 插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。 因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树, 但一个结构经常修改,就不太适合。

总体代码

#pragma once
#include<iostream>
using namespace std;template<class T>
struct AVLTreeNode{AVLTreeNode(const T& data = T()):_pLeft(nullptr), _pRight(nullptr), _pParent(nullptr), _data(data), _bf(0){}AVLTreeNode<T>* _pLeft;AVLTreeNode<T>* _pRight;AVLTreeNode<T>* _pParent;T _data;int _bf;  //当前结点的平衡因子
};template<class T>
class AVLTree
{typedef AVLTreeNode<T> Node;
public:AVLTree():_pRoot(nullptr){}bool Insert(const T& data){if (nullptr == _pRoot){_pRoot = new Node(data);return true;}//非空//按照二叉搜索树的性质:找待插入结点在AVL树中的位置Node *pCur = _pRoot;Node *pParent = nullptr;while (pCur){pParent = pCur;if (data < pCur->_data)pCur = pCur->_pLeft;else if (data>pCur->_data)pCur = pCur->_pRight;elsereturn false;}//插入新结点pCur = new Node(data);if (data < pParent->_data)pParent->_pLeft = pCur;elsepParent->_pRight = pCur;pCur->_pParent = pParent;//可能会导致pParent结点的平衡因子不满足AVL树的性质while (pParent){//必须更新平衡因子//按左子树-右子树if (pCur == pParent->_pLeft)pParent->_bf--;elsepParent->_bf++;if (0 == pParent->_bf)return true;else if (-1 == pParent->_bf || 1 == pParent->_bf){pCur = pParent;pParent = pCur->_pParent;}else{//双亲的平衡因子不满足AVL树的的性质//双亲的结点的平衡因子为:2 或者-2//需要对以双亲为根的二叉树进行旋转处理if (2 == pParent->_bf)        //双亲平衡因子和pcur的平衡因子是同号,用单旋{//右子树高if (1 == pCur->_bf)_RotateL(pParent);else_RotateRL(pParent);}else{//左子树高if (-1 == pCur->_bf)_RotateR(pParent);else_RotateLR(pParent);}break;}}return true;}void Inorder(){_Inorder(_pRoot);}bool IsVaildAVLTree(){return _IsVaildAVLTree(_pRoot);}protected:bool _IsVaildAVLTree(Node *pRoot){if (nullptr == pRoot)return true;int leftHeight = _Height(pRoot->_pLeft);int rightHeight = _Height(pRoot->_pRight);if (!(pRoot->_bf >= -1 && pRoot->_bf <= 1 && pRoot->_bf == rightHeight - leftHeight))return false;return _IsVaildAVLTree(pRoot->_pLeft) && _IsVaildAVLTree(pRoot->_pRight);}int _Height(Node *pRoot){if (nullptr == pRoot)return 0;int leftHeight = _Height(pRoot->_pLeft);int rightHeight = _Height(pRoot->_pRight);return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;}void _Inorder(Node *pRoot){if (pRoot){_Inorder(pRoot->_pLeft);cout << pRoot->_data << " ";_Inorder(pRoot->_pRight);}}//右单旋void _RotateR(Node * pParent){//第一步Node *pSubL = pParent->_pLeft; // pParent的左孩子 Node *pSubLR = pSubL->_pRight; //左子树的右孩子//旋转完成之后//更新孩子指针域pParent->_pLeft = pSubLR;if (pSubLR)pSubLR->_pParent = pParent;//第一步完//第二步pSubL->_pRight = pParent;//更新双亲指针域Node* pPParent = pParent->_pParent;pParent->_pParent = pSubL;pSubL->_pParent = pPParent;//第二步完//第三步//对pParent分情况:根结点||非根结点(pParent可能为其双亲的左||右孩子)if (nullptr == pPParent)_pRoot = pSubL;else{if (pParent == pPParent->_pLeft)pPParent->_pLeft = pSubL;elsepPParent->_pRight = pSubL;}//第三步完//更新平衡因子pParent->_bf = pSubL->_bf = 0;}//左单旋void _RotateL(Node * pParent){Node * pSubR = pParent->_pRight;Node * pSubRL = pSubR->_pLeft;//第一步pParent->_pRight = pSubRL;if (pSubRL)pSubRL->_pParent = pParent;//第一步完//第二步pSubR->_pLeft = pParent;Node * pPParent = pParent->_pParent;pSubR->_pParent = pPParent;pParent->_pParent = pSubR;//第二步完//第三步if (nullptr == pPParent)_pRoot = pSubR;else{if (pParent == pPParent->_pLeft)pPParent->_pLeft = pSubR;elsepPParent->_pRight = pSubR;}//第三步完pParent->_bf = pSubR->_bf = 0;}//右左双旋void _RotateRL(Node * pParent){Node *pSubR = pParent->_pRight;Node*pSubL = pSubR->_pLeft;int bf = pSubL->_bf;_RotateR(pParent->_pRight);_RotateL(pParent);if (-1 == bf)pSubR->_bf = 1;else if (1 == bf)pParent->_bf = -1;}//左右双旋void _RotateLR(Node *pParent){Node* pSubL = pParent->_pLeft;Node* pSubLR = pSubL->_pRight;int bf = pSubLR->_bf;_RotateL(pParent->_pLeft);_RotateR(pParent);if (-1 == bf)pParent->_bf = 1;else if (1 == bf)pSubL->_bf = -1;}
private:Node *_pRoot;
};void TestAVLTree()
{//int array[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };int array[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };AVLTree<int>t;for (auto e : array)t.Insert(e);t.Inorder();printf("\n");if (t.IsVaildAVLTree()){cout << "t is vailed AVL Tree" << endl;}else{cout << "t is not vaild AVL Tree" << endl;}
}

详解 二叉搜索树-----AVL树相关推荐

  1. C++ 第八节数据结构 第七节 ——二叉搜索树 AVL树 红黑树(底层原理图+模拟实现)

    第一次,C++和数据结构联合推出,倾情献上呦~~ 给个关注吧 23333~~~~~~(现在每天系统就给我一个机器人的粉丝量了55555~~~~~) 本节内容,我们将着重来探讨 二叉树 中特殊的两种树- ...

  2. 详解二叉搜索树的增删改查

    文章目录 二叉搜索树 1.1 定义 二.二叉搜索树基本结构 三.二叉搜索树的具体实现 3.1 插入节点 3.2 查询节点 3.3 删除节点(⭐️) 3.4 二叉搜索树的遍历 四.二叉搜索树的性能分析和 ...

  3. 五.树,二叉树,二叉搜索树(BST)和自平衡二叉搜索树(AVL)

    1.树 树是一种数据结构 比如:目录结构 树是一种可以递归定义的数据结构 树是由n个节点组成的集合: 如果 n=0, 那这是一颗空树 如果 n>0, 那存在1个节点作为树的根节点,其他节点可以分 ...

  4. 二叉平衡树(AVL树)详细理解

    二叉平衡树(AVL树) AVL树插入元素结论 单旋转: 双旋转: 如果看到后面会发现,我下面举得列子,类型一和类型三和我结论里面的有点不一样,那是因为类型一的节点4和类型三的节点14无论以何种方式都能 ...

  5. 看动画学算法之:平衡二叉搜索树AVL Tree

    简介 平衡二叉搜索树是一种特殊的二叉搜索树.为什么会有平衡二叉搜索树呢? 考虑一下二叉搜索树的特殊情况,如果一个二叉搜索树所有的节点都是右节点,那么这个二叉搜索树将会退化成为链表.从而导致搜索的时间复 ...

  6. 二叉搜索树(BST树)的简单实现

    #include <stdlib.h> template<typename T> class CBinSTree; template <typename T> cl ...

  7. 数据结构与算法——二叉平衡树(AVL树)详解

    文章目录 AVL树概念 不平衡概况 四种平衡旋转方式 RR平衡旋转(左单旋转) LL平衡旋转(右单旋转) RL平衡旋转(先右后左双旋转) LR平衡旋转(先左后右单旋转) java代码实现 总结 AVL ...

  8. C++泛型编程实现平衡二叉搜索树AVL

    代码如下: #include <iostream> using namespace std;template <typename T> struct AVLNode {type ...

  9. 二叉搜索树详解--实现插入和删除

    目录 BST树概念 BST树操作 BST树的查找 BST树的插入 BST树的删除 实现一个自己的BST树 BSTNode类和BSTree类 查找操作; 插入操作: 删除操作: 应用: 二叉搜索树性能分 ...

最新文章

  1. python 第六章 函数
  2. java log4j logback jcl_知识总结-Java日志框架Log4j、Log4j2、logback、slf4j、简介
  3. vs2003打开项目错误
  4. Kafka Without ZooKeeper ---- 不使用zookeeper的kafka集群
  5. DFS迷宫递归所有路径 新手入门
  6. html让ul的li自动居中,css ul li导航菜单居中问题解决方法
  7. [连载]《C#通讯(串口和网络)框架的设计与实现》- 13.中英文版本切换设计
  8. php中花括号的使用
  9. AngularJS:应用
  10. linux系统存储文件系统,Linux文件系统的深入分析
  11. Citrix,微软,VMware:它们的优缺点(资料整理汇集)
  12. Codeforces 1009E Intercity Travelling 【期望】【概率】
  13. 软件项目管理案例教程(第三版)习题及答案(适用于工程项目管理课程)
  14. 教程:使用Java以编程方式将PLT转换为PDF或图像
  15. SSM框架整合+案例
  16. Python沪深300成分股价值分析
  17. JAVA 逆向工程技术研究日志
  18. android 7.1 开机速度优化记录
  19. 4种实现Web前端可视化的常用方法
  20. 【Unity3D 问题总结】Failed to import package with error: Couldn‘t decompress package

热门文章

  1. idea+maven+ssm搭建boot_crm项目遇到的问题
  2. 快速安装puppeteer (跳过安装Chromium)
  3. ValueError: too many values to unpack
  4. 解析PHP跳出循环的方法以及continue、break、exit的区别介绍
  5. php数据库备份脚本
  6. 在查询的结果中添加自增列 两种方法
  7. 转:JNI jstring与c++字符串类型转换函数
  8. flower.php,flower.php
  9. mysql alter怎么用,mysql alter话语用法
  10. python3连接数据库失败_python3使用pymysql连接mysql数据库报Keyerror