-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.h
228 lines (198 loc) · 7.69 KB
/
avl.h
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
222
223
224
225
226
227
228
// AVL Header
// Shivam Sharma KNIT Sultanpur
#ifndef AVL_H_
#define AVL_H_
#include <iostream>
#include <cmath>
#include "bst.h"
template<class Comparable>
class AVL : public BST<Comparable> {
protected :
virtual void update_height_up(typename BST<Comparable>::Node*& node) ;
typename BST<Comparable>::Node* unhook_leaf(typename BST<Comparable>::Node* target) ;
typename BST<Comparable>::Node* left_left(typename BST<Comparable>::Node*& target) ;
typename BST<Comparable>::Node* left_right(typename BST<Comparable>::Node*& target) ;
typename BST<Comparable>::Node* right_right(typename BST<Comparable>::Node*& target) ;
typename BST<Comparable>::Node* right_left(typename BST<Comparable>::Node*& target) ;
void fix_avl(typename BST<Comparable>::Node*& target) ;
public :
AVL() : BST<Comparable>() { }
virtual void insert(const Comparable& k) ;
virtual void remove(const Comparable& k) ;
} ;
template<class Comparable>
typename BST<Comparable>::Node* AVL<Comparable>::unhook_leaf(typename BST<Comparable>::Node* target) {
typename BST<Comparable>::Node* p = target->parent ;
int lh = - 1, rh = -1 ;
if (p->left and p->left->key == target->key) {
lh = target->h ;
if (p->right) rh = p->right->h ;
if (lh > rh) --p->h ;
p->left = nullptr ;
}
else if (p->right and p->right->key == target->key) {
rh = target->h ;
if (p->left) lh = p->left->h ;
if (rh > lh) --p->h ;
p->right = nullptr ;
}
update_height_up(target) ;
target->set(target->key) ;
return target ;
}
template<class Comparable>
typename BST<Comparable>::Node* AVL<Comparable>::left_left(typename BST<Comparable>::Node*& target) {
typename BST<Comparable>::Node* p = target->parent ;
typename BST<Comparable>::Node* child = target->left ;
typename BST<Comparable>::Node* a = target->right ;
typename BST<Comparable>::Node* b = child->right ;
typename BST<Comparable>::Node* z = child->left ;
if (z) z->set(z->key, nullptr, z->left, z->right) ;
if (b) b->set(b->key, nullptr, b->left, b->right) ;
if (a) a->set(a->key, nullptr, a->left, a->right) ;
swap(target->key, child->key) ;
target->set(target->key, p, z, child) ;
child->set(child->key, target, b, a) ;
if (z) z->set(z->key, target, z->left, z->right) ;
if (b) b->set(b->key, child, b->left, b->right) ;
if (a) a->set(a->key, child, a->left, a->right) ;
swap(target, child) ;
return target ;
}
template<class Comparable>
typename BST<Comparable>::Node* AVL<Comparable>::right_right(typename BST<Comparable>::Node*& target) {
typename BST<Comparable>::Node* p = target->parent ;
typename BST<Comparable>::Node* child = target->right ;
typename BST<Comparable>::Node* a = target->left ;
typename BST<Comparable>::Node* b = child->left ;
typename BST<Comparable>::Node* z = child->right ;
if (z) z->set(z->key, nullptr, z->left, z->right) ;
if (b) b->set(b->key, nullptr, b->left, b->right) ;
if (a) a->set(a->key, nullptr, a->left, a->right) ;
swap(target->key, child->key) ;
target->set(target->key, p, child, z) ;
child->set(child->key, target, a, b) ;
if (z) z->set(z->key, target, z->left, z->right) ;
if (b) b->set(b->key, child, b->left, b->right) ;
if (a) a->set(a->key, child, a->left, a->right) ;
swap(target, child) ;
return target ;
}
template<class Comparable>
typename BST<Comparable>::Node* AVL<Comparable>::left_right(typename BST<Comparable>::Node*& target) {
typename BST<Comparable>::Node* ll_child = right_right(target->left) ;
target->left = ll_child->parent ;
return left_left(target) ;
}
template<class Comparable>
typename BST<Comparable>::Node* AVL<Comparable>::right_left(typename BST<Comparable>::Node*& target) {
typename BST<Comparable>::Node* rr_child = left_left(target->right) ;
target->right = rr_child->parent ;
return right_right(target) ;
}
template<class Comparable>
void AVL<Comparable>::fix_avl(typename BST<Comparable>::Node*& target) {
int lh = -1, rh = -1, ll_h = -1, lr_h = -1, rl_h = -1, rr_h = -1 ;
if (target->left) lh = target->left->h ;
if (target->right) rh = target->right->h ;
if (lh > rh) {
// first left
typename BST<Comparable>::Node* l_child = target->left ;
if (l_child->left) ll_h = l_child->left->h ;
if (l_child->right) lr_h = l_child->right->h ;
if (ll_h >= lr_h) target = left_left(target) ;
else if (lr_h > ll_h) target = left_right(target) ;
}
else {
// first right
typename BST<Comparable>::Node* r_child = target->right ;
if (r_child->left) rl_h = r_child->left->h ;
if (r_child->right) rr_h = r_child->right->h ;
if (rr_h >= rl_h) target = right_right(target) ;
else if (rl_h > rr_h) target = right_left(target) ;
}
}
template<class Comparable>
void AVL<Comparable>::update_height_up(typename BST<Comparable>::Node*& node) {
typename BST<Comparable>::Node* target = node ;
while (target != BST<Comparable>::root) {
target = target->parent ;
int lh = -1, rh = -1 ;
if (target->left) lh = target->left->h ;
if (target->right) rh = target->right->h ;
if (abs(lh - rh) > 1) {
// AVL Violation
fix_avl(target) ;
lh = rh = -1 ;
if (target->left) lh = target->left->h ;
if (target->right) rh = target->right->h ;
}
target->h = 1 + max(lh, rh) ;
}
BST<Comparable>::height = BST<Comparable>::root->h ;
}
template<class Comparable>
void AVL<Comparable>::insert(const Comparable& k) {
typename BST<Comparable>::Node* newNode = new typename BST<Comparable>::Node(k) ;
if (!BST<Comparable>::root) BST<Comparable>::root = newNode ;
else {
typename BST<Comparable>::Node* candidate = BST<Comparable>::find(BST<Comparable>::root, k) ;
newNode->parent = candidate ;
if (k < candidate->key) candidate->left = newNode ;
else if (k > candidate->key) candidate->right = newNode ;
update_height_up(newNode) ;
}
BST<Comparable>::size++ ;
}
template<class Comparable>
void AVL<Comparable>::remove(const Comparable& k) {
typename BST<Comparable>::Node* target = BST<Comparable>::find(BST<Comparable>::root, k) ;
if (target->key != k) return ; // key not present
if (BST<Comparable>::size == 1) {
// If there was just one node
BST<Comparable>::root = nullptr ;
BST<Comparable>::size-- ;
delete target ;
return ;
}
if (target->h == 0) target = unhook_leaf(target) ; // target a leaf node
else {
// target not a leaf node
typename BST<Comparable>::Node* predecessor = nullptr, *successor = nullptr ;
if (target->left) predecessor = BST<Comparable>::inorder_predecessor(target) ;
if (target->right) successor = BST<Comparable>::inorder_successor(target) ;
if (predecessor and predecessor->h == 0) { // predecessor is a leaf
BST<Comparable>::swap_nodes(target, predecessor) ;
target = unhook_leaf(target) ;
}
else if (successor and successor->h == 0) { // successor is a leaf
BST<Comparable>::swap_nodes(target, successor) ;
target = unhook_leaf(target) ;
}
else {
// Neither predecessor nor successor a leaf
typename BST<Comparable>::Node* p = nullptr, *child = nullptr ;
if (predecessor) {
BST<Comparable>::swap_nodes(target, predecessor) ;
p = target->parent ;
child = target->left ;
child->set(child->key, p, child->left, child->right) ;
if (p->left and p->left->key == target->key) p->set(p->key, p->parent, child, p->right) ;
if (p->right and p->right->key == target->key) p->set(p->key, p->parent, p->left, child) ;
}
else if (successor) {
BST<Comparable>::swap_nodes(target, successor) ;
p = target->parent ;
child = target->right ;
child->set(child->key, p, child->left, child->right) ;
if (p->left and p->left->key == target->key) p->set(p->key, p->parent, child, p->right) ;
if (p->right and p->right->key == target->key) p->set(p->key, p->parent, p->left, child) ;
}
target->set(target->key) ;
update_height_up(child) ;
}
}
delete target ;
BST<Comparable>::size-- ;
}
#endif // end of avl