-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.h
156 lines (132 loc) · 3.76 KB
/
AVLTree.h
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
struct AVLNode {
T data;
int height;
AVLNode<T>* left;
AVLNode<T>* right;
AVLNode(T data) : data(data), height(1), left(nullptr), right(nullptr) {}
};
template <typename T>
class AVLTree {
public:
AVLNode<T>* root=nullptr;
bool compare(T a, T b) {
return a < b;
}
void insert(T data) {
insert(root, data);
}
void inorder() {
inorder(root);
}
void rightRotate(AVLNode<T>*& node) {
AVLNode<T>* leftChild = node->left;
node->left = leftChild->right;
leftChild->right = node;
node = leftChild;
// Update heights of the rotated nodes
node->right->height = 1 + max(height(node->right->left), height(node->right->right));
node->height = 1 + max(height(node->left), height(node->right));
}
void leftRotate(AVLNode<T>*& node) {
AVLNode<T>* rightChild = node->right;
node->right = rightChild->left;
rightChild->left = node;
node = rightChild;
// Update heights of the rotated nodes
node->left->height = 1 + max(height(node->left->left), height(node->left->right));
node->height = 1 + max(height(node->left), height(node->right));
}
vector<string> search(string data) {
vector<string> words = split(data);
vector<string> foundApps;
search(root, words, foundApps);
return foundApps;
}
vector<int> TrendingApps (int threshold) {
vector<int> trend;
trendingApps(root, threshold, trend);
return trend;
}
private:
void inorder(AVLNode<T>* node) {
if (node == nullptr) return;
inorder(node->left);
cout << node->data << endl;
inorder(node->right);
}
void trendingApps(AVLNode<T>* node, int threshold, vector<int> &trending) {
if(node == nullptr) return;
trendingApps(node->left,threshold, trending);
if(node->data > threshold) {
trending.push_back(node->data);
}
trendingApps(node->right,threshold, trending);
}
int height(AVLNode<T>* node) {
if (node == nullptr) return 0;
return node->height;
}
int getBalance(AVLNode<T>* node) {
if (node == nullptr) {
return 0;
}
return height(node->left) - height(node->right);
}
void insert(AVLNode<T>*& node, T data) {
if (node == nullptr) {
node = new AVLNode<T>(data);
return;
}
if (compare(data, node->data)) {
insert(node->left, data);
} else if (!compare(data, node->data)) {
insert(node->right, data);
}
// Update height of the current node
node->height = 1 + max(height(node->left), height(node->right));
// Check for balance and perform rotations if necessary
int balance = getBalance(node);
if (balance > 1 && compare(data, node->left->data)) {
// Left-left case
rightRotate(node);
} else if (balance > 1 && !compare(data, node->left->data)) {
// Left-right case
leftRotate(node->left);
rightRotate(node);
} else if (balance < -1 && !compare(data, node->right->data)) {
// Right-right case
leftRotate(node);
} else if (balance < -1 && compare(data, node->right->data)) {
// Right-left case
rightRotate(node->right);
leftRotate(node);
}
}
vector<string> split(string data) {
vector<string> words;
istringstream iss(data);
for (string word; iss >> word; ) {
words.push_back(word);
}
return words;
}
void search(AVLNode<T>* node, vector<string> words, vector<string> &foundApps) {
if (node == nullptr) {
return;
}
for (const string& word : words) {
if (node->data.find(word) != string::npos) {
foundApps.push_back(node->data);
}
}
search(node->left, words, foundApps);
search(node->right, words, foundApps);
}
};