-
-
-
-
22 explicit avl_tree(std::vector<T> __elements = {})
noexcept : root(nullptr) {
-
23 if (!__elements.empty()) {
-
24 for (T &x : __elements) {
-
-
-
-
-
-
-
-
-
-
36 root = __insert(root, key);
-
-
-
-
-
-
-
55 bool search(T key) {
return __search(root, key); }
-
-
-
-
-
-
65 std::vector<T> ino = this->
inorder();
-
-
-
-
-
-
-
75 std::vector<T> ino = this->
inorder();
-
-
-
-
-
84 size_t size() {
return __size; }
-
-
-
90 root = __remove(root, key);
-
-
-
-
-
-
-
-
-
101 [&](std::shared_ptr<node> callbacked) {
-
102 path.push_back(callbacked->info);
-
-
-
-
-
-
-
-
-
-
114 [&](std::shared_ptr<node> callbacked) {
-
115 path.push_back(callbacked->info);
-
-
-
-
-
-
-
-
-
-
127 [&](std::shared_ptr<node> callbacked) {
-
128 path.push_back(callbacked->info);
-
-
-
-
-
-
-
-
-
139 std::string __generated = generate_visualization();
-
140 visualization::visualize(__generated);
-
-
-
-
-
151 typedef struct node {
-
-
-
154 std::shared_ptr<node> left;
-
155 std::shared_ptr<node> right;
-
156 node(T key) : info(key), left(nullptr), right(nullptr), height(0) {}
-
-
-
159 std::shared_ptr<node> root;
-
-
-
162 int64_t height(std::shared_ptr<node> root) {
-
-
-
165 return 1 + std::max(height(root->left), height(root->right));
-
-
-
168 std::shared_ptr<node> createNode(T info) {
-
169 std::shared_ptr<node> nn = std::make_shared<node>(info);
-
-
-
-
173 int64_t getBalance(std::shared_ptr<node> root) {
-
174 return height(root->left) - height(root->right);
-
-
-
177 std::shared_ptr<node> rightRotate(std::shared_ptr<node> root) {
-
178 std::shared_ptr<node> t = root->left;
-
179 std::shared_ptr<node> u = t->right;
-
-
-
-
-
-
185 std::shared_ptr<node> leftRotate(std::shared_ptr<node> root) {
-
186 std::shared_ptr<node> t = root->right;
-
187 std::shared_ptr<node> u = t->left;
-
-
-
-
-
-
193 std::shared_ptr<node> minValue(std::shared_ptr<node> root) {
-
194 if (root->left ==
nullptr)
-
-
196 return minValue(root->left);
-
-
-
199 std::shared_ptr<node> __insert(std::shared_ptr<node> root, T item) {
-
200 std::shared_ptr<node> nn = createNode(item);
-
-
-
203 if (item < root->info)
-
204 root->left = __insert(root->left, item);
-
-
206 root->right = __insert(root->right, item);
-
207 int b = getBalance(root);
-
-
209 if (getBalance(root->left) < 0)
-
210 root->left = leftRotate(root->left);
-
211 return rightRotate(root);
-
-
213 if (getBalance(root->right) > 0)
-
214 root->right = rightRotate(root->right);
-
215 return leftRotate(root);
-
-
-
-
-
220 std::shared_ptr<node> __remove(std::shared_ptr<node> root, T key) {
-
-
-
223 if (key < root->info)
-
224 root->left = __remove(root->left, key);
-
225 else if (key > root->info)
-
226 root->right = __remove(root->right, key);
-
-
-
-
230 std::shared_ptr<node> temp = root->left;
-
-
-
233 }
else if (!root->left) {
-
234 std::shared_ptr<node> temp = root->right;
-
-
-
-
238 std::shared_ptr<node> temp = minValue(root->right);
-
239 root->info = temp->info;
-
240 root->right = __remove(root->right, temp->info);
-
-
-
-
-
245 bool __search(std::shared_ptr<node> root, T key) {
-
-
247 if (root->info < key) {
-
-
249 }
else if (root->info > key) {
-
-
-
-
-
-
-
-
-
258 void __inorder(std::function<
void(std::shared_ptr<node>)> callback,
-
259 std::shared_ptr<node> root) {
-
-
261 __inorder(callback, root->left);
-
-
263 __inorder(callback, root->right);
-
-
-
-
267 void __postorder(std::function<
void(std::shared_ptr<node>)> callback,
-
268 std::shared_ptr<node> root) {
-
-
270 __inorder(callback, root->left);
-
271 __inorder(callback, root->right);
-
-
-
-
-
276 void __preorder(std::function<
void(std::shared_ptr<node>)> callback,
-
277 std::shared_ptr<node> root) {
-
-
-
280 __inorder(callback, root->left);
-
281 __inorder(callback, root->right);
-
-
-
-
285 std::string generate_visualization() {
-
286 std::string __generate = __inorder_gen(root);
-
-
-
-
290 std::string __inorder_gen(std::shared_ptr<node> root) {
-
-
292 if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
-
-
-
-
296 __s += root->left->info;
-
-
298 __s += __inorder_gen(root->left);
-
-
-
-
-
303 __s += root->right->info;
-
-
305 __s += __inorder_gen(root->right);
-
-
-
-
309 __s += std::to_string(root->info) +
"->" +
-
310 std::to_string(root->left->info) +
"\n" +
-
311 __inorder_gen(root->left);
-
-
-
314 __s += std::to_string(root->info) +
"->" +
-
315 std::to_string(root->right->info) +
"\n" +
-
316 __inorder_gen(root->right);
-
-
-
-
-
-