-
Notifications
You must be signed in to change notification settings - Fork 2
/
binaryTree.cpp
178 lines (146 loc) · 3.12 KB
/
binaryTree.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
176
177
/*
* Basic example of binary tree data structure
*
* @author J. Alvarez
*/
#include <iostream>
#include <string>
#include <math.h>
class Node {
public:
Node(std::string info): info(info) {
left = NULL;
right = NULL;
}
Node * getLeft() {
return left;
}
void setLeft(Node * left) {
this->left = left;
}
Node * getRight() {
return right;
}
void setRight(Node * right) {
this->right = right;
}
std::string getInfo() {
return info;
}
void printNode() {
std::cout << info << std::endl;
}
void printNodeComplete() {
std::string leftInfo = left ? left->getInfo() : "NULL";
std::string rightInfo = right ? right->getInfo() : "NULL";
std::cout << info << "... children:\n\t" << leftInfo << " - " <<
rightInfo << std::endl;
}
private:
Node * left;
Node * right;
std::string info;
};
class BinaryTree {
public:
BinaryTree() {
root = NULL;
}
Node * getRoot() {
return root;
}
void addNode(Node * node) {
if(!root)
root = node;
else
addNodeToSubTree(node, root);
}
void addNodeToSubTree(Node * node, Node * subTree) {
if(!subTree)
subTree = node;
else {
int comparison = node->getInfo().compare(subTree->getInfo());
if(comparison < 0) {
if(!subTree->getLeft())
subTree->setLeft(node);
else
addNodeToSubTree(node, subTree->getLeft());
}
else {
if(!subTree->getRight())
subTree->setRight(node);
else
addNodeToSubTree(node, subTree->getRight());
}
}
}
void printTree() {
printSubTree(root, 0);
}
void printSubTree(Node * subTree, int level) {
if(subTree) {
int nTabs = level == 0 ? 0 : (int)pow(2.0, level - 1);
for(int i = 0; i < nTabs ; i++)
std::cout << "\t";
subTree->printNode();
printSubTree(subTree->getLeft(), level + 1);
printSubTree(subTree->getRight(), level + 1);
}
}
void cleanTree() {
if(root)
cleanSubTree(root);
}
void cleanSubTree(Node * subTree) {
if(subTree) {
cleanSubTree(subTree->getLeft());
cleanSubTree(subTree->getRight());
delete subTree;
}
}
Node * search(std::string info) {
Node * found = NULL;
if(root) {
found = searchSubTree(info, root);
}
return found;
}
Node * searchSubTree(std::string info, Node * subTree) {
Node * found = NULL;
if(subTree) {
if(subTree->getInfo().compare(info) == 0)
found = subTree;
else {
found = searchSubTree(info, subTree->getLeft());
if(!found)
found = searchSubTree(info, subTree->getRight());
}
}
return found;
}
private:
Node * root;
};
int main(int argc, char ** args) {
BinaryTree * bt = new BinaryTree;
bt->addNode(new Node("figaro"));
bt->addNode(new Node("pepe"));
bt->addNode(new Node("coal"));
bt->addNode(new Node("boat"));
bt->addNode(new Node("hello"));
bt->addNode(new Node("aloha"));
bt->addNode(new Node("cello"));
bt->printTree();
std::string searchInfo = "pepe";
std::cout << "Searching for... " << searchInfo << std::endl;
Node * search = bt->search(searchInfo);
if(!search)
std::cout << "Node not found :(" << std::endl;
else {
std::cout << "Found it :)" << std::endl;
search->printNodeComplete();
}
bt->cleanTree();
delete bt;
return 0;
}