-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArtistTree.cpp
116 lines (104 loc) · 1.81 KB
/
ArtistTree.cpp
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
#include "ArtistTree.h"
TreeNode::TreeNode(std::string name)
: artist{ name } {};
ArtistTree::ArtistTree() {};
ArtistTree::ArtistTree(TreeNode* first)
: root{ first } {};
ArtistTree::ArtistTree(std::string artist)
: root{ new TreeNode(artist) } {}
void ArtistTree::insert(TreeNode* new_artist)
{
TreeNode *temp = root;
TreeNode *prev = nullptr;
while (temp)
{
prev = temp;
// do not allow for duplicate artists
if (new_artist->artist == temp->artist)
{
return;
}
else if (new_artist->artist < temp->artist)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
new_artist->par = prev;
if (!prev)
{
root = new_artist;
}
else if (new_artist->artist < prev->artist)
{
prev->left = new_artist;
}
else
{
prev->right = new_artist;
}
}
void ArtistTree::insert(std::string artist)
{
TreeNode *temp = new TreeNode(artist);
insert(temp);
}
TreeNode* ArtistTree::find(std::string name)
{
TreeNode* temp = root;
while (temp)
{
if (temp->artist == name)
{
return temp;
}
else if (name < temp->artist)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
if (!temp)
{
std::cout << "Artist " << name << " not found." << std::endl;
}
return temp; // temp is nullptr if no match found
}
void ArtistTree::print_inorder(TreeNode* start)
{
if (start)
{
print_inorder(start->left);
std::cout << start->artist << ", ";
print_inorder(start->right);
}
}
void ArtistTree::print_inorder()
{
print_inorder(root);
}
void ArtistTree::strings_inorder(TreeNode* start)
{
if (start)
{
strings_inorder(start->left);
artists.push_back(start->artist);
strings_inorder(start->right);
}
}
void ArtistTree::strings_inorder()
{
strings_inorder(root);
}
std::vector<std::string> ArtistTree::get_strings()
{
artists.clear();
strings_inorder(root);
return artists;
}