-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
257 lines (213 loc) · 4.39 KB
/
Copy pathBinaryTree.cpp
File metadata and controls
257 lines (213 loc) · 4.39 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
// Define structure node
struct node
{
int value;
struct node *left;
struct node *right;
}*root;
// Define class Btree
class Btree
{
public:
void insert(node *, node *);
void preorder(node *);
void search(node *, int value);
int depth(node *);
Btree()
{
root = NULL;
}
};
int main()
{
int number, option;
Btree tree;
bool loop = true;
while(loop)
{
// Take option from user for specified Tree traversal and perform the function
cout << "Select Function of to perform on a tree" << endl;
cout << "1. Insert element to the tree " << endl;
cout << "2. Pre-order traversal of the tree "<< endl;
cout << "3. Search node in the tree " <<endl;
cout << "4. Height of the Tree " << endl;
cout << "5. Exit program " << endl;
cout << "Enter option number : ";
cin >> option;
switch(option)
{
case 1:
cout << "Enter number of elements to be inserted : ";
int numloop;
cin >> numloop;
for (int i=0; i<numloop; i++)
{
cout << "Enter value of element to be inserted : ";
int eleme;
cin >> eleme;
node* newNode = new node();
newNode->value = eleme;
tree.insert(root, newNode);
}
cout << "Elements inserted " << endl;
cout << endl;
break;
case 2:
cout << endl ;
tree.preorder(root);
cout << endl ;
cout << endl;
break;
case 3:
cout << "Enter element to be searched : ";
int elementsearch;
cin >> elementsearch;
cout << endl;
tree.search(root, elementsearch);
cout << endl;
break;
case 4:
cout << endl;
cout << "Height of tree is : " << tree.depth(root) << endl;
cout << endl;
break;
case 5:
loop = false;
break;
default:
cout << endl;
cout << "Not valid option "<< endl;
cout << endl;
}
}
return 0;
}
// function to insert the node in tree
void Btree::insert(node *tree, node *newNode)
{
// Check if tree is empty
if (root == NULL)
{
root = new node;
root->value = newNode->value;
root->left = NULL;
root->right = NULL;
return;
}
// Check if value exists in tree
if (tree->value == newNode->value)
{
cout << "Element already exist in the tree" << endl;
return;
}
// Find the position where the node is to be inserted
if (tree->value > newNode->value)
{
if (tree->left != NULL)
{
insert(tree->left, newNode);
}
else
{
tree->left = newNode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newNode);
}
else
{
tree->right = newNode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
return;
}
}
}
// Function to perform pre-order traversal
void Btree::preorder(node *treeNode)
{
//Check if tree is empty
if (root == NULL)
{
cout << "Tree is empty" << endl;
return;
}
if (treeNode != NULL)
{
cout << treeNode->value << " ";
preorder(treeNode->left);
preorder(treeNode->right);
}
}
// Function to calculate height / depth of
int Btree::depth(node* tree)
{
// Check if tree is empty
if (root == NULL)
{
cout << "Tree is empty" << endl;
return 0;
}
if (tree == NULL)
{
return 0;
}
else
{
int lTreeDepth = depth(tree->left);
int rTreeDepth = depth(tree->right);
if (lTreeDepth > rTreeDepth)
return (lTreeDepth + 1);
else
return (rTreeDepth + 1);
}
}
// Fucntion to search for element in tree
void Btree::search(node *tree, int value)
{
node *ptrTemp, *ptrPrevious;
// check if Tree is empty
if (root == NULL)
{
cout << "No nodes present in tree. Tree is empty" << endl;
return;
}
//Check if value is at root
if (value == root->value)
{
cout << "Node found" << endl;
return;
}
if (value < root->value)
ptrTemp = root->left;
else
ptrTemp = root->right;
ptrPrevious = root;
// Traverse the tree to locate node
while (ptrTemp != NULL)
{
if (value == ptrTemp->value)
{
cout << "Node found" << endl;
return;
}
ptrPrevious = ptrTemp;
if (value < ptrTemp->value)
ptrTemp = ptrTemp->left;
else
ptrTemp = ptrTemp->right;
}
// If node not in tree
cout << "Item not present in the tree" << endl;
}