-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10p10.java
More file actions
57 lines (54 loc) · 1.34 KB
/
Copy pathq10p10.java
File metadata and controls
57 lines (54 loc) · 1.34 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
public class q10p10{
static Node root = null;
public static void main(String[] args){
int[] arr = {5,1,4,4,5,9,7,13,3};
for(int a : arr){
buildBST(a);
}
//System.out.println(root);
System.out.println(root.rank(10));
}
public static void buildBST(int num){
if(root == null)
root = new Node(num);
else
root.insert(num);
System.out.println(">> "+ root);
}
}
class Node{
int leftSize = 0;
int value = 0;
Node left, right;
public Node(int value){
this.value = value;
}
public int rank(int num){
if(num == value)
return leftSize;
else if(num < value){
if(left == null)
return -1;
else
return left.rank(num);
}else{
int temp = right == null ? -1 : right.rank(num);
if(temp == -1) return -1;
else return leftSize + 1 + temp;
}
}
public void insert(int num){
if(num <= value){
if(left == null)
left = new Node(num);
else
left.insert(num);
this.leftSize++;
}else{
if(right == null)
right = new Node(num);
else
right.insert(num);
}
}
}