-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.java
More file actions
68 lines (53 loc) · 1.15 KB
/
test.java
File metadata and controls
68 lines (53 loc) · 1.15 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
68
public class test {
public static Node select(Node root, int i) {
// if(root == null){
// return root;
// }
if (i > root.count||i<0) {
System.out.println("Number larger than tree size");
return null;
}
if(i==1&&root.count==1){
return root;
}
else if (i == root.left.count+1) {
System.out.println(root.data);
return root;
}
else if (i > root.left.count+1) {
System.out.println("!"+root.data);
return select(root.right, i-root.left.count-1);
}
else if (i < root.left.count+1) {
System.out.println("?"+root.data);
return select(root.left, i);
}
return null;
}
public static void main(String[] args) {
Node n1 = new Node(5);
n1.count = 6;
n1.left = new Node(3);
n1.left.count = 2;
n1.left.right = new Node(4);
n1.left.right.count =1;
n1.right = new Node(7);
n1.right.count = 3;
n1.right.left = new Node(6);
n1.right.left.count = 1;
n1.right.right = new Node(9);
n1.right.right.count = 1;
System.out.println(select(n1,1).data);
}
}
class Node {
Node left ;
Node right ;
int data;
int count;
Node(int i){
this.data = i;
this.left = null;
this.right = null;
}
}