-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
binary_search_tree.cpp
174 lines (159 loc) · 3.75 KB
/
binary_search_tree.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
/**
* \file
* \brief A simple tree implementation using structured nodes
*
* \todo update code to use C++ STL library features and OO structure
* \warning This program is a poor implementation - C style - and does not
* utilize any of the C++ STL features.
*/
#include <iostream>
struct node {
int val;
node *left;
node *right;
};
struct Queue {
node *t[100];
int front;
int rear;
};
Queue queue;
void enqueue(node *n) { queue.t[queue.rear++] = n; }
node *dequeue() { return (queue.t[queue.front++]); }
void Insert(node *n, int x) {
if (x < n->val) {
if (n->left == NULL) {
node *temp = new node;
temp->val = x;
temp->left = NULL;
temp->right = NULL;
n->left = temp;
} else {
Insert(n->left, x);
}
} else {
if (n->right == NULL) {
node *temp = new node;
temp->val = x;
temp->left = NULL;
temp->right = NULL;
n->right = temp;
} else {
Insert(n->right, x);
}
}
}
int findMaxInLeftST(node *n) {
while (n->right != NULL) {
n = n->right;
}
return n->val;
}
void Remove(node *p, node *n, int x) {
if (n->val == x) {
if (n->right == NULL && n->left == NULL) {
if (x < p->val) {
p->right = NULL;
} else {
p->left = NULL;
}
} else if (n->right == NULL) {
if (x < p->val) {
p->right = n->left;
} else {
p->left = n->left;
}
} else if (n->left == NULL) {
if (x < p->val) {
p->right = n->right;
} else {
p->left = n->right;
}
} else {
int y = findMaxInLeftST(n->left);
n->val = y;
Remove(n, n->right, y);
}
} else if (x < n->val) {
Remove(n, n->left, x);
} else {
Remove(n, n->right, x);
}
}
void BFT(node *n) {
if (n != NULL) {
std::cout << n->val << " ";
enqueue(n->left);
enqueue(n->right);
BFT(dequeue());
}
}
void Pre(node *n) {
if (n != NULL) {
std::cout << n->val << " ";
Pre(n->left);
Pre(n->right);
}
}
void In(node *n) {
if (n != NULL) {
In(n->left);
std::cout << n->val << " ";
In(n->right);
}
}
void Post(node *n) {
if (n != NULL) {
Post(n->left);
Post(n->right);
std::cout << n->val << " ";
}
}
int main() {
queue.front = 0;
queue.rear = 0;
int value;
int ch;
node *root = new node;
std::cout << "\nEnter the value of root node :";
std::cin >> value;
root->val = value;
root->left = NULL;
root->right = NULL;
do {
std::cout << "\n1. Insert"
<< "\n2. Delete"
<< "\n3. Breadth First"
<< "\n4. Preorder Depth First"
<< "\n5. Inorder Depth First"
<< "\n6. Postorder Depth First";
std::cout << "\nEnter Your Choice : ";
std::cin >> ch;
int x;
switch (ch) {
case 1:
std::cout << "\nEnter the value to be Inserted : ";
std::cin >> x;
Insert(root, x);
break;
case 2:
std::cout << "\nEnter the value to be Deleted : ";
std::cin >> x;
Remove(root, root, x);
break;
case 3:
BFT(root);
break;
case 4:
Pre(root);
break;
case 5:
In(root);
break;
case 6:
Post(root);
break;
}
} while (ch != 0);
return 0;
}