-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.hpp
239 lines (201 loc) · 6.57 KB
/
avl.hpp
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
229
230
231
232
233
234
235
236
237
238
239
#ifndef AVL_HPP
#define AVL_HPP
#include <cassert>
#include <cmath>
#include "data_structures/bst.hpp"
namespace cgn {
template <typename K, typename V>
struct AvlNode {
K key;
V data;
std::shared_ptr<AvlNode> left;
std::shared_ptr<AvlNode> right;
int height;
AvlNode(const K& key, const V& data)
: key(key), data(data), left(nullptr), right(nullptr), height(0)
{}
};
template <typename K, typename V>
class AvlTree : public BinarySearchTree<K, V, AvlNode<K, V>> {
using Node = AvlNode<K, V>;
public:
virtual bool insert(const K& key, const V& data) override;
int height() const;
virtual bool remove(const K& key) override;
protected:
virtual bool insert(std::shared_ptr<Node>& node, const K& key, const V& data) override;
virtual bool remove(std::shared_ptr<Node>& node, const K& key) override;
private:
const static int ALLOWED_IMBALANCE;
void balance(std::shared_ptr<Node>& node);
int height(std::shared_ptr<Node> node) const;
void rotateRight(std::shared_ptr<Node>& node);
void rotateLeft(std::shared_ptr<Node>& node);
};
template <typename K, typename V>
const int AvlTree<K, V>::ALLOWED_IMBALANCE = 1;
/*
AVL insert
-------------------
Inserts a new node into the AVL if the key does not yet exist and rebalances
if successful.
Time complexity
---------------
O(log n), where n is the number of nodes in the tree.
Space complexity
----------------
O(log n), where n is the number of nodes in the tree.
*/
template <typename K, typename V>
bool AvlTree<K, V>::insert(const K& key, const V& data) {
return BinarySearchTree<K, V, AvlNode<K, V>>::insert(key, data);
}
/*
AVL height
-------------------
Return the height of the AVL tree. If a node is nullptr, then its height is
-1. The height of a node is the maximum of the height of its children plus
1. Return the height of the root node.
Time complexity
---------------
O(1), since the height is cached in the node and updated on balancing.
Space complexity
----------------
O(1)
*/
template <typename K, typename V>
int AvlTree<K, V>::height() const {
return height(BinarySearchTree<K, V, AvlNode<K, V>>::root);
}
/*
AVL remove/delete
-------------------
Remove a node from the AVL if it exists. Return true and rebalance if
successful. Return false otherwise.
Time complexity
---------------
O(log n), where n is the number of nodes in the tree.
Space complexity
----------------
O(log n), where n is the number of nodes in the tree.
*/
template <typename K, typename V>
bool AvlTree<K, V>::remove(const K& key) {
return BinarySearchTree<K, V, AvlNode<K, V>>::remove(key);
}
// ========== Recursive helper functions ==============
template <typename K, typename V>
bool AvlTree<K, V>::insert(std::shared_ptr<Node>& node, const K& key, const V& data) {
auto result = BinarySearchTree<K, V, AvlNode<K, V>>::insert(node, key, data);
if(result) {
balance(node);
}
return result;
}
template <typename K, typename V>
bool AvlTree<K, V>::remove(std::shared_ptr<Node>& node, const K& key) {
auto result = BinarySearchTree<K, V, AvlNode<K, V>>::remove(node, key);
if(result) {
balance(node);
}
return result;
}
// ========== End recursive helper functions ==============
/*
AVL balance
-------------------
Rebalances the AVL tree at the given node so that the height of the left and
right subtrees differ by no more than ALLOWED_IMBALANCE
Time complexity
---------------
O(1)
Space complexity
----------------
O(1)
*/
template <typename K, typename V>
void AvlTree<K, V>::balance(std::shared_ptr<Node>& node) {
if(!node)
return;
if(height(node->left) - height(node->right) > ALLOWED_IMBALANCE) {
if(height(node->left->left) >= height(node->left->right)) {
rotateRight(node);
} else {
rotateLeft(node->left);
rotateRight(node);
}
} else if(height(node->right) - height(node->left) > ALLOWED_IMBALANCE) {
if(height(node->right->right) >= height(node->right->left)) {
rotateLeft(node);
} else {
rotateRight(node->right);
rotateLeft(node);
}
}
node->height = std::max(height(node->left), height(node->right)) + 1;
}
/*
AVL height
-------------------
Return the height of the AVL node. If a node is nullptr, then its height is
-1. The height of a node is the maximum of the height of its children plus
1.
Time complexity
---------------
O(1)
Space complexity
----------------
O(1)
*/
template <typename K, typename V>
int AvlTree<K, V>::height(std::shared_ptr<Node> node) const {
return node ? node->height : -1;
}
/*
AVL rotate right
-------------------
Rotates a node right. The left child becomes the root and the node becomes
the right child of the root.
Time complexity
---------------
O(1)
Space complexity
----------------
O(1)
*/
template <typename K, typename V>
void AvlTree<K, V>::rotateRight(std::shared_ptr<Node>& node) {
assert(node);
auto child = node->left;
assert(child);
node->left = child->right;
child->right = node;
node->height = std::max(height(node->left), height(node->right)) + 1;
child->right->height = std::max(height(child->right->left), height(child->right->right)) + 1;
node = child;
}
/*
AVL rotate left
-------------------
Rotates a node left. The right child becomes the root and the node becomes
the left child of the root.
Time complexity
---------------
O(1)
Space complexity
----------------
O(1)
*/
template <typename K, typename V>
void AvlTree<K, V>::rotateLeft(std::shared_ptr<Node>& node) {
assert(node);
auto child = node->right;
assert(child);
node->right = child->left;
child->left = node;
node->height = std::max(height(node->left), height(node->right)) + 1;
child->left->height = std::max(height(child->left->left), height(child->left->right)) + 1;
node = child;
}
}
#endif