-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLNode.cpp
94 lines (81 loc) · 1.82 KB
/
AVLNode.cpp
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
#include "AVLTree.h"
template <class Key,class Comp>
AVLNode<Key,Comp>::AVLNode()
{
height=1;
size=1;
left=right=parent=NULL;
key=Key();
}
template <class Key,class Comp>
AVLNode<Key,Comp>::AVLNode(Key k)
{
height=1;
size=1;
left=right=parent=NULL;
key=k;
}
template<class Key,class Comp>
int AVLNode<Key,Comp>::getHeight(AVLNode *node)
{
if(node){
return node->height;
}
return 0;
}
template<class Key,class Comp>
int AVLNode<Key,Comp>::getBalance(AVLNode *node)
{
if(node){
return AVLNode::getHeight(node->left)-AVLNode::getHeight(node->right);
}
return 0;
}
template<class Key,class Comp>
int AVLNode<Key,Comp>::getSize(AVLNode *node)
{
if(node){
return node->size;
}
return 0;
}
template<class Key,class Comp>
void AVLNode<Key,Comp>::updateSize()
{
size=AVLNode::getSize(left)+AVLNode::getSize(right)+1;
}
template<class Key,class Comp>
void AVLNode<Key,Comp>::updateHeight()
{
height=max(AVLNode::getHeight(left),AVLNode::getHeight(right))+1;
}
template<class Key,class Comp>
AVLNode<Key,Comp>* AVLNode<Key,Comp>::rightRotate(AVLNode<Key,Comp> *x)
{
AVLNode<Key,Comp> *p=x->parent,*y=x->left,*B=y->right;
y->parent=B;
y->right=x;
x->parent=y;
x->left=B;
if(B)B->parent=x;
x->updateHeight();
y->updateHeight();
x->updateSize();
y->updateSize();
return y;
}
template<class Key,class Comp>
AVLNode<Key,Comp>* AVLNode<Key,Comp>::leftRotate(AVLNode<Key,Comp> *x)
{
AVLNode<Key,Comp> *p=x->parent,*y=x->right,*B=y->left;
y->parent=B;
y->left=x;
x->parent=y;
x->right=B;
if(B)B->parent=x;
x->updateHeight();
y->updateHeight();
x->updateSize();
y->updateSize();
return y;
}