标签:平衡
一.AVL树的性质
左子树和右子树的高度之差的绝对值不超过1;
树中的每个左子树和右子树都是AVL树。
二.代码实现
#include<iostream> using namespace std; template<class K,class V> struct AVLTreeNode { AVLTreeNode<K, V>* _left; AVLTreeNode<K, V>* _right; AVLTreeNode<K, V>* _parent; K _key; V _value; int _bf;//平衡因子 AVLTreeNode(const K& key,const V& value) : _left(NULL) , _right(NULL) , _parent(NULL) , _key(key) , _value(value) , _bf(0) {} }; template<class K,class V> class AVLTree { typedef AVLTreeNode<K, V> Node; public: AVLTree() :_root(NULL) {} ~AVLTree() {} bool Insert(const K& key, const V& value) { Node* cur = _root; Node* parent = NULL; //找节点的位置 if (_root == NULL) { _root = new Node(key, value); return true; } while (cur) { if (key > cur->_key) { parent = cur; cur = cur->_right; } else if (key < cur->_key) { parent = cur; cur = cur->_left; } else { return false; } } //找到了,插入 cur = new Node(key, value); if (key > parent->_key) { parent->_right = cur; cur->_parent = parent; } else if (key < parent->_key) { parent->_left = cur; cur->_parent = parent; } //调整平衡因子 while (parent) { //插入在左子数,parent的平衡因子-1 if (cur == parent->_left) { parent->_bf -= 1; } //插入在左子数,parent的平衡因子 + 1 else { parent->_bf += 1; } //如果插入节点的父节点平衡因子为0,停止更新平衡因子 if (parent->_bf == 0) { break; } //如果插入节点的父节点平衡因子为+1、-1,向上更新 else if (parent->_bf == 1 || parent->_bf == -1) { cur = parent; parent = parent->_parent; } //如果插入节点的父节点平衡因子为+2、-2,旋转 else { //根据旋转因子判断旋转类型 if (parent->_bf == 2) { //左单旋 if (cur->_bf == 1) { RotateL(parent); } //右左旋 -1 else { RotateRL(parent); } } //-2 else { //右单旋 if (cur->_bf == -1) { RotateR(parent); } //左右旋 else { RotateLR(parent); } } break; } } return true; } //parent一定不为空 void RotateL(Node* parent) { Node* subR = parent->_right; Node* subRL = subR->_left; parent->_right = subRL; if (subRL) { subRL->_parent = parent; } //保存parent->_parent Node* ppNode = parent->_parent; subR->_left = parent; parent->_parent = subR; //判断ppNode,确定subR->_parent是谁 if (ppNode == NULL) { _root = subR; subR->_parent = NULL; } else { subR->_parent = ppNode; if (ppNode->_left==parent) { ppNode->_left = subR; } else { ppNode->_right = subR; } } subR->_bf = parent->_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 (ppNode == NULL) { _root = subL; subL->_parent = NULL; } else { subL->_parent = ppNode; if (ppNode->_left == parent) { ppNode->_left = subL; } else { ppNode->_right = subL; } } subL->_bf = parent->_bf = 0; } void RotateRL(Node* parent) { Node* subR = parent->_right; Node* subRL = subR->_left; int bf = subRL->_bf; RotateR(parent->_right); RotateL(parent); //处理平衡因子,可以结合图,如下图1、2 if (bf == 1) { parent->_bf = -1; subR->_bf = 0; } else if (bf == -1) { parent->_bf = 0; subR->_bf = 1; } else//0 { parent->_bf = subR->_bf = 0; } subRL->_bf = 0; } void RotateLR(Node* parent) { Node* subL = parent->_left; Node* subLR = subL->_right; int bf = subLR->_bf; RotateL(parent->_left); RotateR(parent); //插在subLR的右边 if (bf == 1) { parent->_bf = 0; subL->_bf = -1; } //插在subLR的左边 else if (bf == -1) { parent->_bf = 1; subL->_bf = 0; } //没插 else { parent->_bf = subL->_bf = 0; } //旋转后subLR为这棵树的根,无论哪种情况,平衡因子为0 subLR->_bf = 0; } bool IsBlance() { return _IsBlance(_root); } int Height(Node* root) { if (root == NULL) { return 0; } int left = Height(root->_left); int right = Height(root->_right); return left > right ? left +1: right+1;//加根节点 } void InOrder() { _InOrder(_root); cout << endl; } protected: Node* _root; bool _IsBlance(Node* root) { if (root == NULL) return true; int left = Height(root->_left); int right = Height(root->_right); //平衡因子异常 if ((right - left) != root->_bf && (abs((right - left) >= 2)))//当前树满足,还需递归求子树是否满足 { cout << "平衡因子异常" << root->_key << endl; return false; } return (_IsBlance(root->_left)) && (_IsBlance(root->_right)); //递归求子树是否满足 } void _InOrder(Node*& root) { if (root == NULL) return; _InOrder(root->_left); cout << root->_key << " "; _InOrder(root->_right); } }; void Test1() { int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 }; AVLTree<int, int> avl; for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { avl.Insert(a[i], i); } avl.InOrder(); cout << "Is Blance?" << avl.IsBlance() << endl; } void Test2() { int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 }; AVLTree<int, int> avl; for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { avl.Insert(a[i], i); } avl.InOrder(); cout << "Is Blance?" << avl.IsBlance() << endl; } int main() { Test2(); system("pause"); return 0; }
三.补充(图)
本文出自 “sunshine225” 博客,请务必保留此出处http://10707460.blog.51cto.com/10697460/1827973
标签:平衡
原文地址:http://10707460.blog.51cto.com/10697460/1827973