-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BinarySearchTree.cs
241 lines (227 loc) · 5.96 KB
/
BinarySearchTree.cs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Binary search tree(BST) is a rooted binary tree whose internal nodes each store a key greater than all
the keys in the node's left subtree and less than those in its right subtree. */
using System;
namespace Binary_Search_Tree
{
public class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
public class BinarySearchTree
{
Node root;
BinarySearchTree()
{
root = null;
}
public void Insert(int data)
{
root = Insert(root, data);
}
//Insert New Node in Binary search tree
public Node Insert(Node root, int data)
{
if (root is null)
{
Node newNode = new Node(data);
root = newNode;
return root;
}
if (data < root.data)
{
root.left = Insert(root.left, data);
}
else if (data > root.data)
{
root.right = Insert(root.right, data);
}
return root;
}
//To limit parameters
public void Inorder()
{
Inorder(root);
}
// to perform inorder traversal of BST
public void Inorder(Node root)
{
if (root != null)
{
Inorder(root.left);
Console.WriteLine(root.data);
Inorder(root.right);
}
}
//To limit parameters
public void Postorder()
{
Postorder(root);
}
// to perform postorder traversal of BST
public void Postorder(Node root)
{
if (root != null)
{
Postorder(root.left);
Postorder(root.right);
Console.WriteLine(root.data);
}
}
//To limit parameters
public void Preorder()
{
Preorder(root);
}
// to perform preorder traversal of BST
public void Preorder(Node root)
{
if (root != null)
{
Console.WriteLine(root.data);
Preorder(root.left);
Preorder(root.right);
}
}
//To limit parameters
public int FindMin()
{
return FindMin(root);
}
//To find the min value in BST
public int FindMin(Node root)
{
if(root.left is null)
{
return root.data;
}
return FindMin(root.left);
}
//To limit parameters
public int FindMax()
{
return FindMax(root);
}
//To find the max value in BST
public int FindMax(Node root)
{
if (root.right is null)
{
return root.data;
}
return FindMax(root.right);
}
//To search for a specific node in BST
public Node Find(int data)
{
Node current = root;
while (data != current.data)
{
if (data < current.data)
{
current = current.left;
}
else
{
current = current.right;
}
if (current is null)
{
Console.WriteLine("Node is not found.");
return null;
}
}
return current;
}
//To limit number of parameters
public void Delete(int data)
{
root = Delete(root, data);
}
//To Delete a specific node
public Node Delete(Node root, int data)
{
if (root == null)
return root;
//search for the node with the needed value
if (data < root.data)
root.left = Delete(root.left, data);
else if (data > root.data)
root.right = Delete(root.right, data);
else
{
//for nodes with one or none child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
//for nodes with two childs get min value in right
root.data = FindMin(root.right);
root.right = Delete(root.right, root.data);
}
return root;
}
public static void Main(String[] args)
{
BinarySearchTree BST = new BinarySearchTree();
BST.Insert(50);
BST.Insert(30);
BST.Insert(20);
BST.Insert(40);
BST.Insert(70);
BST.Insert(60);
BST.Insert(80);
Console.WriteLine("Inorder traversal");
BST.Inorder();
Console.WriteLine("Delete 20");
BST.Delete(20);
Console.WriteLine("Inorder traversal");
BST.Inorder();
Console.WriteLine("Preorder traversal");
BST.Preorder();
Console.WriteLine("Postorder traversal");
BST.Postorder();
Console.WriteLine("Min Value:");
Console.WriteLine(BST.FindMin());
Console.WriteLine("Max Value:");
Console.WriteLine(BST.FindMax());
}
}
}
/*
Sample Input: 50 30 20 4070 60 80
inorder traversal: 20 30 40 50 60 70 80
Delete Node with data = 20
Inorder traversal: 30 40 50 60 70 80
Preorder traversal
50
30
40
70
60
80
Postorder traversal
40
30
60
80
70
50
Min Value:
30
Max Value:
80
Insersion Time Complexity: O(n)
Insersion Space Complexity: O(n)
Deletion Time Complexity: O(n)
Deletion Space Complexity: O(n)
Searching Time Complexity: O(n)
Searching Space Complexity: O(n)
*/