AVL树实现(含删除)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #ifndef AVLTree_H #define AVLTree_H template<typename T> class AVLTree { AVLTree(); AVLTree( const AVLTree & rhs); ~AVLTree(); const T & findMin() const ; const T & findMax() const ; bool contains( const T & x) const ; bool isEmpty() const ; void printTree() const ; void makeEmpty(); void insert( const T & x); void remove( const T & x); const AVLTree & operator =( const AVLTree & rhs); private : struct AVLNode { T element; int height; AVLNode * left; AVLNode * right; AVLNode( const T & theElement,AVLNode *lt = NULL,AVLNode *rt = NULL, int h = 0):element(theElement),left(lt),right(rt),height(h) {} }; AVLTree * root; int max( int a, int b) { return a > b ? a:b; } void insert( const T & x,AVLTree * &t) const { if (t == NULL) t = new AVLNode(x); else if (x < t->element) { insert(x,t->left); if (height(t->left) - height(t->right) == 2) { if (x < t->element->left) { rotateWithLeftChild(t); } else { doubleWithLefrChild(t); } } } else if (x > t->element) { insert(x,t->right); if (height(t->right) - height(t->left) == 2) { if (x > t->right->element) rotateWithRightChild(t); else doubleWithRightChild(t); } } else t -> height = max(height(t->left),height(t->right) ) +1; } void remove( const T & x,AVLNode * &t) const { if (t == NULL) return ; if (x == t->element) { if (t->right == NULL) //如果t的右儿子为空则直接删除 { AVLNode* temp = t; t = t->left; delete temp; } else //否则,找t->right的最左儿子来替代(即t的直接后继) { AVLNode* temp = t->right; t->element = findMin(temp)->element; t->right = remove(t->element,t->right); t->height = max(height(t->left),height(t->right)) + 1; } return t; } else if (x < t->element) t->left = remove(x,t->left); else t->right = remove(x,t->right); t->height = max(height(t->left),height(t->right)) + 1; if (t->left != NULL) t->left = rotate(t->left); if (t->right != NULL) t->right = rotate(t->right); t = rotate(t); return t; } AVLNode * findMin(AVLNode * t) const { if (t == NULL) return NULL; else { while (t->left != NULL) t = t->left; return t; } } AVLNode * findMax(AVLNode * t) const { if (t == NULL) return NULL; else { while (t->right != NULL) t = t->right; return t; } } bool contains( const T & x,AVLNode *t) const { if (t == NULL) return false ; else if (x < t->element) return contains(x,t->left); else if (c > t->element) return contains(x,t->right); else return true ; } void makeEmpty(AVLNode *t) const { if (t == NULL) ; else { makeEmpty(t->left); makeEmpty(t->right); delete t; t = NULL; } } AVLNode * clone(AVLNode * t) const { if (t == NULL) return NULL; else return new AVLNode(t->element,clone(t->left),clone(t->right)); } void printTree(AVLNode * &t) const { //这里采用中缀遍历 cout << t->element << endl; printTree(t->left); printTree(t->right); } int height(AVLNode * t) const { return t == NULL ? -1 : t->height; } AVLNode* rotateWithLeftChild(AVLNode * & k2) { AVLNode * k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = max(height(k2->left),height(k2->right)) +1; k1->height = max(height(k2->left),height(k2->right)) +1; k2 = k1; return k1; } AVLNode* doubleWithLeftChild(AVLNode * & k3) { rotateWithLeftChild(k3->left); return rotateWithLeftChild(k3); } AVLNode* rotateWithRightChild(AVLNode * & k2) { AVLNode * k1 = k2->right; k2->right = k1->left; k1->left = k2; k2->height = max(height(k2->right),height(k2->left)) +1; k1->height = max(height(k2->right),height(k2->left)) +1; k2 = k1; return k1; } AVLNode* doubleWithRightChild(AVLNode * & k3) { rotateWithRightChild(k3->right); return rotateWithRightChild(k3); } //对单个结点进行AVL调整 AVLNode* rotate(AVLNode * t) { if (height(t->left) - height(t->right) == 2) { if (height(t->left->left) >= height(t->left->right)) t = rotateWithLeftChild(t); else t = doubleWithLeftChild(t); } else if (height(t->right) - height(t->left) == 2) { if (height(t->right->right) >= height(t->right->left)) t = rotateWithRightChild(t); else t = doubleWithRightChild(t); } return t; } }; #endif |
#include "AVLTree.h" #include <iostream> using namespace std; template<typename T> AVLTree<T>::AVLTree() { root = NULL; } template<typename T> AVLTree<T>::AVLTree( const AVLTree & rhs) { root = NULL; * this = rhs; } template<typename T> AVLTree<T>::~AVLTree() { makeEmpty(); } template<typename T> const T & AVLTree<T>::findMin() const { if (!isEmpty()) return findMin(root)->element; } template<typename T> const T & AVLTree<T>::findMax() const { if (!isEmpty()) return findMax(root)->element; } template<typename T> bool AVLTree<T>::contains( const T & x) const { return contains(x,root); } template<typename T> bool AVLTree<T>::isEmpty() const { return root == NULL; } template<typename T> void AVLTree<T>::printTree() const { if (isEmpty()) cout << "Empty tree." << endl; else printTree(root); } template<typename T> void AVLTree<T>::makeEmpty() { makeEmpty(root); } template<typename T> void AVLTree<T>::insert( const T & x) { insert(x,root); } template<typename T> void AVLTree<T>::remove( const T & x) { remove(x,root); } /* Deep Copy*/ template<typename T> const AVLTree<T> &AVLTree<T>:: operator =( const AVLTree<T> & rhs) { if ( this != &rhs ) { makeEmpty(); root = clone(rhs.root); } return * this ; } |