-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintSortedArray.java
More file actions
57 lines (38 loc) · 853 Bytes
/
Copy pathprintSortedArray.java
File metadata and controls
57 lines (38 loc) · 853 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
public class BST {
public class Node{
int data;
Node left, right;
public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public static Node root = null;
public static void insert(int value, Node root) {
if(root == null) {
root = new Node(value);
}
if(value < root.data) {
insert(value, root.left);
} else if(value > root.data) {
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) {
int[] arr = {2,3,5,6,7,9};
for(int i = 0; i < arr.length; i++) {
insert(arr[i], root);
}
inOrderTraversal(root);
}
}