目录
如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在O(log2 n),搜索时间复杂度O(log2 n)
平衡因子不是必须要维护的,在操作时也可以直接通过高度函数来算,只不过比较麻烦
只有满二叉树才能保证每个子树高度差是0(2^h-1)
做不到相等,退而求其次,左右高度差不超过1
完全二叉树:最后一层缺一些节点
AVL二叉树:最后两层缺一些节点
右边高--左旋转
旋转原则:
1、保持搜索树的规则
2、子树变平衡
左单旋的条件:(30)parent->_bf = 2,(60)subR->_bf = 1 (_bf是平衡因子,subR是parent的右孩子)
左单旋规则:对30进行左单旋,60必须是30的右支,父亲30是儿子60的左支就是左旋,右支就是右旋(如果在b下加子节点 就等价 先右后左双旋的情况2了,图在下面)
左单旋:(右边高)在c下插入一个节点时,把b给根30右节点,再把30给60的左节点。
30这棵树可能是局部的子树,上面还有根节点ppNode,更新完30开始的这颗子树的平衡因子后不必再向上更新,因为30为根的这个树旋转前高度是h+2(30,60,h),旋转后高度还是h+2 没变
(如果在b下加子节点 就等价 先右后左双旋的情况2了)
- #pragma once
-
- template<class K, class V>
- struct AVLTreeNode
- {
- pair
_kv; - AVLTreeNode
* _left; - AVLTreeNode
* _right; - AVLTreeNode
* _parent; -
- // 右子树-左子树的高度差
- int _bf; // balance factor
-
- AVLTreeNode(const pair
& kv) - :_kv(kv)
- , _left(nullptr)
- , _right(nullptr)
- , _parent(nullptr)
- , _bf(0)
- {}
-
- // AVL树并没有规定必须要设计平衡因子
- // 只是一个实现的选择,方便控制平衡
- };
-
- template<class K, class V>
- class AVLTree
- {
- typedef AVLTreeNode
Node; - public:
- bool Insert(const pair
& kv) - {
- // 1、搜索树的规则插入
- // 2、看是否违反平衡规则,如果违反就需要处理:旋转
- if (_root == nullptr)
- {
- _root = new Node(kv);
- _root->_bf = 0;
- return true;
- }
-
- Node* parent = nullptr;
- Node* cur = _root;
- while (cur)
- {
- if (cur->_kv.first < kv.first)
- {
- parent = cur;
- cur = cur->_right;
- }
- else if (cur->_kv.first > kv.first)
- {
- parent = cur;
- cur = cur->_left;
- }
- else
- {
- return false;
- }
- }
-
- cur = new Node(kv);
- if (parent->_kv.first < kv.first)
- {
- parent->_right = cur;
- }
- else
- {
- parent->_left = cur;
- }
-
- cur->_parent = parent;
-
- // ...
- // 更新平衡因子
- while (parent) // 最远要更新根
- {
- if (cur == parent->_right)
- {
- parent->_bf++;
- }
- else
- {
- parent->_bf--;
- }
-
- // 是否继续更新?
- if (parent->_bf == 0) // 1 or -1 -》 0 插入节点填上矮的那边
- {
- // 高度不变,更新结束
- break;
- }
- else if (parent->_bf == 1 || parent->_bf == -1)
- // 0 -》 1 或 -1 插入节点导致一边变高了
- {
- // 子树的高度变了,继续更新祖先
- cur = cur->_parent;
- parent = parent->_parent;
- }
- else if (parent->_bf == 2 || parent->_bf == -2)
- // -1 or 1 -》 2 或 -2 插入节点导致本来高一边又变高了
- {
- // 子树不平衡 -- 需要旋转处理
- // ...
- }
- else
- {
- // 插入之前AVL就存在不平衡子树,|平衡因子| >= 2的节点
- assert(false);
- }
- }
-
- return true;
- }
- private:
- void RotateL(Node* parent)
- {
- Node* subR = parent->_right;
- Node* subRL = subR->_left;
-
- parent->_right = subRL;
- if (subRL)
- subRL->_parent = parent;
-
- Node* ppNode = parent->_parent;
-
- subR->_left = parent;
- parent->_parent = subR;
-
- if (parent == _root)
- {
- _root = subR;
- _root->_parent = nullptr;
- }
- else
- {
- if (parent == ppNode->_left)
- {
- ppNode->_left = subR;
- }
- else
- {
- ppNode->_right = subR;
- }
-
- subR->_parent = ppNode;
- }
- }
-
- private:
- Node* _root = nullptr;
- };
-
- void TestAVLTree()
- {
- AVLTree<int, int> t;
- t.Insert(make_pair(1, 1));
- t.Insert(make_pair(2, 2));
- t.Insert(make_pair(3, 3));
- }
右单旋的条件:(60)parent->_bf = -2,(60)subL->_bf = -1
动的就是parent 60,subL 30,subLR b
示例:
代码:
- void RotateR(Node* parent)
- {
- Node* subL = parent->_left;
- Node* subLR = subL->_right;
-
- parent->_left = subLR;
- if (subLR)
- subLR->_parent = parent;
-
- Node* ppNode = parent->_parent;
-
- subL->_right = parent;
- parent->_parent = subL;
-
- if (parent == _root)
- {
- _root = subL;
- _root->_parent = nullptr;
- }
- else
- {
- if (ppNode->_left == parent)
- {
- ppNode->_left = subL;
- }
- else
- {
- ppNode->_right = subL;
- }
- subL->_parent = ppNode;
- }
-
- subL->_bf = parent->_bf = 0;
- }
AVL树的双旋转使用对应的单旋转后还要更新平衡因子
先左后右双旋的条件:parent->_bf = -2,subL->_bf = 1,不用管subLR是几
树 h>=1
相当于是把60的左给30的右,把60的右给90的左,30,90作左右子树,60再做根节点
h==0,先左后右 双旋最简单的情况
先右后左双旋的条件:parent->_bf = 2,subR->_bf = -1,不用管subRL是几
- #pragma once
- #include
- #include
- #include
- #include
-
- template<class K, class V>
- struct AVLTreeNode
- {
- pair
_kv; - AVLTreeNode
* _left; - AVLTreeNode
* _right; - AVLTreeNode
* _parent; -
- // 右子树-左子树的高度差
- int _bf; // balance factor
-
- AVLTreeNode(const pair
& kv) - :_kv(kv)
- , _left(nullptr)
- , _right(nullptr)
- , _parent(nullptr)
- , _bf(0)
- {}
-
- // AVL树并没有规定必须要设计平衡因子
- // 只是一个实现的选择,方便控制平衡
- };
-
- template<class K, class V>
- class AVLTree
- {
- typedef AVLTreeNode
Node; - public:
- // Find
- // Erase
- bool Insert(const pair
& kv) - {
- // 1、搜索树的规则插入
- // 2、看是否违反平衡规则,如果违反就需要处理:旋转
- if (_root == nullptr)
- {
- _root = new Node(kv);
- _root->_bf = 0;
- return true;
- }
-
- Node* parent = nullptr;
- Node* cur = _root;
- while (cur)
- {
- if (cur->_kv.first < kv.first)
- {
- parent = cur;
- cur = cur->_right;
- }
- else if (cur->_kv.first > kv.first)
- {
- parent = cur;
- cur = cur->_left;
- }
- else
- {
- return false;
- }
- }
-
- cur = new Node(kv);
- if (parent->_kv.first < kv.first)
- {
- parent->_right = cur;
- }
- else
- {
- parent->_left = cur;
- }
-
- cur->_parent = parent;
-
- // ...
- // 更新平衡因子
- while (parent) // 最远要更新根
- {
- if (cur == parent->_right)
- {
- parent->_bf++;
- }
- else
- {
- parent->_bf--;
- }
-
- // 是否继续更新?
- if (parent->_bf == 0) // 1 or -1 -》 0 插入节点填上矮的那边
- {
- // 高度不变,更新结束
- break;
- }
- else if (parent->_bf == 1 || parent->_bf == -1)
- // 0 -》 1 或 -1 插入节点导致一边变高了
- {
- // 子树的高度变了,继续更新祖先
- cur = cur->_parent;
- parent = parent->_parent;
- }
- else if (parent->_bf == 2 || parent->_bf == -2)
- // -1 or 1 -》 2 或 -2 插入节点导致本来高一边又变高了
- {
- // 子树不平衡 -- 需要旋转处理
- if (parent->_bf == 2 && cur->_bf == 1) // 左单旋
- {
- RotateL(parent);
- }
- else if (parent->_bf == -2 && cur->_bf == -1) // 右单旋
- {
- RotateR(parent);
- }
- else if (parent->_bf == -2 && cur->_bf == 1) // 左右双旋
- {
- RotateLR(parent);
- }
- else if (parent->_bf == 2 && cur->_bf == -1) // 右左双旋
- {
- RotateRL(parent);
- }
-
- break;
- }
- else
- {
- // 插入之前AVL就存在不平衡子树,|平衡因子| >= 2的节点
- assert(false);
- }
- }
-
- return true;
- }
- private:
- void RotateL(Node* parent)
- {
- Node* subR = parent->_right;
- Node* subRL = subR->_left;
-
- parent->_right = subRL;
- if (subRL)
- subRL->_parent = parent;
-
- Node* ppNode = parent->_parent;
-
- subR->_left = parent;
- parent->_parent = subR;
-
- if (parent == _root)
- {
- _root = subR;
- _root->_parent = nullptr;
- }
- else
- {
- if (parent == ppNode->_left)
- {
- ppNode->_left = subR;
- }
- else
- {
- ppNode->_right = subR;
- }
-
- subR->_parent = ppNode;
- }
-
- // 更新平衡因子
- parent->_bf = 0;
- subR->_bf = 0;
- }
-
- void RotateR(Node* parent)
- {
- Node* subL = parent->_left;
- Node* subLR = subL->_right;
-
- parent->_left = subLR;
- if (subLR)
- subLR->_parent = parent;
-
- Node* ppNode = parent->_parent;
-
- subL->_right = parent;
- parent->_parent = subL;
-
- if (parent == _root)
- {
- _root = subL;
- _root->_parent = nullptr;
- }
- else
- {
- if (ppNode->_left == parent)
- {
- ppNode->_left = subL;
- }
- else
- {
- ppNode->_right = subL;
- }
- subL->_parent = ppNode;
- }
-
- subL->_bf = parent->_bf = 0;
- }
-
- void RotateLR(Node* parent)
- {
- Node* subL = parent->_left;
- Node* subLR = subL->_right;
- int bf = subLR->_bf;
-
- RotateL(parent->_left);
- RotateR(parent);
-
- // 更新平衡因子
- if(bf == 0)
- {
- parent->_bf = 0;
- subL->_bf = 0;
- subLR->_bf = 0;
- }
- else if (bf == 1)
- {
- parent->_bf = 0;
- subL->_bf = -1;
- subLR->_bf = 0;
- }
- else if (bf == -1)
- {
- parent->_bf = 1;
- subL->_bf = 0;
- subLR->_bf = 0;
- }
- else
- {
- // subLR->_bf旋转前就有问题
- assert(false);
- }
- }
-
- void RotateRL(Node* parent)
- {
- Node* subR = parent->_right;
- Node* subRL = subR->_left;
- int bf = subRL->_bf;
-
- RotateR(parent->_right);
- RotateL(parent);
-
- if (bf == 0)
- {
- subRL->_bf = 0;
- parent->_bf = 0;
- subR->_bf = 0;
- }
- else if (bf == 1)
- {
- subRL->_bf = 0;
- parent->_bf = -1;
- subR->_bf = 0;
- }
- else if (bf == -1)
- {
- subRL->_bf = 0;
- parent->_bf = 0;
- subR->_bf = 1;
- }
- else
- {
- // subLR->_bf旋转前就有问题
- assert(false);
- }
- }
-
- void _InOrder(Node* root)
- {
- if (root == nullptr)
- return;
-
- _InOrder(root->_left);
- cout << root->_kv.first <<" ";
- _InOrder(root->_right);
- }
-
- int _Height(Node* root)
- {
- if (root == nullptr)
- return 0;
-
- int lh = _Height(root->_left);
- int rh = _Height(root->_right);
-
- return lh > rh ? lh + 1 : rh + 1;
- }
-
- bool _IsBalanceTree(Node* root)
- {
- // 空树也是AVL树
- if (nullptr == root)
- return true;
-
- // 计算pRoot节点的平衡因子:即pRoot左右子树的高度差
- int leftHeight = _Height(root->_left);
- int rightHeight = _Height(root->_right);
- int diff = rightHeight - leftHeight;
-
- // 如果计算出的平衡因子与pRoot的平衡因子不相等,或者
- // pRoot平衡因子的绝对值超过1,则一定不是AVL树
- if (abs(diff) >= 2)
- {
- cout << root->_kv.first << "节点平衡因子异常" << endl;
- return false;
- }
-
- if (diff != root->_bf)
- {
- cout << root->_kv.first << "节点平衡因子不符合实际" << endl;
- return false;
- }
-
- // pRoot的左和右如果都是AVL树,则该树一定是AVL树
- return _IsBalanceTree(root->_left)
- && _IsBalanceTree(root->_right);
- }
-
- public:
-
- void InOrder()
- {
- _InOrder(_root);
- cout << endl;
- }
-
- vector
int>> levelOrder() { - vector
int>> vv; - if (_root == nullptr)
- return vv;
-
- queue
q; - int levelSize = 1;
- q.push(_root);
-
- while (!q.empty())
- {
- // levelSize控制一层一层出
- vector<int> levelV;
- while (levelSize--)
- {
- Node* front = q.front();
- q.pop();
- levelV.push_back(front->_kv.first);
- if (front->_left)
- q.push(front->_left);
-
- if (front->_right)
- q.push(front->_right);
- }
- vv.push_back(levelV);
- for (auto e : levelV)
- {
- cout << e << " ";
- }
- cout << endl;
-
- // 上一层出完,下一层就都进队列
- levelSize = q.size();
- }
-
- return vv;
- }
-
- bool IsBalanceTree()
- {
- return _IsBalanceTree(_root);
- }
-
- int Height()
- {
- return _Height(_root);
- }
- private:
- Node* _root = nullptr;
- };
-
- void TestAVLTree1()
- {
- //int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
- int a[] = { 30,29,28,27,26,25,24,11,8,7,6,5,4,3,2,1 };
- AVLTree<int, int> t;
- for (auto e : a)
- {
- t.Insert(make_pair(e, e));
- }
- t.levelOrder();
- }
-
- void TestAVLTree2()
- {
- const size_t N = 1024*1024*10;
- vector<int> v;
- v.reserve(N);
- srand(time(0));
- for (size_t i = 0; i < N; ++i)
- {
- //v.push_back(rand());
- v.push_back(i);
- }
-
- AVLTree<int, int> t;
- for (auto e : v)
- {
- t.Insert(make_pair(e, e));
- }
-
- //t.levelOrder();
- //cout << endl;
- cout << "是否平衡?" << t.IsBalanceTree() << endl;
- cout << "高度:" << t.Height() << endl;
-
-
- //t.InOrder();
- }