-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPlusLeafNode.java
More file actions
130 lines (115 loc) · 3.64 KB
/
Copy pathBPlusLeafNode.java
File metadata and controls
130 lines (115 loc) · 3.64 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*;
/**
* Author: Asif Qureshi, Cole Keller, Goldhuli, Sharjeel
* This class serves as a representation of the leaf nodes in the B+ tree,
* which store key-value pairs. Leaf nodes do not have any children.
* The minimum and maximum number of key-value pairs that a leaf node can
* contain are determined by the max degree of the B+ tree, denoted by m.
* The leaf nodes are connected in a doubly linked list, with each leaf node
* having a left and right sibling.
*/
public class BPlusLeafNode extends BPlusNode {
public static final int MAX_ENTRY = 16;
public static final int MIN_ENTRY = 16;
public int numberOfEntry;
public BPlusLeafNode leftSibling;
public BPlusLeafNode rightSibling;
public Product[] products;
/**
* Constructor to initialize a leaf node with a product.
* @param product: first dictionary pair insert into new node
*/
public BPlusLeafNode(Product product) {
super();
this.products = new Product[MAX_ENTRY];
this.numberOfEntry = 0;
this.insertProduct(product);
}
/**
* Constructor to initialize a leaf node.
*
* @param products: list of product
* @param parent: parent to be the leaf node
*/
public BPlusLeafNode(Product[] products, BPlusIndexNode parent) {
super();
this.products = products;
this.numberOfEntry = searchEmpty(products);
this.parent = parent;
}
/**
* Getter method for get all products.
*
* @return all products.
*/
public Product[] getProducts() {
return products;
}
/**
* This method remove a product from this leaf node.
*
* @param index: the location of the product.
*/
public void deleteProduct(int index) {
//System.out.println(Arrays.toString(products));
products[index] = null;
//System.out.println(Arrays.toString(products));
numberOfEntry--;
}
/**
* This method attempts to insert a dictionary pair within the product list.
*
* @param product: product to be inserted
* @return is successful
*/
public boolean insertProduct(Product product) {
if (this.isFull()) {
return false;
} else {
this.products[numberOfEntry] = product;
numberOfEntry++;
Arrays.sort(this.products, 0, numberOfEntry);
return true;
}
}
/**
* This straightforward method checks whether the Index Node has fallen below the minimum degree or not.
* An Index Node is considered has lower then minimum when its current number of children is less than
* the minimum allowed.
*/
public boolean hasLowerThenMinimum() {
return numberOfEntry < MIN_ENTRY;
}
/**
* This method determines if the Node is considered full.
*/
public boolean isFull() {
return numberOfEntry == MAX_ENTRY - 1;
}
/**
* This method determines if the leaf node is capable of
* lending one of its entry to a deficient node.
*/
public boolean canLend() {
return numberOfEntry > MIN_ENTRY;
}
/**
* This method determines if the BPlusIndexNode is capable of being
* merged with.
*/
public boolean canMerge() {
return numberOfEntry == MIN_ENTRY;
}
/**
* This method search on an array of product
* and returns the index of the first null entry found.
*/
private int searchEmpty(Product[] products) {
for (int i = 0; i < products.length; i++) {
if (products[i] == null) {
return i;
}
}
return -1;
}
}