[代码] AVL
007 |
#define MAX(a,b) (a > b ? a : b) |
014 |
T value; // value contained in the node |
015 |
TreeNode<T> * parent; // Pointer to the parent |
016 |
TreeNode<T> * left; // Pointer to the left child |
017 |
TreeNode<T> * right; // Pointer to the right child |
020 |
TreeNode() // No-arg constructor |
028 |
TreeNode(T value) // Constructor |
039 |
template < typename T > |
045 |
AVLTree(T values[], int arraySize); |
048 |
void inOrderNorRec(); |
049 |
int deleteNode(T value); |
050 |
int successor(T value); |
051 |
int predecessor(T value); |
054 |
int getSize(T value); |
058 |
TreeNode<T> * treeroot; |
059 |
void LeftRotate(TreeNode<T> * target); |
060 |
void RightRotate(TreeNode<T> * target); |
061 |
void LeftRightRotate(TreeNode<T> * target); |
062 |
void RightLeftRotate(TreeNode<T> * target); |
063 |
int insert(TreeNode<T> * targetParent,TreeNode<T> * target,T value); |
064 |
void inOrder(TreeNode<T> *target); |
065 |
void inOrderNorRec(TreeNode<T> *target); |
066 |
TreeNode<T> * search(T searchvalue); |
067 |
int deleteNode(TreeNode<T> *target,T value); |
068 |
TreeNode<T> * successor(TreeNode<T> *target); |
069 |
TreeNode<T> * predecessor(TreeNode<T> *target); |
070 |
TreeNode<T> * maxValue(TreeNode<T> *target); |
071 |
TreeNode<T> * minValue(TreeNode<T> *target); |
072 |
int Height(TreeNode<T> *target); |
073 |
int getSize(TreeNode<T> *target); |
074 |
void output(TreeNode<T> *target, int totalSpaces); |
078 |
template < typename T > |
079 |
AVLTree<T>::AVLTree() |
086 |
template < typename T > |
087 |
AVLTree<T>::AVLTree(T values[], int arraySize) |
091 |
for ( int i=0 ; i<arraySize ; i++){ |
092 |
insert(treeroot,treeroot,values[i]); |
097 |
template < typename T > |
098 |
void AVLTree<T>::LeftRotate(TreeNode<T> * target) |
100 |
TreeNode<T> *rightchild = target->right; |
101 |
target->right = rightchild->left; |
102 |
if (rightchild->left != NULL){ |
103 |
rightchild->left->parent = target; |
106 |
if (target->parent == NULL){ |
107 |
treeroot = rightchild; |
108 |
rightchild->parent = NULL; |
109 |
} else if (target->parent->left == target){ |
110 |
target->parent->left = rightchild; |
111 |
rightchild->parent = target->parent; |
113 |
target->parent->right = rightchild; |
114 |
rightchild->parent = target->parent; |
117 |
rightchild->left = target; |
118 |
target->parent = rightchild; |
120 |
int leftHeight = Height(target->left); |
121 |
int RightHeight = Height(target->right); |
122 |
target->height = MAX(leftHeight,RightHeight) + 1; |
124 |
leftHeight = Height(rightchild->left); |
125 |
RightHeight = Height(rightchild->right); |
126 |
rightchild->height = MAX(leftHeight,RightHeight) + 1; |
130 |
template < typename T > |
131 |
void AVLTree<T>::RightRotate(TreeNode<T> * target) |
134 |
TreeNode<T> *leftchild = target->left; |
135 |
target->left = leftchild->right; |
136 |
if (leftchild->right != NULL){ |
137 |
leftchild->left->parent = target; |
140 |
if (target->parent == NULL){ |
141 |
treeroot = leftchild; |
142 |
leftchild->parent = NULL; |
143 |
} else if (target->parent->left == target){ |
144 |
target->parent->left = leftchild; |
145 |
leftchild->parent = target->parent; |
147 |
target->parent->right = leftchild; |
148 |
leftchild->parent = target->parent; |
151 |
leftchild->right = target; |
152 |
target->parent = leftchild; |
154 |
int leftHeight = Height(target->left); |
155 |
int RightHeight = Height(target->right); |
156 |
target->height = MAX(leftHeight,RightHeight) + 1; |
158 |
leftHeight = Height(leftchild->left); |
159 |
RightHeight = Height(leftchild->right); |
160 |
leftchild->height = MAX(leftHeight,RightHeight) + 1; |
165 |
template < typename T > |
166 |
void AVLTree<T>::LeftRightRotate(TreeNode<T> * target) |
168 |
LeftRotate(target->left); |
173 |
template < typename T > |
174 |
void AVLTree<T>::RightLeftRotate(TreeNode<T> * target) |
176 |
RightRotate(target->right); |
181 |
template < typename T> |
182 |
int AVLTree<T>::insert(T value) |
184 |
this ->insert(treeroot,treeroot,value); |
189 |
template < typename T> |
190 |
int AVLTree<T>::insert(TreeNode<T> * targetParent,TreeNode<T> * target,T value) |
193 |
target = new TreeNode<T>(value); |
194 |
if (targetParent == NULL){ |
195 |
this ->treeroot = target; |
197 |
target->parent = targetParent; |
198 |
if (targetParent->value > value){ |
199 |
targetParent->left = target; |
201 |
targetParent->right = target; |
207 |
else if (target->value > value){ |
208 |
this ->insert(target,target->left,value); |
209 |
if ( this ->Height(target->left) - this ->Height(target->right) >= 2){ |
210 |
if (target->left->value > value){ |
211 |
this ->RightRotate(target); |
214 |
this ->LeftRightRotate(target); |
218 |
else if (target->value < value){ |
219 |
this ->insert(target,target->right,value); |
220 |
if ( this ->Height(target->right) - this ->Height(target->left) >= 2){ |
221 |
if (target->right->value < value){ |
222 |
this ->LeftRotate(target); |
225 |
this ->RightLeftRotate(target); |
232 |
int leftHeight = Height(target->left); |
233 |
int RightHeight = Height(target->right); |
234 |
target->height = MAX(leftHeight,RightHeight) + 1; |
239 |
template < typename T> |
240 |
TreeNode<T> * AVLTree<T>::search(T searchvalue) |
242 |
TreeNode<T> *current = this ->treeroot; |
244 |
while (current != NULL && find == 0){ |
245 |
if (current->value == searchvalue){ |
248 |
else if (current->value > searchvalue){ |
249 |
current = current->left; |
251 |
current = current->right; |
263 |
template < typename T> |
264 |
int AVLTree<T>::deleteNode(T value){ |
265 |
TreeNode<T> *delNode = this ->search(value); |
266 |
if ( delNode == NULL){ |
267 |
cout << "not find " << endl; |
270 |
this ->deleteNode( this ->treeroot,value); |
271 |
cout << "Node " << value << " has been deleted." << endl; |
276 |
template < typename T> |
277 |
int AVLTree<T>::deleteNode(TreeNode<T> *target,T value){ |
278 |
if (target->value > value) deleteNode(target->left,value); |
279 |
else if (target->value < value) deleteNode(target->right,value); |
280 |
else if (target->left != NULL) |
282 |
target->value = this ->maxValue(target->left)->value; |
283 |
deleteNode(target->left,target->value); |
285 |
else if (target->right != NULL) |
287 |
target->value = this ->minValue(target->right)->value; |
288 |
deleteNode(target->right,target->value); |
292 |
if (target->parent->left == target){ |
293 |
target->parent->left = NULL; |
295 |
target->parent->right = NULL; |
302 |
int leftHeight = Height(target->left); |
303 |
int RightHeight = Height(target->right); |
304 |
target->height = MAX(leftHeight,RightHeight) + 1; |
305 |
if (RightHeight - leftHeight >= 2) |
307 |
if (target->right->right==NULL) |
309 |
this ->RightLeftRotate(target); |
313 |
this ->LeftRotate(target); |
316 |
else if (leftHeight - RightHeight >= 2) |
318 |
if (target->left->left == NULL) |
320 |
this ->LeftRightRotate(target); |
324 |
this ->RightRotate(target); |
332 |
template < typename T> |
333 |
int AVLTree<T>::successor(T value) |
335 |
TreeNode<T> *position = this ->search(value); |
336 |
if ( position == NULL){ |
337 |
cout << "not find " << endl; |
340 |
TreeNode<T> *successorNode = this ->successor(position); |
341 |
if ( successorNode != NULL) |
342 |
cout << value << " \'s successor is:" << successorNode->value << endl; |
344 |
cout << value << " has no successor" << endl; |
349 |
template < typename T> |
350 |
TreeNode<T> * AVLTree<T>::successor(TreeNode<T> *target) |
352 |
if ( target->right != NULL){ |
353 |
return minValue(target->right); |
355 |
TreeNode<T> * parentNode =target->parent; |
356 |
while ( parentNode != NULL && parentNode->right == target){ |
358 |
parentNode = parentNode->parent; |
364 |
template < typename T> |
365 |
int AVLTree<T>::predecessor(T value) |
367 |
TreeNode<T> *position = this ->search(value); |
368 |
if ( position == NULL){ |
369 |
cout << "Can\'t find " << value << " in AVL tree." <<endl; |
372 |
TreeNode<T> *predecessorNode = this ->predecessor(position); |
373 |
if ( predecessorNode != NULL) |
374 |
cout << value << " \'s predecessor is:" << predecessorNode->value << endl; |
376 |
cout << value << " has no predecessor" << endl; |
381 |
template < typename T> |
382 |
TreeNode<T> * AVLTree<T>::predecessor(TreeNode<T> *target) |
384 |
if ( target->left != NULL){ |
385 |
return maxValue(target->left); |
387 |
TreeNode<T> * parentNode =target->parent; |
388 |
while ( parentNode != NULL && parentNode->left == target){ |
390 |
parentNode = parentNode->parent; |
396 |
template < typename T> |
397 |
int AVLTree<T>::maxValue() |
399 |
TreeNode<T> * max = this ->maxValue(treeroot); |
404 |
template < typename T> |
405 |
TreeNode<T> * AVLTree<T>::maxValue(TreeNode<T> *target) |
407 |
while (target -> right != NULL){ |
408 |
target = target -> right; |
414 |
template < typename T> |
415 |
int AVLTree<T>::minValue() |
417 |
TreeNode<T> * min = this ->minValue(treeroot); |
422 |
template < typename T> |
423 |
TreeNode<T> * AVLTree<T>::minValue(TreeNode<T> *target) |
425 |
while (target -> left != NULL){ |
426 |
target = target -> left; |
432 |
template < typename T> |
433 |
int AVLTree<T>::getSize(T value) |
435 |
TreeNode<T> *target = this ->search(value); |
436 |
return getSize(target); |
440 |
template < typename T> |
441 |
int AVLTree<T>::getSize(TreeNode<T> *target) |
447 |
if (target->left == NULL && target->left == NULL){ |
450 |
return this ->getSize(target->left) + 1 + this ->getSize(target->right); |
455 |
template < typename T> |
456 |
void AVLTree<T>::inOrder() |
463 |
template < typename T> |
464 |
void AVLTree<T>::inOrder(TreeNode<T> *target) |
468 |
inOrder(target->left); |
469 |
cout << target->value << " " ; |
470 |
inOrder(target->right); |
474 |
template < typename T> |
475 |
void AVLTree<T>::inOrderNorRec() |
477 |
inOrderNorRec(treeroot); |
482 |
template < typename T> |
483 |
void AVLTree<T>::inOrderNorRec(TreeNode<T> *target) |
485 |
stack < TreeNode<T> *> s; |
486 |
while ((target != NULL) || !s.empty()) |
491 |
target = target->left; |
496 |
cout << target->value << " " ; |
498 |
target = target->right; |
504 |
template < typename T > |
505 |
int AVLTree<T>::Height(TreeNode<T> *target) |
507 |
return target == NULL ? -1 : target->height; |
511 |
template < typename T> |
512 |
void AVLTree<T>::output() |
518 |
template < typename T> |
519 |
void AVLTree<T>::output(TreeNode<T> *target, int totalSpaces) |
523 |
output(target->right,totalSpaces+4); |
524 |
for ( int i=0;i<totalSpaces;i++){ |
527 |
if (target->parent != NULL){ |
528 |
cout << target->value << "[" << target->parent->value << "]" << endl; |
531 |
cout << target->value << "[ROOT]" << endl; |
533 |
output(target->left,totalSpaces+4); |
|