-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
54 lines (43 loc) · 993 Bytes
/
BinarySearchTree.java
File metadata and controls
54 lines (43 loc) · 993 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
package Lec38;
import java.util.*;
public class BinarySearchTree {
public class Node {
int data;
Node left;
Node right;
}
private Node root;
public BinarySearchTree(int[] in) {
// TODO Auto-generated constructor stub
this.root = CreateTree(in, 0, in.length - 1);
}
public Node CreateTree(int[] in, int si, int ei) {
// TODO Auto-generated method stub
if (si > ei) {
return null;
}
int mid = (si + ei) / 2;
Node nn = new Node();
nn.data = in[mid];
nn.left = CreateTree(in, si, mid - 1);
nn.right = CreateTree(in, mid + 1, ei);
return nn;
}
public void PreOrder() {
PreOrder(this.root);
System.out.println();
}
private void PreOrder(Node node) {
if (node == null) {
return;
}
System.out.print(node.data + " ");
PreOrder(node.left);
PreOrder(node.right);
}
public static void main(String[] args) {
int[] in = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
BinarySearchTree bst = new BinarySearchTree(in);
bst.PreOrder();
}
}