-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4th.cpp
More file actions
165 lines (131 loc) · 3.33 KB
/
4th.cpp
File metadata and controls
165 lines (131 loc) · 3.33 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<bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int d) {
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
int getHeight(Node* node){
if(node == NULL){
return 0;
}
return max(getHeight(node->left), getHeight(node->right)) + 1;
}
void invertTree(Node* root){
if(root == NULL)
return;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
}
class BST {
Node* root;
public:
BST() {
root = NULL;
}
void insert(int d){
root = insertUtil(root, d);
}
bool search(int d){
return searchUtil(root, d);
}
int minValue(){
if(root == NULL)
return -1;
Node* node = root;
while(node->left != NULL)
node = node->left;
return node->data;
}
int nodesInLongestPath(){
return getHeight(root);
}
void invert() {
invertTree(root);
}
bool searchUtil(Node* node, int d) {
if(node == NULL)
return false;
if(node->data == d)
return true;
if( d < node->data ){
return searchUtil(node->left, d);
}
else {
return searchUtil(node->right, d);
}
}
Node* insertUtil(Node* node, int d){
// base case
if(node == NULL){
node = new Node(d);
return node;
}
if( d > node->data ){
node->right = insertUtil(node->right, d);
}
else {
node->left = insertUtil(node->left, d);
}
}
};
int main() {
BST t;
int choice = -1, val = -1;
bool found = 0;
while(choice != 6){
cout << "************* MENU *************\n";
cout << "1. Insert Value\n";
cout << "2. Search Value\n";
cout << "3. Print Minimum Data\n";
cout << "4. Get Number of Nodes in longest path\n";
cout << "5. Invert the Binary Search Tree\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
cout << "Enter Value to insert: ";
cin >> val;
t.insert(val);
cout << val << " inserted Successfully.";
break;
case 2:
cout << "Enter Value to Search: ";
cin >> val;
found = t.search(val);
if(found) {
cout << "Value found in the BST.";
}
else {
cout << "Value not found in the BST.";
}
break;
case 3:
val = t.minValue();
cout << "Minimum value in BST: " << val;
break;
case 4:
cout << "No of nodes in longest path: " << t.nodesInLongestPath();
break;
case 5:
t.invert();
cout << "BST inverted, reinvert for other operations to work";
break;
case 6:
return 0;
default:
cout << "Invalid Choice.";
break;
}
cout << "\n\n";
}
return 0;
}