详解 二叉搜索树-----AVL树
二叉搜索樹
具有以上的特征的二叉樹就是二叉搜索樹也叫二叉排序數
二叉搜索樹的操作
查找
要保存查找值的雙親,以便于后續執行插入操作
插入
刪除
首先查找元素是否在二叉搜索樹中,如果不存在,則返回, 否則要刪除的結點可能分下面四種情況:
- 要刪除的結點無孩子結點
- 要刪除的結點只有左孩子結點
- 要刪除的結點只有右孩子結點
- 要刪除的結點有左、右孩子結點
總結一下,實際情況要刪除的話,分為三步
時間復雜度
插入和刪除操作都必須先查找,查找效率代表了二叉搜索樹中各個操作的性能。
對有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樹的插入 過程可以分為兩步:
平衡因子的判定
旋轉處理
如果parent的平衡因子是2或者-2,需要對以parent為根的二叉樹進行旋轉處理:
- 左單旋
- 右單旋
- 左右雙旋
- 右左雙旋
左單旋
新結點插入到較高右子樹的右側----右右---->左單旋
上圖在插入前,AVL樹是平衡的,新節點插入到30的左子樹(注意:此處不是左孩子)中,30左子樹增加 了一層,導致以60為根的二叉樹不平衡,要讓60平衡,只能將60左子樹的高度減少一層,右子樹增加一 層
即將左子樹往上提,這樣60轉下來,因為60比30大,只能將其放在30的右子樹,而如果30有右子樹,右 子樹根的值一定大于30,小于60,只能將其放在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,分以下情況考慮
AVL樹的驗證
AVL樹是在二叉搜索樹的基礎上加入了平衡性的限制,因此要驗證AVL樹,可以分兩步:
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树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关联式容器(map,set,multim
- 下一篇: vue怎么引入jq??? 财富值2