-
Notifications
You must be signed in to change notification settings - Fork 0
/
Song.h
211 lines (177 loc) · 4.48 KB
/
Song.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
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
#include "Heap.h"
class TrackNode {
public:
string song_name;
string artist_name;
string genre;
string release_year;
string bpm;
string energy;
string danceability;
string loudness;
string duration;
string speechiness;
string popularity;
int height;
TrackNode* right;
TrackNode* left;
int balancevector;
TrackNode() {
height = 1;
balancevector = 0;
right = NULL;
left = NULL;
}
};
class SongAVLTree {
public:
TrackNode* root = NULL;
TrackNode* loc = NULL;
TrackNode* ploc = NULL;
bool isEmpty() {
return root == NULL;
}
int calculateHeight(TrackNode* TrackNode) {
if (TrackNode == NULL) {
return 0;
}
else {
return 1 + max(calculateHeight(TrackNode->left), calculateHeight(TrackNode->right));
}
}
int balanceVector(TrackNode* TrackNode) {
return calculateHeight(TrackNode->left) - calculateHeight(TrackNode->right);
}
TrackNode* rightRotate(TrackNode* curr) {
TrackNode* x = curr;
TrackNode* y = curr->left;
//Rotation
x->left = y->right;
y->right = x;
//update heights
x->height = calculateHeight(x);
y->height = calculateHeight(y);
x->balancevector = balanceVector(x);
y->balancevector = balanceVector(y);
return y;
}
TrackNode* leftRotate(TrackNode* curr) {
TrackNode* x = curr;
TrackNode* y = curr->right;
//Rotation
x->right = y->left;
y->left = x;
//update heights
x->height = calculateHeight(x);
y->height = calculateHeight(y);
x->balancevector = balanceVector(x);
y->balancevector = balanceVector(y);
return y;
}
TrackNode* Insert(TrackNode* curr, string song, string artist, string gen, string year, string bpm, string energy, string dance, string loud, string duration, string speech, string pop) {
//base case
if (!isEmpty()) {
if (curr == NULL) {
TrackNode* nn = new TrackNode;
nn->song_name = song;
nn->artist_name = artist;
nn->genre = gen;
nn->release_year = year;
nn->bpm = bpm;
nn->energy = energy;
nn->danceability = dance;
nn->loudness = loud;
nn->duration = duration;
nn->speechiness = speech;
nn->popularity = pop;
return nn;
}
if (song > curr->song_name) {
curr->right = Insert(curr->right, song, artist, gen, year, bpm, energy, dance, loud, duration, speech, pop);
}
else if (song < curr->song_name) {
curr->left = Insert(curr->left, song, artist,gen, year, bpm, energy, dance, loud, duration, speech, pop);
}
curr->height = calculateHeight(curr);
curr->balancevector = balanceVector(curr);
if (curr->balancevector > 1) {
//Right Rotation
if (curr->left->song_name > song) {
return rightRotate(curr);
}
//Left Right Rotation
else if (curr->left->song_name < song) {
curr->left = leftRotate(curr->left);
return rightRotate(curr);
}
}
else if (curr->balancevector < -1) {
//Left Rotation
if (curr->right->song_name < song) {
return leftRotate(curr);
}
//Right Left Rotation
else if (curr->right->song_name > song) {
curr->right = rightRotate(curr->right);
return leftRotate(curr);
}
}
return curr;
}
else {
TrackNode* nn = new TrackNode();
nn->song_name = song;
nn->artist_name = artist;
nn->genre = gen;
nn->release_year = year;
nn->bpm = bpm;
nn->energy = energy;
nn->danceability = dance;
nn->loudness = loud;
nn->duration = duration;
nn->speechiness = speech;
nn->popularity = pop;
root = nn;
return root;
}
}
TrackNode* search(TrackNode* temp, string song) {
if (temp == NULL) {
return NULL;
}
else {
if (song == temp->song_name) {
return temp;
}
else if (song > temp->song_name) {
return search(temp->right, song);
}
else {
return search(temp->left, song);
}
}
}
void inOrder(TrackNode* TrackNode) {
if (TrackNode != NULL) {
inOrder(TrackNode->left);
cout << TrackNode->song_name << endl;
inOrder(TrackNode->right);
}
}
void countArtists(TrackNode* root, ArtistNode* headnode) {
int count = 0;
if (root != NULL) {
ArtistNode* loc = headnode;
while (loc != NULL) {
if (root->artist_name == loc->artist) {
count++;
}
loc = loc->next;
}
loc = headnode;
countArtists(root->left, loc);
cout << root->artist_name << " " << count << endl;
countArtists(root->right, loc);
}
}
};