-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTrees.java
More file actions
363 lines (297 loc) · 11.9 KB
/
Copy pathbinarySearchTrees.java
File metadata and controls
363 lines (297 loc) · 11.9 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class binarySearchTrees {
//Inspired from Binary Search Algorithm.
//The tree is sorted.
//For any node n: everything in left subtree is less than n and
//anything in right subtree is greater than n.
public static void main(String args[]){
binarySearchTrees bst = new binarySearchTrees();
//12 10 14 8 11 13 15 -1 -1 -1 -1 -1 -1 -1 -1
//4 2 7 1 5 6 8 -1 -1 -1 -1 -1 -1 -1 -1
//24 12 32 8 18 27 39 4 11 -1 -1 25 -1 -1 42 -1 -1 -1 -1 -1 -1 -1 -1
BinarySearchTreeNode<Integer> root = bst.takeInputLevelwise(new Scanner(System.in));
System.out.println();
bst.elementsInRangeK1K2(root, 10, 15);
System.out.println();
System.out.println(bst.isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE));
//bst.SortedArrayToBST(new int[] {1,2,3,4,5,6,7}, 0, 6);
bst.constructLinkedList(root);
ArrayList<Integer> alist = bst.getPath(root, 16);
for(int value : alist){
System.out.print(value +" ");
}
System.out.println();
bst.printLevelWise(root);
bst.preOrder(root);
System.out.println();
bst.preOrderIterative(root);
System.out.println();
bst.inOrder(root);
System.out.println();
bst.inOrderIterative(root);
System.out.println();
bst.postOrder(root);
System.out.println();
bst.postOrderInterative(root);
System.out.println();
bst.levelOrderTraversal(root);
}
public class BinarySearchTreeNode<T> implements Comparable<BinarySearchTreeNode<T>>{
T data;
BinarySearchTreeNode<T> leftNode;
BinarySearchTreeNode<T> rightNode;
BinarySearchTreeNode(){}
BinarySearchTreeNode(T data){
this.data = data;
}
@Override
public int compareTo(BinarySearchTreeNode<T> o) {
return Integer.compare((Integer)o.data, (Integer)this.data);
}
}
public BinarySearchTreeNode<Integer> takeInput(Scanner sc){
System.out.println("Enter the value of the node");
int data = sc.nextInt();
BinarySearchTreeNode<Integer> node = new BinarySearchTreeNode<Integer>(data);
node.leftNode = takeInput(sc);
node.rightNode = takeInput(sc);
return node;
}
public BinarySearchTreeNode<Integer> takeInputLevelwise(Scanner sc){
Queue<BinarySearchTreeNode<Integer>> queue = new LinkedList<BinarySearchTreeNode<Integer>>();
System.out.println("Enter the value of the node: ");
int nodeData = sc.nextInt();
if(nodeData == -1) return null;
BinarySearchTreeNode<Integer> node = new BinarySearchTreeNode<Integer>(nodeData);
queue.add(node);
while(!queue.isEmpty()){
BinarySearchTreeNode<Integer> item = queue.remove();
System.out.print("Enter the value of the left node of "+item.data+" :");
int leftData = sc.nextInt();
BinarySearchTreeNode<Integer> left = (leftData == -1) ? null : new BinarySearchTreeNode<Integer>(leftData);
if(left != null){
item.leftNode = left;
queue.add(left);
}
System.out.print("Enter the value of the right node of "+item.data+" :");
int rightData = sc.nextInt();
BinarySearchTreeNode<Integer> right = (rightData == -1) ? null : new BinarySearchTreeNode<Integer>(rightData);
if(right != null){
item.rightNode = right;
queue.add(right);
}
}
return node;
}
public void printLevelWise(BinarySearchTreeNode<Integer> node){
Queue<BinarySearchTreeNode<Integer>> queue = new LinkedList<BinarySearchTreeNode<Integer>>();
queue.add(node);
while(!queue.isEmpty()){
BinarySearchTreeNode<Integer> item = queue.remove();
int leftData = (item.leftNode == null) ? -1 : item.leftNode.data;
int rightData = (item.rightNode == null) ? -1 : item.rightNode.data;
System.out.print(item.data+":L:"+leftData+",R:"+rightData);
if(item.leftNode != null) queue.add(item.leftNode);
if(item.rightNode != null) queue.add(item.rightNode);
System.out.println();
}
}
public boolean searchInBST(BinarySearchTreeNode<Integer> node, int k){
if(node == null) return false;
if(node.data == k){
return true;
}
boolean val = false;
if(node.data > k){
val = searchInBST(node.leftNode, k);
}else if(node.data < k){
val = searchInBST(node.rightNode, k);
}
return val;
}
public void elementsInRangeK1K2(BinarySearchTreeNode<Integer> node,int k1,int k2){
if(node == null) return;
if(node.data > k2){
elementsInRangeK1K2(node.leftNode, k1, k2);
}else if(node.data < k1){
elementsInRangeK1K2(node.rightNode, k1, k2);
}else{
elementsInRangeK1K2(node.leftNode, k1, k2);
System.out.print(node.data+" ");
elementsInRangeK1K2(node.rightNode, k1, k2);
}
}
public boolean isBST(BinarySearchTreeNode<Integer> node, int min, int max){
if(node == null) return true;
if(node.data < min || node.data >= max){
return false;
}
boolean left = isBST(node.leftNode, min, node.data);
boolean right = isBST(node.rightNode, node.data, max);
return left && right;
}
public BinarySearchTreeNode<Integer> SortedArrayToBST(int[] arr, int start, int end){
if(start > end){
return null;
}
int mid = (start + end)/2;
BinarySearchTreeNode<Integer> node = new BinarySearchTreeNode<Integer>(arr[mid]);
node.leftNode = SortedArrayToBST(arr, start, mid-1);
node.rightNode = SortedArrayToBST(arr, mid+1, end);
return node;
}
public LinkedListNode<Integer> constructLinkedList(BinarySearchTreeNode<Integer> node){
//This does not work, fix it
if (node == null)
return null;
LinkedListNode<Integer> mid = new LinkedListNode<Integer>(node.data);
LinkedListNode<Integer> lNode = constructLinkedList(node.leftNode);
LinkedListNode<Integer> rNode = constructLinkedList(node.rightNode);
if (lNode != null) {
lNode.next = mid;
mid.next = rNode;
}
return lNode;
}
class LinkedListNode<T>{
T data;
LinkedListNode<T> next;
LinkedListNode(T data){
this.data = data;
}
}
public ArrayList<Integer> getPath(BinarySearchTreeNode<Integer> node, int data){
if(node == null) return null;
ArrayList<Integer> output = new ArrayList<>();
if(node.data == data){
output.add(node.data);
return output;
}
if(node.data > data){
ArrayList<Integer> left = new ArrayList<>();
left = getPath(node.leftNode, data);
if(left != null && !left.isEmpty()){
left.add(node.data);
output.addAll(left);
}
}else if(node.data < data){
ArrayList<Integer> right = new ArrayList<>();
right = getPath(node.rightNode, data);
if(right != null && !right.isEmpty()){
right.add(node.data);
output.addAll(right);
}
}
return output;
}
/*
Tree Traversals - Both Recursive and Interative
inOrder - Left-Root-Right
preOrder - Root-Left-Right
postOrder - Left-Right-Root
*/
public void inOrder(BinarySearchTreeNode<Integer> node){
if(node == null) return;
inOrder(node.leftNode);
System.out.print(node.data+" ");
inOrder(node.rightNode);
}
public void inOrderIterative(BinarySearchTreeNode<Integer> node){
//Non-Recursive Approach
//Left-Root-Right
Stack<BinarySearchTreeNode<Integer>> stack = new Stack<BinarySearchTreeNode<Integer>>();
BinarySearchTreeNode<Integer> currentNode = node;
boolean done = false;
while(!done){
if(currentNode != null){
stack.push(currentNode);
currentNode = currentNode.leftNode;
}else{
if(stack.isEmpty()) done = true;
else{
currentNode = stack.pop();
System.out.print(currentNode.data+" ");
currentNode = currentNode.rightNode;
}
}
}
}
public void preOrder(BinarySearchTreeNode<Integer> node){
if(node == null) return;
System.out.print(node.data+" ");
preOrder(node.leftNode);
preOrder(node.rightNode);
}
public void preOrderIterative(BinarySearchTreeNode<Integer> node){
//Non-Recursive Approach
//Root-Left-Right
//Use a stack, push the root, pop one by one, print the popped element, push it's right node, then it's left node
Stack<BinarySearchTreeNode<Integer>> stack = new Stack<BinarySearchTreeNode<Integer>>();
stack.push(node);
while(!stack.isEmpty()){
BinarySearchTreeNode<Integer> item = stack.pop();
System.out.print(item.data+" ");
if(item.rightNode != null)
stack.push(item.rightNode);
if(item.leftNode != null)
stack.push(item.leftNode);
}
}
public void postOrder(BinarySearchTreeNode<Integer> node){
//Left-Right-Root
if(node == null) return;
postOrder(node.leftNode);
postOrder(node.rightNode);
System.out.print(node.data+" ");
}
public void postOrderInterative(BinarySearchTreeNode<Integer> node){
/*
Non-Recursive Approach
Left-Right-Root
Maintain two stacks, one for exploration and second for to print the order.
The algorithm starts by pushing the root onto one. Then, it enters a loop
where it pops nodes from one, pushes them onto two, and pushes their
left and right children onto one. This process continues until all nodes
have been visited.
*/
Stack<BinarySearchTreeNode<Integer>> one = new Stack<BinarySearchTreeNode<Integer>>();
Stack<BinarySearchTreeNode<Integer>> two = new Stack<BinarySearchTreeNode<Integer>>();
BinarySearchTreeNode<Integer> currentNode = null;
one.push(node);
while(!one.isEmpty()){
currentNode = one.pop();
two.push(currentNode);
if(currentNode.leftNode != null)
one.push(currentNode.leftNode);
if(currentNode.rightNode != null)
one.push(currentNode.rightNode);
}
while(!two.isEmpty()){
System.out.print(two.pop().data+" ");
}
}
public void levelOrderTraversal(BinarySearchTreeNode<Integer> node){
Queue<BinarySearchTreeNode<Integer>> queue = new LinkedList<BinarySearchTreeNode<Integer>>();
queue.add(node);
queue.add(new BinarySearchTreeNode<Integer>(Integer.MIN_VALUE));
BinarySearchTreeNode<Integer> currentNode = null;
while(!queue.isEmpty()){
currentNode = queue.remove();
if(currentNode.data > Integer.MIN_VALUE){
System.out.print(currentNode.data+" ");
}else if(currentNode.data == Integer.MIN_VALUE && queue.size() >= 1){
System.out.println();
queue.add(new BinarySearchTreeNode<Integer>(Integer.MIN_VALUE));
}
if(currentNode.leftNode != null)
queue.add(currentNode.leftNode);
if(currentNode.rightNode != null){
queue.add(currentNode.rightNode);
}
}
}
}