-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.binarytree
More file actions
165 lines (147 loc) · 3.76 KB
/
3.binarytree
File metadata and controls
165 lines (147 loc) · 3.76 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
#include<iostream>
#include<cstdlib>
using namespace std;
class node {
public:
node *leftc;
int data;
node *rightc;
};
class binarytree {
private:
node *temp;
int op;
public:
node *root;
binarytree() {
root = NULL;
}
void create();
void insert(node*, node*);
void inorder(node*);
void preorder(node*);
void postorder(node*);
void search(node*, int);
int height(node*);
};
int main() {
binarytree B;
int choice;
while(1) {
cout << "\nMENU" << endl;
cout << "1. Create a binary search tree" << endl;
cout << "2. Insert a new node" << endl;
cout << "3. Display Inorder traversal" << endl;
cout << "4. Display Preorder traversal" << endl;
cout << "5. Display Postorder traversal" << endl;
cout << "6. Search for a value" << endl;
cout << "7. Find height of the tree" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
case 2:
B.create();
break;
case 3:
B.inorder(B.root);
cout << endl;
break;
case 4:
B.preorder(B.root);
cout << endl;
break;
case 5:
B.postorder(B.root);
cout << endl;
break;
case 6:
int value;
cout << "Enter value to search: ";
cin >> value;
B.search(B.root, value);
break;
case 7:
cout << "Height of the tree: " << B.height(B.root) << endl;
break;
case 8:
cout << "Exiting...!" << endl;
exit(0);
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}
void binarytree::create() {
int value;
do {
temp = new node;
cout << "Enter the value: ";
cin >> value;
temp->data = value;
temp->leftc = temp->rightc = NULL;
if (root == NULL)
root = temp;
else
insert(root, temp);
cout << "Enter 1 to insert another node, 0 to stop: ";
cin >> op;
} while(op == 1);
}
void binarytree::insert(node *root, node *temp) {
if (temp->data < root->data) {
if (root->leftc == NULL)
root->leftc = temp;
else
insert(root->leftc, temp);
} else {
if (root->rightc == NULL)
root->rightc = temp;
else
insert(root->rightc, temp);
}
}
void binarytree::inorder(node *root) {
if (root != NULL) {
inorder(root->leftc);
cout << root->data << " ";
inorder(root->rightc);
}
}
void binarytree::preorder(node *root) {
if (root != NULL) {
cout << root->data << " ";
preorder(root->leftc);
preorder(root->rightc);
}
}
void binarytree::postorder(node *root) {
if (root != NULL) {
postorder(root->leftc);
postorder(root->rightc);
cout << root->data << " ";
}
}
void binarytree::search(node *root, int value) {
if (root == NULL) {
cout << "Value " << value << " not found in the tree." << endl;
return;
}
if (root->data == value) {
cout << "Value " << value << " found in the tree." << endl;
return;
}
if (value < root->data)
search(root->leftc, value);
else
search(root->rightc, value);
}
int binarytree::height(node *root) {
if (root == NULL)
return -1;
int leftHeight = height(root->leftc);
int rightHeight = height(root->rightc);
return max(leftHeight, rightHeight) + 1;
}