-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
228 lines (199 loc) · 5.17 KB
/
BinaryTree.cpp
File metadata and controls
228 lines (199 loc) · 5.17 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
/**
* Author: Hemant Tripathi
*/
#include <iostream>
using namespace std;
struct NodePtr {
int info;
struct NodePtr *left, *right;
};
NodePtr* insertOp(struct NodePtr* ptr, int info);
bool isLeft(struct NodePtr* ptr);
bool isRight(struct NodePtr* ptr);
void showInorder(struct NodePtr* ptr);
void showPreorder(struct NodePtr* ptr);
void showPostorder(struct NodePtr* ptr);
NodePtr* searchBST(struct NodePtr* ptr, int data);
int getBSTHeight(struct NodePtr* ptr);
int main() {
cout << "#########Program to create and traversal through binary tree##########" << endl;
bool isExit = false;
struct NodePtr* tree = NULL;
while(1) {
if(isExit) {
cout << "Terminating the program" << endl;
break;
}
cout << "Select your option..." << endl;
cout << "1. Insert an element into the tree" << endl;
cout << "2. Show all the elements of the tree" << endl;
cout << "3. Preorder Traversal to all nodes" << endl;
cout << "4. Inorder Traversal to all nodes" << endl;
cout << "5. Postorder Traversal to all nodes" << endl;
cout << "6. Search an element in BST" << endl;
cout << "7. Find Hight of BST" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice" << endl;
int choice;
cin >> choice;
if(choice < 1 || choice > 8) {
cout << "Invalid input. Please choose between 1 to 7" << endl;
continue;
}
switch(choice) {
case 1:
cout << "Enter an element to insert into the tree" << endl;
int data;
cin >> data;
tree = insertOp(tree, data);
break;
case 2:
case 4:
cout << "Printing tree nodes in Inorder form"<<endl;
if(tree != NULL) {
//By default it will show an inorder form.
if(isLeft(tree)) {
showInorder(tree->left);
}
cout << "\t" << tree->info;
if(isRight(tree)) {
showInorder(tree->right);
}
} else {
cout << "No Tree structure found!"<<endl;
}
cout << endl;
break;
case 3:
cout << "Printing tree nodes in Preorder form" <<endl;
if(tree != NULL) {
cout << "\t" << tree->info;
if(isLeft(tree)) {
showPreorder(tree->left);
}
if(isRight(tree)) {
showPreorder(tree->right);
}
} else {
cout << "No tree structure found!"<<endl;
}
cout << endl;
break;
case 5:
cout << "Printing tree nodes in Postorder form"<<endl;
if(tree != NULL) {
if(isLeft(tree)) {
showPostorder(tree->left);
}
if(isRight(tree)) {
showPostorder(tree->right);
}
cout << "\t" << tree->info;
} else {
cout << "No tree structure found!" << endl;
}
cout << endl;
break;
case 6:
cout << "Enter the element you want to search in BST"<<endl;
int element;
cin >> element;
NodePtr* searchElement;
searchElement = searchBST(tree, element);
if(searchElement != NULL) {
cout << "Search element found!"<<endl;
} else {
cout << "Element not found!"<<endl;
}
break;
case 7:
cout << "Finding height of Binary Search Tree" << endl;
int height;
height = getBSTHeight(tree);
cout << "Height of tree : "<<height<<endl;
break;
case 8:
isExit = true;
break;
}
}
}
NodePtr* insertOp(struct NodePtr* tree, int info) {
if(tree == NULL) {
cout << "Tree not found! Create a new tree" << endl;
tree = (struct NodePtr*)malloc(sizeof(struct NodePtr));
tree->info = info;
tree->left = NULL;
tree->right = NULL;
} else {
cout << "Tree found! Adding new node into tree" << endl;
if(info >= tree->info) {
cout << "info = " << info << " is greater or equals to the parent node " << tree->info << " hence moving to right of the tree" << endl;
tree->right = insertOp(tree->right, info);
}else {
cout << "info = " << info << " is less than the parent node "<< tree->info << " hence moving to left of the tree"<<endl;
tree->left = insertOp(tree->left, info);
}
}
return tree;
}
bool isLeft(struct NodePtr* ptr) {
if(ptr->left != NULL) {
return true;
} else {
return false;
}
}
bool isRight(struct NodePtr* ptr) {
if(ptr->right != NULL) {
return true;
} else {
return false;
}
}
void showInorder(struct NodePtr* ptr) {
if(isLeft(ptr)) {
showInorder(ptr->left);
}
cout << "\t" << ptr->info;
if(isRight(ptr)) {
showInorder(ptr->right);
}
}
void showPreorder(struct NodePtr* ptr) {
cout << "\t" << ptr->info;
if(isLeft(ptr)) {
showPreorder(ptr->left);
}
if(isRight(ptr)) {
showPreorder(ptr->right);
}
}
void showPostorder(struct NodePtr* ptr) {
if(isLeft(ptr)) {
showPostorder(ptr->left);
}
if(isRight(ptr)) {
showPostorder(ptr->right);
}
cout << "\t" << ptr->info;
}
NodePtr* searchBST(struct NodePtr* tree, int data) {
if(tree == NULL) {
return tree;
} else {
struct NodePtr* ptr;
ptr = tree;
while(ptr != NULL && data != ptr->info) {
ptr = (data < ptr->info)?ptr = ptr->left : ptr=ptr->right;
}
return ptr;
}
}
int getBSTHeight(struct NodePtr* ptr) {
if(ptr == NULL) return -1;
int leftHeight, rightHeight;
leftHeight = getBSTHeight(ptr->left);
rightHeight = getBSTHeight(ptr->right);
return (leftHeight > rightHeight)?leftHeight+1:rightHeight+1;
}