-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
123 lines (115 loc) · 3.26 KB
/
BST.cpp
File metadata and controls
123 lines (115 loc) · 3.26 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
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include "song.h"
#include "BST.h"
#include "Survey.h"
#include "Node.h"
#include "BST.h"
using namespace std;
//constructor
BST::BST() {
this->root = nullptr;
this->size = 0;
}
//return true if insertion successful, calls on recursive helper function
bool BST::insert(song *Song) {
//check duration is unique if not inc till is
Node* temp = searchRecursion(root, Song);
while(temp != nullptr) {
if(temp->Song.songName == (Song->songName)) {
//song is repeated in data and has already been inserted
return true;
}
else {
Song->duration = Song->duration + 1;
temp = searchRecursion(root, Song);
}
}
size++;
return insertRecusrion(root, Song);
}
// Return true if insertion successful
bool BST::insertRecusrion(Node* temp, song* Song) {
//three main insert cases
//first case check if root has yet to be made, i.e., first insertion
if(root == nullptr) {
root = new Node(*Song);
return true;
}
//use song duration to sort
else if(Song->duration > temp->Song.duration) {
if (temp->right == nullptr) {
temp->right = new Node(*Song);
return true;
}
else {
//use recursion to insert new node
return insertRecusrion(temp->right, Song);
}
}
//id number is less than node
else if(Song->duration < temp->Song.duration) {
if (temp->left == nullptr) {
temp->left = new Node(*Song);
return true;
}
else {
//use recursion to insert new node
return insertRecusrion(temp->left, Song);
}
}
else {
return false;
}
}
// method to find height of given node
int BST::height(Node* temp) {
if (temp == nullptr) {
return 0;
}
return 1 + max(height(temp->left), height(temp->right));
}
// Return vector of songs of inorder traversal (by duration)
vector<song> BST::traverseInOrder(Node* temp){
vector<song> toQL;
vector<song> toQR;
if(temp != nullptr) {
toQL = traverseInOrder(temp->left);
toQL.push_back(temp->Song);
toQR = traverseInOrder(temp->right);
toQL.insert(toQL.end(), toQR.begin(), toQR.end());
}
return toQL;
}
// Return vector of songs of postorder traversal (by duration)
vector<song> BST::traversePostOrder(Node* temp){
vector<song> toQ;
vector<song> toQL;
vector<song> toQR;
if(temp != nullptr) {
toQL = traversePostOrder(temp->left);
if(toQL.size()) {
toQ.insert(toQ.end(), toQL.begin(), toQL.end());
}
toQR = traversePostOrder(temp->right);
if(toQR.size()) {
toQ.insert(toQ.end(), toQR.begin(), toQR.end());
}
toQ.push_back(temp->Song);
}
return toQ;
}
//use recursion to search through tree to see if contains duplicate
Node* BST::searchRecursion(Node* temp, song* Song) {
if(temp == nullptr || temp->Song.duration == Song->duration) {
return temp;
}
else if(Song->duration < temp->Song.duration) {
return searchRecursion(temp->left, Song);
}
else {
return searchRecursion(temp->right, Song);
}
}