-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
61 lines (39 loc) · 964 Bytes
/
Copy pathBST.java
File metadata and controls
61 lines (39 loc) · 964 Bytes
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
import java.io.*;
import java.util.*;
public class BST {
public BST() {
}
public static Node root;
public static void insert(int value,Node root) {
System.out.println(root.data);
if(root == null) {
System.out.println("Iam in node");
root = new Node(value);
return;
}
if(value < root.data) {
System.out.println("I am in node.left");
insert(value, root.left);
} else if(value > root.data) {
System.out.println("I am in node.right");
insert(value, root.right);
}
}
public static void inOrderTraversal(Node focusNode) {
if(focusNode == null) {
System.out.print("");
return;
}
inOrderTraversal(focusNode.left);
System.out.print(focusNode.data + " ");
inOrderTraversal(focusNode.right);
}
public static void main(String[] args) {
BST tree = new BST();
int[] arr = {2,3,5,6,7,9};
for(int i = 0; i < arr.length; i++) {
tree.insert(arr[i], root);
}
inOrderTraversal(root);
}
}