-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAVLTree.cpp
More file actions
165 lines (133 loc) · 3.21 KB
/
Copy pathAVLTree.cpp
File metadata and controls
165 lines (133 loc) · 3.21 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
#include "AVLTree.h"
AVLTree::AVLTree()
{
root = nullptr;
}
AVLTree::AVLTree(Node *node)
{
this->root = node;
}
int AVLTree::height(Node *node)
{
if(node == nullptr)
return -1;
return node->height;
}
int AVLTree::getBalance(Node *node)
{
if(node == nullptr)
return 0;
return height(node->left) - height(node->right);
}
Node *AVLTree::rightRotate(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = std::max(height(y->left), height(y->right)) + 1;
x->height = std::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 = std::max(height(x->left), height(x->right)) + 1;
y->height = std::max(height(y->left), height(y->right)) + 1;
return y;
}
int AVLTree::compareUsername(User *u1, User *u2)
{
return u1->getUsername().compare(u2->getUsername());
}
Node *AVLTree::insert(Node *node, User *user)
{
if (node == nullptr)
{
return new Node(user);
}
int compare = compareUsername(user, node->user);
if (compare < 0)
{
node->left = insert(node->left, user);
}
else if (compare > 0)
{
node->right = insert(node->right, user);
}
else
{
std::cout << "Duplicate user detected: " << user->getUsername() << std::endl;
return node;
}
node->height = 1 + std::max(height(node->left), height(node->right));
int balance = getBalance(node);
if (balance > 1 && compareUsername(user, node->left->user) < 0)
{
return rightRotate(node);
}
if (balance < -1 && compareUsername(user, node->right->user) > 0)
{
return leftRotate(node);
}
if (balance > 1 && compareUsername(user, node->left->user) > 0)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && compareUsername(user, node->right->user) < 0)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void AVLTree::insert(User *user)
{
if (user == nullptr) {
std::cerr << "Error: Attempting to insert a nullptr into AVLTree" << std::endl;
return;
}
root = insert(root, user);
}
Node *AVLTree::search(Node *node, const String &username)
{
if(node == nullptr || node->user->getUsername() == username)
return node;
if(username < node->user->getUsername())
return search(node->left, username);
return search(node->right, username);
}
bool AVLTree::searchUser(const String &username)
{
return search(root, username) != nullptr;
}
bool AVLTree::userLogin(const String &username, const String &password)
{
Node *node = search(root, username);
if(node == nullptr)
{
return false;
}
else if(node->user->getPassword() == password)
{
return true;
}
return false;
}
Node *AVLTree::getRoot()
{
return root;
}
void AVLTree::inOrder(Node *node)
{
if(node != nullptr)
{
inOrder(node->left);
std::cout << node->user->getUsername() << std::endl;
inOrder(node->right);
}
}