您好,登錄后才能下訂單哦!
AVL樹的性質(zhì)
1. 左子樹和右子樹的高度之差的絕對值不超過1
2. 樹中的每個左子樹和右子樹都是AVL樹
3. 每個節(jié)點都有一個平衡因子(balance factor--bf),任一節(jié)點的平衡因子是-1,0,1。(每個節(jié)點的平衡因子等于右子樹的高度減去左子樹的高度 )
#pragma once template<class K, class V> struct AVLTreeNode { K _key; V _value; AVLTreeNode<K, V>* _left; AVLTreeNode<K, V>* _right; AVLTreeNode<K, V>* _parent; int _bf; //平衡因子 AVLTreeNode(const K& key, const V& value) : _key(key) , _value(value) , _left(NULL) , _right(NULL) , _parent(NULL) , _bf(0) {} }; template<class K, class V> class AVLTree { typedef AVLTreeNode<K, V> Node; public: AVLTree() : _root(NULL) {} ~AVLTree() {} public: //空樹 //查找位置 //插入節(jié)點 //更新平衡因子 //如果進行了旋轉(zhuǎn)調(diào)整,則將parent進行重新連接 bool Insert(const K& key, const V& value) { //空樹 if (_root == NULL) { _root = new Node(key, value); return true; } //查找位置 Node* cur = _root; Node* parent = NULL; while (cur) { if (key > cur->_key) { parent = cur; cur = cur->_right; } else if (key < cur->_key) { parent = cur; cur = cur->_left; } else { return false; } } //插入節(jié)點 cur = new Node(key, value); if (key > parent->_key) { parent->_right = cur; cur->_parent = parent; } else { parent->_left = cur; cur->_parent = parent; } //更新平衡因子(右樹-左樹) bool isRotate = false; //定義標志位,記錄是否旋轉(zhuǎn) while (parent) { if (parent->_left == cur)//插在parent的左邊,平衡因子減1 { parent->_bf--; } else //插在parent的右邊,平衡因子加1 { parent->_bf++; } if (parent->_bf == 0) break; else if (parent->_bf == 1 || parent->_bf == -1) { cur = parent; parent = cur->_parent; } else//旋轉(zhuǎn),調(diào)整平衡因子 2 -2 { isRotate = true; if (parent->_bf == 2) { if (cur->_bf == 1) { _RotateL(parent); } else //cur->_bf == -1 { _RotateRL(parent); } } else //parent->_bf == -2 { if (cur->_bf == -1) { _RotateR(parent); } else { _RotateLR(parent); } } break; } } if (isRotate) //true則表示進行了調(diào)整 { Node* GrandParent = parent->_parent; if (GrandParent == NULL) { _root = parent; } else { if (parent->_key < GrandParent->_key) { GrandParent->_left = parent; } else { GrandParent->_right = parent; } } } return true; } void InOrder() { _InOrder(_root); cout << endl; } bool IsBalanceTree() { return _IsBalanceTree(_root); } protected: bool _IsBalanceTree(Node* root) { if (root == NULL) { return true; } int left = _Height(root->_left); int right = _Height(root->_right); int bf = abs(right - left); if (bf > 1) { return false; } else if (bf != abs(root->_bf)) { cout << root->_bf << "平衡因子有誤!" << endl; return false; } return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right); } //左單旋 void _RotateL(Node*& parent) { Node* subR = parent->_right; Node* subRL = subR->_left; parent->_right = subRL; if (subRL) { subRL->_parent = parent; } subR->_left = parent; subR->_parent = parent->_parent; parent->_parent = subR; parent->_bf = subR->_bf = 0; parent = subR; } void _RotateR(Node*& parent) { Node* subL = parent->_left; Node* subLR = subL->_right; parent->_left = subLR; if (subLR) { subLR->_parent = parent; } subL->_right = parent; subL->_parent = parent->_parent; parent->_parent = subL; parent->_bf = subL->_bf = 0; parent = subL; } void _RotateLR(Node*& parent) { Node* subL = parent->_left; Node* subLR = subL->_right; // 左單旋 subL->_right = subLR->_left; if (subLR->_left) { subLR->_left->_parent = subL; } subLR->_left = subL; subLR->_parent = subL->_parent; subL->_parent = subLR; if (subLR->_bf == 0 || subLR->_bf == -1) { subL->_bf = 0; } else // subLR->_bf == 1 { subL->_bf = -1; } // 右單旋 parent->_left = subLR->_right; if (subLR->_right) { subLR->_right->_parent = parent; } subLR->_right = parent; subLR->_parent = parent->_parent; parent->_parent = subLR; if (subLR->_bf == 0 || subLR->_bf == 1) { parent->_bf = 0; } else // subLR->_bf == -1 { parent->_bf = 1; } subLR->_bf = 0; parent = subLR; } void _RotateRL(Node*& parent) { Node* subR = parent->_right; Node* subRL = subR->_left; subR->_left = subRL->_right; if (subRL->_right) { subRL->_right->_parent = subR; } subRL->_right = subR; subR->_parent = subRL; if (subRL->_bf == 0 || subRL->_bf == 1) { subR->_bf = 0; } else { subR->_bf = 1; } parent->_right = subRL->_left; if (subRL->_left) { subRL->_left->_parent = parent; } subRL->_left = parent; subRL->_parent = parent->_parent; parent->_parent = subRL; if (subRL->_bf == 0 || subRL->_bf == -1) { parent->_bf = 0; } else { parent->_bf = -1; } subRL->_bf = 0; parent = subRL; } void _InOrder(Node* root) { if (root == NULL) { return; } _InOrder(root->_left); cout << root->_key << " "; _InOrder(root->_right); } int _Height(Node* root) { if (root == NULL) { return 0; } int left = _Height(root->_left) + 1; int right = _Height(root->_right) + 1; return left > right ? left : right; } protected: Node* _root; }; void TestAVL1() { AVLTree<int, int> t; int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 }; for (int i = 0; i < sizeof(a) / sizeof(int); ++i) { t.Insert(a[i], i); } t.InOrder(); cout << "IsBlance?" << t.IsBalanceTree() << endl; } void TestAVL2() { AVLTree<int, int> t; int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 }; for (int i = 0; i < sizeof(a) / sizeof(int); ++i) { t.Insert(a[i], i); t.InOrder(); } cout << "IsBlance?" << t.IsBalanceTree() << endl; }
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。