日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

详解 二叉搜索树-----AVL树

發布時間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详解 二叉搜索树-----AVL树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二叉搜索樹

  • 根結點比左子樹中所有結點都大
  • 根結點比右子樹所有結點都小
  • 最小的元素在最左側
  • 最大的元素在最右側
  • 中序遍歷有序
  • 具有以上的特征的二叉樹就是二叉搜索樹也叫二叉排序數

    二叉搜索樹的操作

    查找



    要保存查找值的雙親,以便于后續執行插入操作

    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;}

    插入

  • 樹為空,則直接插入
  • 樹不空,按二叉搜索樹性質查找插入位置,插入新節點
  • 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;}

    刪除

    首先查找元素是否在二叉搜索樹中,如果不存在,則返回, 否則要刪除的結點可能分下面四種情況:

    • 要刪除的結點無孩子結點
    • 要刪除的結點只有左孩子結點
    • 要刪除的結點只有右孩子結點
    • 要刪除的結點有左、右孩子結點

    總結一下,實際情況要刪除的話,分為三步

  • 刪除該結點且使被刪除節點的雙親結點指向被刪除節點的左孩子結點
  • 刪除該結點且使被刪除節點的雙親結點指向被刪除結點的右孩子結點
  • :在它的右子樹中尋找中序下的第一個結點(關鍵碼最小),用它的值填補到被刪除節點中, 再來處理該結點的刪除問題

  • //刪除 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樹的插入 過程可以分為兩步:

  • 按照二叉搜索樹的方式插入新節點
  • 調整節點的平衡因子
  • 平衡因子的判定

  • 先按照二叉搜索樹的規則將節點插入到AVL樹中
  • 新節點插入后,AVL樹的平衡性可能會遭到破壞,此時就需要更新平衡因子,并檢測是否破壞了AVL樹的平衡性
  • pCur插入后,pParent的平衡因子一定需要調整,在插入之前,pParent的平衡因子分為三種情況:-1,0, 1, 分以下兩種情況:
  • 1. 如果pCur插入到pParent的左側,只需給pParent的平衡因子-1即可 2. 如果pCur插入到pParent的右側,只需給pParent的平衡因子+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的左子樹,旋轉完成后,更新節點的平衡因子即可。在旋轉過程中,有以下幾種情況需要考慮:

  • 30節點的右孩子可能存在,也可能不存在
  • 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,分以下情況考慮

  • pParent的平衡因子為2,說明pParent的右子樹高,設pParent的右子樹的根為pSubR
  • 1.當pSubR的平衡因子為1時,執行左單旋 2.當pSubR的平衡因子為-1時,執行右左雙旋
  • pParent的平衡因子為-2,說明pParent的左子樹高,設pParent的左子樹的根為pSubL
  • 1.當pSubL的平衡因子為-1是,執行右單旋 2.當pSubL的平衡因子為1時,執行左右雙旋

    AVL樹的驗證

    AVL樹是在二叉搜索樹的基礎上加入了平衡性的限制,因此要驗證AVL樹,可以分兩步:

  • 驗證其為二叉搜索樹
  • 如果中序遍歷可得到一個有序的序列,就說明為二叉搜索樹
  • 驗證其為平衡樹
  • 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树的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。