-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree-implementation.cpp
175 lines (152 loc) · 3.95 KB
/
binary-tree-implementation.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
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
#include <iostream>
#include <algorithm>
#include <exception>
#include <string>
using dType = int;
template <typename T>
struct Node
{
T val;
Node<T>* rightNode;
Node<T>* leftNode;
};
template <class T>
class BinaryTree
{
private:
Node<T>* root = nullptr;
static int getHeight(Node<T>* root)
{
if (!root)
return 0;
return 1 + std::max(getHeight(root->leftNode), getHeight(root->rightNode));
}
static const std::string print(Node<T>* node)
{
if (!node)
return "Empty";
return "" + std::to_string(node->val) + " (" + print(node->leftNode) + ") (" + print(node->rightNode) + ")";
}
public:
BinaryTree() {}
void insert(T val)
{
Node<T>* newNode { new Node<T>[sizeof(Node<T>)] };
newNode->val = val;
newNode->rightNode = nullptr;
newNode->leftNode = nullptr;
if (!root)
{
root = newNode;
return;
}
Node<T>* currNode = root;
while (currNode)
{
T oldVal = currNode->val;
if (val > oldVal)
{
if (currNode->rightNode)
{
currNode = currNode->rightNode;
continue;
}
currNode->rightNode = newNode;
break;
}
else if (val < oldVal)
{
if (currNode->leftNode)
{
currNode = currNode->leftNode;
continue;
}
currNode->leftNode = newNode;
break;
}
else
throw std::runtime_error("Duplicate value.");
}
}
int height()
{
return getHeight(root);
}
friend std::ostream& operator<<(std::ostream& os, const BinaryTree<T>& bt)
{
os << print(bt.root) << "\n";
return os;
}
bool search(T val)
{
Node<T>* currNode = root;
while (currNode)
{
if (currNode->val == val)
return true;
else if (currNode->val < val)
currNode = currNode->rightNode;
else
currNode = currNode->leftNode;
}
return false;
}
};
template <typename T>
void insert_element(BinaryTree<T>& root)
{
std::cout << "Enter value to insert: ";
T value {};
std::cin >> value;
root.insert(value);
std::cout << "Successfully inserted " << value << "!\n\n";
}
template <typename T>
void search_element(BinaryTree<T>& root)
{
std::cout << "Enter value to insert: ";
T value {};
std::cin >> value;
if (root.search(value))
std::cout << "Value exists in tree.\n\n";
else
std::cout << "Value does not exist in tree.\n\n";
}
int main()
{
BinaryTree<dType> root{};
do
{
try
{
std::cout << "What would you like to do?\n";
std::cout << "1. Insert an element\n";
std::cout << "2. Get height of tree\n";
std::cout << "3. Print the tree\n";
std::cout << "4. Search for an element in the tree\n";
std::cout << "5. Quit\n";
std::cout << "Enter your choice.\n";
int ch {};
std::cin >> ch;
switch (ch)
{
case 1: insert_element(root);
break;
case 2: std::cout << "Height is " << root.height() << ".\n\n";
break;
case 3: std::cout << "Tree is:\n" << root << "\n";
break;
case 4: search_element(root);
break;
case 5: return 0;
default: std::cout << "Invalid option. Try again.\n\n";
}
}
catch (std::runtime_error e)
{
std::cerr << e.what() << "\n\n";
}
}
while(true);
return 0;
}