AVL樹的概念
AVL樹的插入
template<class K> struct AVLTreeNode { K _key; int _bf; AVLTreeNode <K, V>* _left; AVLTreeNode <K, V>* _right; AVLTreeNode <K, V>* _parent; AVLTreeNode( const K& key, const V& value) :_key(key), _bf( 0 ), _left(NULL), _right(NULL), _parent(NULL) {} };
程序代碼:
1 bool Insert(const K& key, const V& value) 2 { 3 if (_root == NULL) 4 { 5 _root = new Node(key, value); 6 return true ; 7 } 8 Node* pcur = _root; 9 Node* parent = NULL; 10 while (pcur) 11 { 12 if (pcur->_key == key) 13 return false ; 14 else if (pcur->_key < key) 15 { 16 parent = pcur; 17 pcur = pcur->_right; 18 } 19 else 20 { 21 parent = pcur; 22 pcur = pcur->_left; 23 } 24 } 25 if (parent->_key < key) 26 { 27 pcur = parent->_right = new Node(key, value); 28 pcur->_parent = parent; 29 } 30 else 31 { 32 pcur = parent->_left = new Node(key, value); 33 pcur->_parent = parent; 34 } 35 36 while (parent) 37 { 38 // 修正平衡因子 39 if (pcur == parent->_left) 40 { 41 parent->_bf--; 42 } 43 if (pcur == parent->_right) 44 { 45 parent->_bf++; 46 } 47 // 48 if (parent->_bf == 0 ) 49 break ; 50 else if (parent->_bf == -1 || parent->_bf == 1 ) 51 { 52 pcur = parent; 53 parent = pcur->_parent; 54 } 55 else // parent->bf -2 || 2 56 { 57 58 if (parent->_bf == -2 ) 59 { 60 if (pcur->_bf == -1 ) // 右單旋 61 RotateR(parent); 62 else // 左右雙旋 63 RotateLR(parent); 64 } 65 else 66 { 67 if (pcur->_bf == 1 ) // 左單旋 68 RotateL(parent); 69 else // 右左雙旋 70 RotateRL(parent); 71 } 72 break ; 73 } 74 } 75 return true ; 76 }
>>旋轉
1 void RotateR(Node* parent) 2 { 3 Node* subL = parent->_left; 4 Node* subLR = subL->_right; 5 // 換指向 6 parent->_left = subLR; 7 subL->_right = parent; 8 9 if (subLR) 10 { 11 subLR->_parent = parent; 12 } 13 14 Node* PParent = parent->_parent; // 判斷parent是否有父節點 15 if (PParent) 16 { 17 if (parent == PParent->_left) 18 { 19 PParent->_left = subL; 20 subL->_parent = PParent; 21 } 22 else 23 { 24 PParent->_right = subL; 25 subL->_parent = PParent; 26 } 27 } 28 else 29 { 30 _root = subL; 31 subL->_parent = NULL; 32 } 33 parent->_parent = subL; 34 // 修改bf 35 subL->_bf = 0 ; 36 parent->_bf = 0 ; 37 } 38 39 // 40 void RotateL(Node* parent) 41 { 42 Node* subR = parent->_right; 43 Node* subRL = subR->_left; 44 // 調整指向 45 subR->_left=parent; 46 parent->_right = subRL; 47 48 if (subRL) // 如果subRL非空 49 { 50 subRL->_parent = parent; 51 } 52 53 Node* PPNode = parent->_parent; 54 if (PPNode) 55 { 56 if (PPNode->_left == parent) 57 PPNode->_left = subR; 58 else 59 PPNode->_right = subR; 60 61 // subR的父節點改變 62 subR->_parent = PPNode; 63 } 64 else 65 { 66 _root = subR; // 根節點 67 subR->_parent = NULL; 68 } 69 parent->_parent = subR; 70 /* 修改bf */ 71 parent->_bf = subR->_bf = 0 ; 72 } 73 74 // 雙旋(左右、、右左) 75 void RotateRL(Node* parent) 76 { 77 Node* subR = parent->_right; 78 Node* subRL = subR->_left; 79 int bf = subRL->_bf; 80 81 RotateR(parent->_right); 82 RotateL(parent); 83 84 // 調整subR和parent的平衡因子 85 if (bf == -1 ) 86 subR->_bf = 1 ; // subR的bf在左旋中已經置0了,這裏就沒有再寫 87 else if (bf == 1 ) 88 parent->_bf = -1 ; 89 90 else 91 { 92 parent->_bf = 0 ; 93 subRL->_bf = 0 ; 94 } 95 } 96 97 void RotateLR(Node* parent) 98 { 99 Node* subL = parent->_left; 100 Node* subLR = subL->_right; 101 int bf = subLR->_bf; 102 RotateL(parent->_left); 103 RotateR(parent); 104 105 // 調整parent和subL的平衡因子 106 if (bf == -1 ) 107 parent->_bf = 1 ; // subL的bf在左旋中已經置0了,這裏就沒有再寫 108 else if (bf == 1 ) 109 subL->_bf = -1 ; // parent的bf在左旋中已經置0了 110 else 111 { 112 subL->_bf = 0 ; 113 parent = 0 ; 114 } 115 }