-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.cpp
More file actions
204 lines (197 loc) · 3.69 KB
/
binarySearchTree.cpp
File metadata and controls
204 lines (197 loc) · 3.69 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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
void insert(struct node *root, int key)
{
node *prev = NULL;
while (root != NULL)
{
prev = root;
if (root->data == key)
{
cout << "Cannot insert" << key << endl;
return;
}
else if (root->data < key)
{
root = root->right;
}
else
{
root = root->left;
}
}
struct node *n = new node(key);
if (key < prev->data)
{
prev->left = n;
}
else
{
prev->right = n;
}
}
struct node *inOrderPredecessor(struct node *root)
{
root = root->left;
while (root->right != NULL)
{
root = root->right;
}
return root;
}
struct node *deleteNode(struct node *root, int value)
{
struct node *iPre;
if (root == NULL)
{
return NULL;
}
if (root->left == NULL && root->right == NULL)
{
free(root);
return NULL;
}
if (value < root->data)
{
root->left = deleteNode(root->left, value);
}
else if (value > root->data)
{
root->right = deleteNode(root->right, value);
}
//Deletion strategy when the node is found
else
{
iPre = inOrderPredecessor(root);
root->data = iPre->data;
root->left = deleteNode(root->left, iPre->data);
}
return root;
}
struct node *search(struct node *root, int key)
{
if (root = NULL)
{
return NULL;
}
if (root->data == key)
{
return root;
}
else if (root->data > key)
{
return search(root->left, key);
}
else
{
return search(root->right, key);
}
}
void topview(struct node *root)
{
if (root == NULL)
{
return;
}
if (root->left == NULL)
{
while (root!= NULL)
{
cout << root->data << " ";
root = root-> right;
}
}
else if (root->right == NULL)
{
while (root!= NULL)
{
cout << root->data << " ";
root = root-> left;
}
}
}
int isBst(struct node *root)
{
static struct node *prev = NULL;
if (root != NULL)
{
if (!isBst(root->left))
{
return 0;
}
if (prev != NULL && root->data <= prev->data)
{
return 0;
}
prev = root;
return isBst(root->right);
}
else
{
return 1;
}
}
void preorder(struct node *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
void inorder(struct node *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
void postorder(struct node *root)
{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
int main()
{
struct node *root = new node(5);
root->left = new node(3);
root->right = new node(6);
root->left->left = new node(1);
root->left->right = new node(4);
cout << (isBst(root) == 1 ? "It is binary search tree" : "It is not a binary search tree") << endl;
// preorder(root);
// cout << endl;
// inorder(root);
// cout << endl;
// postorder(root);
// cout << endl;
// insert(root, 6);
inorder(root);
cout << endl;
deleteNode(root, 3);
inorder(root);
cout << endl;
return 0;
}