-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserAVL.cpp
More file actions
112 lines (78 loc) · 2.59 KB
/
Copy pathUserAVL.cpp
File metadata and controls
112 lines (78 loc) · 2.59 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
#include "UserAVL.h"
#include <string>
#include <iostream>
using namespace std;
User::User(int id, string name, string email, string creationDate): id(id), name(name), email(email), creationDate(creationDate) {}
Node::Node(User *user): user(user), left(nullptr), right(nullptr), height(1) {}
AVLTree::AVLTree() : root(nullptr) {}
int AVLTree::height(Node *node) {
return node ? node->height : 0;
};
int AVLTree::getBalance(Node *node) {
return node ? height(node->left) - height(node->right) : 0;
}
Node *AVLTree::rightRotate(Node *y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) +1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
Node *AVLTree::leftRotate(Node *x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) +1;
y->height = max(height(y->left), height(y->right)) +1;
return y;
}
Node *AVLTree::insert(Node *node, User *user) {
if(!node) return new Node(user);
if(user->id < node->user->id) {
node -> left = insert(node->left, user);
} else if(user->id > node->user->id) {
node->right = insert(node->right, user);
} else {
return node;
}
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);
if(balance > 1 && user->id < node->left->user->id) {
return rightRotate(node);
}
if(balance < -1 && user->id > node->right->user->id) {
return leftRotate(node);
}
if(balance > 1 && user->id > node->left->user->id) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && user->id < node->right->user->id) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
// Función para buscar un usuario en el árbol AVL por ID
User* AVLTree::search(Node* node, int id) {
if (!node)
return nullptr;
if (node->user->id == id)
return node->user;
if (id < node->user->id)
return search(node->left, id);
return search(node->right, id);
}
// Función para recorrer el árbol en orden y mostrar los usuarios
void AVLTree::inorder(Node* root) {
if (!root)
return;
inorder(root->left);
std::cout << "ID: " << root->user->id << ", Nombre: " << root->user->name
<< ", Email: " << root->user->email
<< ", Fecha de creación: " << root->user->creationDate << endl;
inorder(root->right);
}