-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
56 lines (53 loc) · 2.26 KB
/
tree.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
#include <iostream>
using namespace std;
template <class T>
class Node {
public:
Node *right;
Node *left;
T data;
};
template <class T>
class Tree {
private:
Node<T> *root = nullptr;
void insert(Node<T> *¤t, Node<T> *&temp) {
if (current == nullptr)
current = temp;
else if (current->data > temp->data)
insert(current->left, temp);
else
insert(current->right, temp);
}
public:
void insertNode(T newData) {
Node<T> *temp = new Node<T>;
temp->data = newData;
temp->right = temp->left = nullptr;
insert(root, temp);
}
bool solvable(T newData) {
Node<T> *current = root;
while(current) {
if (current == nullptr) return false;
if (current->data == newData) return true;
if (current->data > newData) current = current->left;
else current = current->right;
}
}
void solution(T newData) {
Node<T> *current = root;
while(current) {
if (current->data == newData) {
cout << "Found " << newData << ". Retrace your steps to the entrance." << e$
break;
} else if (current->data > newData) {
cout << "Take the left door at the " << current->data << "." << endl;
current = current->left;
} else {
cout << "Take the right door at the " << current->data << "." << endl;
current = current->right;
}
}
}
};