-
Notifications
You must be signed in to change notification settings - Fork 28
/
DummyBTreeNode.java
96 lines (75 loc) · 1.84 KB
/
DummyBTreeNode.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
package ds_011_balanced_trees;
public class DummyBTreeNode<T extends Comparable<T>> {
private final int order;
public T[] values;
public DummyBTreeNode<T>[] branches;
private int bucketSize;
public DummyBTreeNode(int order) {
this.order = order;
values = (T[]) new Comparable[2*this.order];
branches = new DummyBTreeNode[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()) {
return;
}
values[bucketSize++] = 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 DummyBTreeNode<T> find(DummyBTreeNode<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
private boolean isFull() {
return bucketSize == values.length;
}
private boolean isEmpty() {
return bucketSize == 0;
}
public void printBucket() {
printBucket(this);
}
private void printBucket(DummyBTreeNode<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]);
}
}