一.AVL树的四种旋转方式
以上是AVL树插入和删除时需要用到的四种旋转方式。为什么要旋转?因为树不平衡了,通过旋转使其再次平衡。 但是上面的四副图在旋转前就是平衡的,所以这样的旋转是没有意义的,重点在于理解旋转的方法。下面的插入和删除操作会用到这四种方法,都是在这四副图的基础上变化。
具体如何选择旋转方法呢?记住以下几个技巧:
- 当某个结点(parent)的平衡因子绝对值大于1时(即2或-2)时,需要进行旋转
- child永远指向parent高的那颗子树
- parent和child都是左边高时右单旋,都是右边高时左单旋
- child左边高,parent右边高时进行右左单旋;child右边高,parent左边高时进行左右单旋
- 当进行插入和删除操作后,只可能影响被操作结点到根的路径上的所有结点的平衡因子,其余结点不受影响。
- 一个结点的平衡因子是否会受到影响,关键看它的子树高度是否发生变化。
二.AVL树的插入操作
parent和child都是左边高,右单旋,插入前和旋转后,这颗树的高度不变(h+1),故旋转后不用向上更新平衡因子
parent和child都是右边高,左单旋,插入前和旋转后,这颗树的高度不变(h+1),故旋转后不用向上更新平衡因子
parent右边高,child左边高,右左单旋,插入前和旋转后,这颗树的高度不变(h+2),故旋转后不用向上更新平衡因子
parent左高,child右边高,左右单旋,插入前和旋转后,这颗树的高度不变(h+2),故旋转后不用向上更新平衡因子
三.AVL树的删除操作
parent左边高,child两边一样高,右单旋。插入前和旋转后,这棵树的高度不变(h+2),故旋转后不用向上更新平衡因子
parent和child都是左边高,右单旋。插入前和旋转后,这棵树的高度发生变化(h+2->h+1),故旋转后需要向上更新平衡因子
parent右边高,child两边一样高,左单旋。插入前和旋转后,这棵树的高度不变(h+2),故旋转后需要向上更新平衡因子
parent和child都是右边高,左单旋。插入前和旋转后,这棵树的高度发生变化(h+2->h+1),故旋转后需要向上更新平衡因子
parent左边高,child右边高,左右双旋。插入前和旋转后,这棵树的高度发生变化(h+2->h+1),故旋转后需要向上更新平衡因子
parent右边高,child左边高,右左双旋。插入前和旋转后,这棵树的高度发生变化(h+2->h+1),故旋转后需要向上更新平衡因子
四.参考代码
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;template <class K, class V>
struct AVLTreeNode
{AVLTreeNode(const pair<K, V>& kv):_kv(kv),_bf(0),_left(nullptr),_right(nullptr),_parent(nullptr){}pair<K, V> _kv;int _bf = 0;//平衡因子AVLTreeNode* _left = nullptr;AVLTreeNode* _right = nullptr;AVLTreeNode* _parent = nullptr;
};template <class K, class V>
class AVLTree
{typedef AVLTreeNode<K, V> Node;
public:AVLTree() = default;//查找Node* find(const K& key){Node* cur = _root;while (cur){if (cur->_kv.first == key){return cur;}else if (key > cur->_kv.first){cur = cur->_right;}else{cur = cur->_left;}}return nullptr;}//插入bool insert(const pair<K, V>& kv){if (_root == nullptr){//printf("插入根结点%d\n", kv.first);_root = new Node(kv);return true;}Node* cur = _root;Node* parent = nullptr;while (cur){if (cur->_kv == kv){return false;}else if (kv.first > cur->_kv.first){parent = cur;cur = cur->_right;}else{parent = cur;cur = cur->_left;}}//找到空结点了,开始插入cur = new Node(kv);cur->_parent = parent;if (kv.first < parent->_kv.first){parent->_left = cur;}else{parent->_right = cur;}//以parent为起点,向上更新平衡因子,必要时进行调整//一个结点的平衡因子是否被影响,只需看它的子树高度是否发生变化while (parent){if (cur == parent->_left){parent->_bf--;}else{parent->_bf++;}if (parent->_bf == 0)//-1->0或者1->0,说明以parent为根结点的这颗树的高度没有变化,不用向上更新平衡因子了{break;}else if (parent->_bf == 1 || parent->_bf == -1)//说明以parent为根结点的这颗子树高度+1,需要继续向上跟新平衡因子{cur = parent;parent = parent->_parent;}else if (parent->_bf == 2 || parent->_bf == -2)//parent这个结点已经不平衡了,需要调整{if (cur->_bf == 1 && parent->_bf == 2){//左单旋--parent和cur都是右边高rotateL(parent);}else if (cur->_bf == -1 && parent->_bf == -2){//右单旋--parent和cur都是左边高rotateR(parent);}else if (cur->_bf == -1 && parent->_bf == 2){//右左双旋--cur左边高,parent右边高rotateRL(parent);}else if (cur->_bf == 1 && parent->_bf == -2){//左右双旋rotateLR(parent);}break;//调整后不用往上更新平衡因子了,因为以parent为根结点的这棵树插入前高度和调整后高度一样}else{cout << "平衡因子出错" << endl;assert(false);}}return true;}bool erase(const K& key){//先找到它Node* cur = find(key);if (cur == nullptr){return false;}if (key == 9798){int i = 0;}//如果cur有两个孩子,则用右子树的最左结点替换它if (cur->_left && cur->_right){Node* target = cur->_right;while (target->_left){target = target->_left;}swap(cur->_kv, target->_kv);cur = target;}//删除curNode* parent = cur->_parent;Node* lChild = cur->_left;Node* rChild = cur->_right;Node* child = nullptr;//1.cur右孩子为空if (rChild == nullptr){child = lChild;}//2.cur左孩子为空else if (lChild == nullptr){child = rChild;}else{cout << "替换结点有误" << endl;assert(false);}//parent和child构建关系//1.被删的cur是根结点if (parent == nullptr){_root = child;}//2.被删的是一个普通结点,cur的父亲不为空else{if (cur == parent->_left){parent->_left = child;parent->_bf++;}else{parent->_right = child;parent->_bf--;}}if (child){child->_parent = parent;}delete cur;//开始更新平衡因子bool flag = false;while (parent){//第一次进来不更新平衡因子if (flag){if (child == parent->_left){parent->_bf++;}else{parent->_bf--;}}flag = true;if (parent->_bf == 1 || parent->_bf == -1)//还是平衡的,树高度没有变,不需要向上处理{break;}else if (parent->_bf == 0)//高那颗子树删除了一个结点,树的高度降低,继续向上更新{child = parent;parent = parent->_parent;}else if (parent->_bf == 2 || parent->_bf == -2)//不平衡了,需要调整{//child指向高的那颗子树if (parent->_bf > 0){child = parent->_right;}else{child = parent->_left;}assert(child);Node* grandpa = parent->_parent;//提前记录parent的父节点,否则旋转后就找不到了//child和parent都是同一边高,或者child两边一样高,单旋即可if (child->_bf * parent->_bf >= 0){int bf = child->_bf;if (parent->_bf == 2){rotateL(parent);if (bf == 0){parent->_bf = 1;child->_bf = -1;}}else{rotateR(parent);if (bf == 0){parent->_bf = -1;child->_bf = 1;}}//判断是否还需要向上更新平衡因子//--当child原来的_bf = 0时parent这颗树删除前和调整后高度不变if (bf == 0){break;}child = parent->_parent;parent = grandpa;}//parent和child的平衡因子符号不同--需要双旋else{if (parent->_bf == 2 && child->_bf == -1){rotateRL(parent);}else if (parent->_bf == -2 && child->_bf == 1){rotateLR(parent);}child = parent->_parent;parent = grandpa;}}else{cout << "平衡因子出错" << endl;assert(false);}}return true;}void inOrder(){_inOrder(_root);}bool isBalance(){return _isBalance(_root);}int height(Node* root){if (root == nullptr){return 0;}return max(height(root->_left), height(root->_right)) + 1;}protected:void rotateL(Node* parent){Node* grandfather = parent->_parent;Node* subR = parent->_right;Node* subRL = subR->_left;//grandfather和subRsubR->_parent = grandfather;if (grandfather == nullptr){_root = subR;}else{parent == grandfather->_left ? grandfather->_left = subR : grandfather->_right = subR;}//parent和subRLparent->_right = subRL;if (subRL){subRL->_parent = parent;}//subR和parentsubR->_left = parent;parent->_parent = subR;subR->_bf = parent->_bf = 0;}void rotateR(Node* parent){Node* grandpa = parent->_parent;Node* subL = parent->_left;Node* subLR = subL->_right;//grandpa和subLsubL->_parent = grandpa;if (grandpa == nullptr){_root = subL;}else{parent == grandpa->_left ? grandpa->_left = subL : grandpa->_right = subL;}//parent和subLRparent->_left = subLR;if (subLR){subLR->_parent = parent;}//subL和parentparent->_parent = subL;subL->_right = parent;subL->_bf = parent->_bf = 0;}void rotateRL(Node* parent)//--本质是将subRL作为根结点,parent和subR成为它的左右护法,并将subRL的左右子树分给它们{Node* subR = parent->_right;Node* subRL = subR->_left;int bf = subRL->_bf;rotateR(subR);rotateL(parent);//更新平衡因子if (bf == 0)//subRL自己就是新插入的结点{subR->_bf = subRL->_bf = parent->_bf = 0;}else if (bf == 1)//在subRL的右子树插入结点{subR->_bf = subRL->_bf = 0;parent->_bf = -1;}else if (bf == -1)//在subRL的左子树插入结点{subRL->_bf = parent->_bf = 0;subR->_bf = 1;}}Node* _root = nullptr;void rotateLR(Node* parent){Node* subL = parent->_left;Node* subLR = subL->_right;int bf = subLR->_bf;rotateL(subL);rotateR(parent);//更新平衡因子if (bf == 0)//subLR自己就是新插入的结点{subLR->_bf = parent->_bf = subL->_bf = 0;}else if (bf == -1)//在subLR左子树插入结点{subLR->_bf = subL->_bf = 0;parent->_bf = 1;}else if (bf == 1)//在subLR右子树插入结点{subLR->_bf = parent->_bf = 0;subL->_bf = -1;}}void _inOrder(Node* root){if (root == nullptr){return;}_inOrder(root->_left);cout << root->_kv.first << ":" << root->_kv.second << " ";_inOrder(root->_right);}bool _isBalance(Node* root){if (root == nullptr){return true;}int leftHeight = height(root->_left);int rightHeight = height(root->_right);if (rightHeight - leftHeight != root->_bf){cout << root->_kv.first << "平衡因子出错:实际" << rightHeight - leftHeight << "记录" << root->_bf << endl;return false;}return abs(leftHeight - rightHeight) <= 1&& _isBalance(root->_left)&& _isBalance(root->_right);}
};
一些说明:
四个旋转函数包含了平衡因子的调节,这是以插入时的四种情况为准(因为我是先实现的插入),删除有几种情况平衡因子还需要自己调节。建议大家实现旋转函数时不包含平衡因子调节。