-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL.h
More file actions
177 lines (154 loc) · 4.06 KB
/
AVL.h
File metadata and controls
177 lines (154 loc) · 4.06 KB
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
#pragma once
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <memory>
template<typename T>
class AVL {
protected:
struct Node {
T data;
int height;
std::unique_ptr<Node> l, r;
Node(T data, int height, std::unique_ptr<Node> l = nullptr, std::unique_ptr<Node> r = nullptr)
: data(data), height(height), l(std::move(l)), r(std::move(r)) {}
};
std::unique_ptr<Node> root;
std::function<bool(T, T)> comp;
int height(const Node* n) {
int left = (n->l == nullptr) ? 0 : n->l->height;
int right = (n->r == nullptr) ? 0 : n->r->height;
return 1 + std::max(left, right);
}
void adjustHeight(Node* n) {
n->height = height(n);
}
int findBalance(const Node* n) {
int left = (n->l == nullptr) ? 0 : n->l->height;
int right = (n->r == nullptr) ? 0 : n->r->height;
return right - left;
}
std::unique_ptr<Node> rotateLeft(std::unique_ptr<Node> n) {
std::unique_ptr<Node> nr = std::move(n->r);
std::unique_ptr<Node> midSubtree = std::move(nr->l);
nr->l = std::move(n);
nr->l->r = std::move(midSubtree);
adjustHeight(nr->l.get());
adjustHeight(nr.get());
return nr;
}
std::unique_ptr<Node> rotateRight(std::unique_ptr<Node> n) {
std::unique_ptr<Node> nl = std::move(n->l);
std::unique_ptr<Node> midSubtree = std::move(nl->r);
nl->r = std::move(n);
nl->r->l = std::move(midSubtree);
adjustHeight(nl->r.get());
adjustHeight(nl.get());
return nl;
}
std::unique_ptr<Node> balance(std::unique_ptr<Node> n) {
int bal = findBalance(n.get());
if (bal == 2) {
if (findBalance(n->r.get()) >= 0) {
return rotateLeft(std::move(n));
} else {
n->r = rotateRight(std::move(n->r));
return rotateLeft(std::move(n));
}
} else if (bal == -2) {
if (findBalance(n->l.get()) <= 0) {
return rotateRight(std::move(n));
} else {
n->l = rotateLeft(std::move(n->l));
return rotateRight(std::move(n));
}
}
adjustHeight(n.get());
return n;
}
bool lookup(const Node* n, const T& data) {
if (n == nullptr)
return false;
if (comp(data, n->data))
return lookup(n->l.get(), data);
else if (comp(n->data, data))
return lookup(n->r.get(), data);
return true;
}
std::unique_ptr<Node> insertRec(std::unique_ptr<Node> n, const T& data) {
if (n == nullptr)
return std::make_unique<Node>(data, 1);
if (comp(data, n->data)) {
n->l = insertRec(std::move(n->l), data);
n = balance(std::move(n));
} else if (comp(n->data, data)) {
n->r = insertRec(std::move(n->r), data);
n = balance(std::move(n));
}
return n;
}
T min(const Node* n) {
if (!n->l)
return n->data;
return min(n->l.get());
}
std::unique_ptr<Node> removeMinRec(std::unique_ptr<Node> n) {
if (!n->l)
return std::move(n->r);
n->l = removeMinRec(std::move(n->l));
n = balance(std::move(n));
return n;
}
std::unique_ptr<Node> removeRoot(std::unique_ptr<Node> n) {
if (!n->r)
return std::move(n->l);
n->data = min(n->r.get());
n->r = removeMinRec(std::move(n->r));
n = balance(std::move(n));
return n;
}
std::unique_ptr<Node> removeRec(std::unique_ptr<Node> n, const T& data) {
if (!n)
return n;
if (comp(data, n->data)) {
n->l = removeRec(std::move(n->l), data);
n = balance(std::move(n));
} else if (comp(n->data, data)) {
n->r = removeRec(std::move(n->r), data);
n = balance(std::move(n));
} else {
return removeRoot(std::move(n));
}
return n;
}
int sizeRec(const Node* n) {
if (!n)
return 0;
return 1 + sizeRec(n->l.get()) + sizeRec(n->r.get());
}
void printRec(const Node* n, const std::string& space) {
if (n == nullptr)
return;
printRec(n->r.get(), space + ' ');
std::cout << space << n->data << '\n';
printRec(n->l.get(), space + ' ');
}
public:
AVL(std::function<bool(T, T)> comp = std::less<T>()) : comp(comp) {}
bool lookup(const T& data) {
return lookup(root.get(), data);
}
void insert(const T& data) {
root = insertRec(std::move(root), data);
}
void remove(const T& data) {
root = removeRec(std::move(root), data);
}
int size() {
return sizeRec(root.get());
}
void print() {
printRec(root.get(), "");
}
};