-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_1.cpp
190 lines (176 loc) · 4.98 KB
/
code_1.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
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// code_1.cpp
// Data Structure
//
// Created by Mohd Shoaib Rayeen on 10/04/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node {
int key;
node *left, *right;
};
node *newNode(int item) {
node *temp = new node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(node *root) {
if (root != NULL) {
inorder(root->left);
cout << root->key << "\t";
inorder(root->right);
}
}
void preorder(node *root) {
if (root != NULL) {
cout << root->key << "\t";
preorder(root->left);
preorder(root->right);
}
}
void postorder(node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout << root->key << "\t";
}
}
node* insert(node* node, int key) {
if (node == NULL) {
return newNode(key);
}
if (key <= node->key) {
node->left = insert(node->left, key);
}
else if (key > node->key) {
node->right = insert(node->right, key);
}
return node;
}
node * minValueNode(struct node* node) {
struct node* current = node;
while (current->left != NULL) {
current = current->left;
}
return current;
}
node* deleteNode(struct node* root, int key) {
if (root == NULL) {
return root;
}
if (key < root->key) {
root->left = deleteNode(root->left, key);
}
else if (key > root->key) {
root->right = deleteNode(root->right, key);
}
else {
if (root->key == key) {
root = NULL;
free(root);
return root;
}
else if (root->left == NULL) {
node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
node *temp = root->left;
free(root);
return temp;
}
node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
bool Search(node *root, int key) {
while (root != NULL) {
if (key > root->key) {
root = root->right;
}
else if (key < root->key) {
root = root->left;
}
else {
return true;
}
}
return false;
}
int main() {
node *root = NULL;
int value;
char choice;
bool done = true;
while(done) {
cout << "\n\n\t\t--------Binary Search Tree-------";
cout << "\n 1. Insertion";
cout << "\n 2. Displaying Tree";
cout << "\n 3. Delete a node";
cout << "\n 4. Search in the Tree";
cout << "\n 5. Exit ";
cout << "\n Enter your Choice\t:\t";
cin >> choice;
switch(choice) {
case '1' : cout << "\n Enter the element\t:\t";
cin >> value;
if(root == NULL ) {
root = insert(root, value);
}
else {
insert(root , value);
}
break;
case '2' :
if(root == NULL ) {
cout << "\n Tree is Empty\n";
break;
}
cout << "\n Inorder\t:\t";
inorder(root);
cout << "\n Preorder\t:\t";
preorder(root);
cout << "\n Postorder\t:\t";
postorder(root);
break;
case '3' :
if(root == NULL ) {
cout << "\n Tree is Empty\n";
break;
}
cout << "\n Enter the element\t:\t";
cin >> value;
if(!Search(root , value)) {
cout << "\n" << value << " is not in the Tree\n";
break;
}
root = deleteNode(root , value);
break;
case '4' :
if(root == NULL ) {
cout << "\n Tree is Empty\n";
break;
}
cout << "\n Enter the element\t:\t";
cin >> value;
if(!Search(root , value)) {
cout << "\n" << value << " is not available in the Tree\n";
}
else {
cout << "\n" << value << " is available in the Tree\n";
}
break;
case '5' : done = false;
break;
default : cout << "\n Invalid Choice\n ";
}
}
return 0;
}