-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.java
More file actions
67 lines (62 loc) · 1.12 KB
/
bst.java
File metadata and controls
67 lines (62 loc) · 1.12 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
import java.util.*;
class Node {
int value;
public Node left,right;
public Node(int value) {
this.value=value;
this.left=this.right=null;
}
}
class BSTree {
Node root;
public BSTree() {
root=null;
}
public void inOrder_(Node root) {
if(root!=null) {
inOrder_(root.left);
System.out.print(root.value+"\t");
inOrder_(root.right);
}
}
public void inOrder() {
inOrder_(root);
}
public void insert_(Node root,int value) {
if(value < root.value) {
if(root.left==null)
root.left=new Node(value);
else
insert_(root.left,value);
}
else {
if(root.right==null)
root.right=new Node(value);
else
insert_(root.right,value);
}
}
public void insert(int value) {
if(root!=null) {
insert_(root, value);
}
else {
root=new Node(value);
}
}
public static int[] randomArray(int n) {
int[] ret=new int[n];
for(int i=0;i<n;i++) {
ret[i]=(int)(Math.random() * 10000);
}
return ret;
}
public static void main(String[] args) {
BSTree bst=new BSTree();
int[] a=randomArray(1000000000);
for(int x: a) {
bst.insert(x);
}
bst.inOrder();
}
}