-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.h
More file actions
368 lines (309 loc) · 10 KB
/
AVLTree.h
File metadata and controls
368 lines (309 loc) · 10 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef AVLTREE_H
#define AVLTREE_H
#include <iostream>
#include <cstring>
using namespace std;
// Structure to store theme information in the AVL tree
struct ThemeInfo {
int themeID; // Unique identifier for the theme
char themeName[50]; // Name of the theme
char description[200]; // Description of the theme
int colorCode; // Color code representing the theme's appearance
// Default constructor
ThemeInfo() {
themeID = -1;
themeName[0] = '\0';
description[0] = '\0';
colorCode = 0;
}
// Parameterized constructor
ThemeInfo(int id, const char* name, const char* desc, int color) {
themeID = id;
strncpy(themeName, name, 49);
themeName[49] = '\0'; // Ensure null termination
strncpy(description, desc, 199);
description[199] = '\0'; // Ensure null termination
colorCode = color;
}
};
// AVL Tree node structure
struct AVLNode {
ThemeInfo theme; // Theme data
AVLNode* left; // Left child
AVLNode* right; // Right child
int height; // Height of the node for balancing
// Constructor
AVLNode(const ThemeInfo& themeData) {
theme = themeData;
left = right = nullptr;
height = 1; // New node is initially at height 1
}
};
class AVLTree {
private:
AVLNode* root; // Root of the AVL tree
// Get the height of a node (0 if nullptr)
int getHeight(AVLNode* node) {
if (node == nullptr) {
return 0;
}
return node->height;
}
// Calculate the balance factor of a node
int getBalanceFactor(AVLNode* node) {
if (node == nullptr) {
return 0;
}
return getHeight(node->left) - getHeight(node->right);
}
// Update the height of a node based on its children's heights
void updateHeight(AVLNode* node) {
if (node == nullptr) {
return;
}
node->height = 1 + max(getHeight(node->left), getHeight(node->right));
}
// Right rotate subtree rooted with y
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
updateHeight(y);
updateHeight(x);
// Return new root
return x;
}
// Left rotate subtree rooted with x
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
updateHeight(x);
updateHeight(y);
// Return new root
return y;
}
// Recursive function to insert a theme in the subtree rooted with node
AVLNode* insertNode(AVLNode* node, const ThemeInfo& theme) {
// Normal BST insertion
if (node == nullptr) {
return new AVLNode(theme);
}
if (theme.themeID < node->theme.themeID) {
node->left = insertNode(node->left, theme);
}
else if (theme.themeID > node->theme.themeID) {
node->right = insertNode(node->right, theme);
}
else {
// Duplicate themeID not allowed, update instead
node->theme = theme;
return node;
}
// Update height of this node
updateHeight(node);
// Get the balance factor to check if this node became unbalanced
int balance = getBalanceFactor(node);
// Left Left Case
if (balance > 1 && theme.themeID < node->left->theme.themeID) {
return rightRotate(node);
}
// Right Right Case
if (balance < -1 && theme.themeID > node->right->theme.themeID) {
return leftRotate(node);
}
// Left Right Case
if (balance > 1 && theme.themeID > node->left->theme.themeID) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
// Right Left Case
if (balance < -1 && theme.themeID < node->right->theme.themeID) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
// Return the unchanged node pointer
return node;
}
// Recursive function to search for a theme by ID
AVLNode* searchNode(AVLNode* node, int themeID) {
if (node == nullptr || node->theme.themeID == themeID) {
return node;
}
if (themeID < node->theme.themeID) {
return searchNode(node->left, themeID);
}
else {
return searchNode(node->right, themeID);
}
}
// Recursive function to search for a theme by name
AVLNode* searchNodeByName(AVLNode* node, const char* themeName) {
if (node == nullptr) {
return nullptr;
}
int cmp = strcmp(themeName, node->theme.themeName);
if (cmp == 0) {
return node;
}
else if (cmp < 0) {
return searchNodeByName(node->left, themeName);
}
else {
return searchNodeByName(node->right, themeName);
}
}
// Find the node with minimum value in a subtree
AVLNode* findMinNode(AVLNode* node) {
AVLNode* current = node;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
// Recursive function to delete a node with given themeID
AVLNode* deleteNode(AVLNode* root, int themeID) {
// Standard BST delete
if (root == nullptr) {
return root;
}
if (themeID < root->theme.themeID) {
root->left = deleteNode(root->left, themeID);
}
else if (themeID > root->theme.themeID) {
root->right = deleteNode(root->right, themeID);
}
else {
// Node to be deleted found
// Node with only one child or no child
if (root->left == nullptr) {
AVLNode* temp = root->right;
delete root;
return temp;
}
else if (root->right == nullptr) {
AVLNode* temp = root->left;
delete root;
return temp;
}
// Node with two children
AVLNode* temp = findMinNode(root->right);
root->theme = temp->theme;
root->right = deleteNode(root->right, temp->theme.themeID);
}
// If the tree had only one node
if (root == nullptr) {
return root;
}
// Update height
updateHeight(root);
// Check balance factor
int balance = getBalanceFactor(root);
// Left Left Case
if (balance > 1 && getBalanceFactor(root->left) >= 0) {
return rightRotate(root);
}
// Left Right Case
if (balance > 1 && getBalanceFactor(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right Right Case
if (balance < -1 && getBalanceFactor(root->right) <= 0) {
return leftRotate(root);
}
// Right Left Case
if (balance < -1 && getBalanceFactor(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
// Recursive function for in-order traversal
void inOrderTraversal(AVLNode* node) {
if (node == nullptr) {
return;
}
inOrderTraversal(node->left);
cout << "Theme ID: " << node->theme.themeID << ", Name: " << node->theme.themeName << endl;
inOrderTraversal(node->right);
}
// Recursively clean up nodes
void deleteTree(AVLNode* node) {
if (node == nullptr) {
return;
}
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
public:
// Constructor
AVLTree() {
root = nullptr;
}
// Destructor
~AVLTree() {
deleteTree(root);
}
// Insert a new theme
void insert(const ThemeInfo& theme) {
root = insertNode(root, theme);
}
// Search for a theme by ID
ThemeInfo search(int themeID) {
AVLNode* node = searchNode(root, themeID);
if (node != nullptr) {
return node->theme;
}
else {
return ThemeInfo(); // Return empty theme if not found
}
}
// Search for a theme by name
ThemeInfo searchByName(const char* themeName) {
AVLNode* node = searchNodeByName(root, themeName);
if (node != nullptr) {
return node->theme;
}
else {
return ThemeInfo(); // Return empty theme if not found
}
}
// Delete a theme by ID
void remove(int themeID) {
root = deleteNode(root, themeID);
}
// Display all themes in sorted order (by ID)
void displayInOrder() {
cout << "Themes in ID order:" << endl;
inOrderTraversal(root);
}
// Check if a theme exists by ID
bool exists(int themeID) {
return searchNode(root, themeID) != nullptr;
}
// Check if a theme exists by name
bool existsByName(const char* themeName) {
return searchNodeByName(root, themeName) != nullptr;
}
// Get the number of themes (count nodes)
int count() {
return countNodes(root);
}
private:
// Helper function to count nodes in the tree
int countNodes(AVLNode* node) {
if (node == nullptr) {
return 0;
}
return 1 + countNodes(node->left) + countNodes(node->right);
}
};
#endif // AVLTREE_H