-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBTreeBucket.java
108 lines (85 loc) · 2.1 KB
/
BTreeBucket.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ds_011_balanced_trees;
public class BTreeBucket<T extends Comparable<T>> {
private final int order;
private T[] values;
private BTreeBucket<T>[] branches;
private int bucketSize;
public BTreeBucket(int order) {
this.order = order;
values = (T[]) new Comparable[2*this.order];
branches = new BTreeBucket[2*this.order + 1];
// System.out.printf("Order of %d Node is created: Min: %d, Max: %d\n", order, order, values.length);
}
public void add(T value) {
if(isFull()) {
System.out.printf("\tError: node is full: %d is not added\n", value);
return;
}
if(bucketSize == 0) {
values[bucketSize++] = value;
} else {
if(value.compareTo(values[bucketSize-1]) > 0) {
values[bucketSize++] = value;
}
}
}
// private BTreeBucket<T> add(BTreeBucket<T> bucket, T value) {
// ;
// }
public void remove(T value) {
if(isEmpty()) {
return;
}
values[bucketSize--] = null;
if(bucketSize < order) {
// balance operations
;
}
}
public boolean find(T value) {
if(find(this, value) != null) {
return true;
}
return false;
}
private BTreeBucket<T> find(BTreeBucket<T> node, T value) {
if(node == null) {
return null;
}
int i = 0;
for(; i < node.bucketSize; i++) {
// TODO: to be deleted, for debug purposes only
// System.out.printf("\t\tValue: %s, Node_Value: %s\n", value, node.values[i]);
if(value.equals(node.values[i])) {
return this;
}
if(value.compareTo(node.values[i]) < 0) {
return find(node.branches[i], value);
}
}
if(i == node.bucketSize) {
return find(node.branches[bucketSize], value);
}
return null;
}
// trivial
public void printBucket() {
printBucket(this);
}
private void printBucket(BTreeBucket<T> bucket) {
if(bucket == null) {
return;
}
for(int i = 0; i < bucket.bucketSize; i++) {
printBucket(bucket.branches[i]);
System.out.printf("%s ", bucket.values[i]);
}
printBucket(bucket.branches[bucketSize]);
}
private boolean isFull() {
return bucketSize == values.length;
}
private boolean isEmpty() {
return bucketSize == 0;
}
}