-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPlusTree.java
218 lines (189 loc) · 5.06 KB
/
BPlusTree.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package BPlusTree;
import java.util.ArrayList;
import java.util.List;
import BPlusTree.Node;
public class BPlusTree {
static int order; //degree of the tree
Node nodeObj ;
static Node rootNode;//to keep track of current root
static List<Node> stack;//we use this stack for traversal, to keep track of parents during split and merge
public BPlusTree() {
//default
order = 2;
stack = new ArrayList<Node>();
nodeObj = new Node();
rootNode = new Node();
}
//initializes order
public boolean initialize(int inputOrder) {
//check if order meets minimum requirements for Bplus tree
if(inputOrder >=2) {
order = inputOrder;
rootNode = new LeafNode(null, null);
return true;
}
else {
return false;
}
}
//insert key value pair
public boolean insert(double keyVal, String dataVal) {
stack = new ArrayList<Node>();
stack.add(0, rootNode);//push root to stack
if(rootNode.isLeafNode) {
if(nodeObj.addToLeafNode(keyVal, dataVal, (LeafNode)rootNode)) {
return true;
}
else {
return false;
}
}
else {
Node sequence=new Node(rootNode);
//keep traversing till leaf node found
while(sequence!=null&&sequence.isLeafNode==false)
{
boolean flagForTraversal=false;
int i;
//find relevant node
for(i=0;i<sequence.keys.size();i++)
{
if(sequence.keys.get(i).keyValue>keyVal)
{
flagForTraversal=true;
sequence=sequence.keys.get(i).left;//greater value found so continue for left child
break;
}
}
//continue for right most child of the node
if(!flagForTraversal)
{
sequence=sequence.keys.get(sequence.keys.size()-1).right;
}
stack.add(0,sequence);
}
//add to leaf node in sorted order
nodeObj.addToLeafNode(keyVal, dataVal, (LeafNode)sequence);
return true;
}
}
//search data for given key
public boolean search(double keyVal) {
LeafNode leafNode;
stack = new ArrayList<Node>();
stack.add(0, rootNode);//push root to stack
if(rootNode.isLeafNode)
{
leafNode = (LeafNode)rootNode;
}
else {//root not leaf node
Node sequence=new Node(rootNode);
//continue till leaf node found
while(sequence!=null&&!sequence.isLeafNode)
{
boolean flag=false;
for(int i=0;i<sequence.keys.size();i++)
{
if(sequence.keys.get(i).keyValue>keyVal)
{
flag=true;
sequence=sequence.keys.get(i).left;
break;
}
}
if(!flag)
{
sequence=sequence.keys.get(sequence.keys.size()-1).right;
}
stack.add(0,sequence);// includes first node as LeafTreeNode, we keep pushing the nodes we traverse into the stack
}
leafNode = (LeafNode)sequence;
}
boolean nodeExists=false;// to check whether value is found or not
for(int i=0;i<leafNode.leafKeys.size();i++)
{
if(leafNode.leafKeys.get(i).keyValue==keyVal)// key found
{
for(int j=0;j<leafNode.leafKeys.get(i).leafData.size();j++)
{
treesearch.output.add((leafNode.leafKeys.get(i).leafData.get(j) +","));
}
//treesearch.output.set(treesearch.output.size()-1, treesearch.output.get(treesearch.output.size()-1).substring(0,treesearch.output.size()-1).length()-1));
nodeExists=true;
break;
}
}
if(!nodeExists)// value not found so print Null
{
treesearch.output.add("Null");
}
treesearch.output.add("NL");//for new line, for new search
return true;
}
//range search
public boolean search(double keyVal1, double keyVal2) {
LeafNode node;
stack = new ArrayList<Node>();
stack.add(0, rootNode);
if(rootNode.isLeafNode) {
node = (LeafNode)rootNode;
}
else {
Node sequence=new Node(rootNode);
//keep traversing till leaf node found
while(sequence!=null&&sequence.isLeafNode==false)
{
boolean flag=false;
int i;
for(i=0;i<sequence.keys.size();i++)
{
if(sequence.keys.get(i).keyValue>keyVal1)
{
flag=true;
sequence=sequence.keys.get(i).left;
break;
}
}
if(!flag)
{
sequence=sequence.keys.get(sequence.keys.size()-1).right;
}
stack.add(sequence);
}
node = (LeafNode)sequence;
}
//booleans to check if both keys present in the tree
boolean k1Exists=false, k2Exists=false;
//breaking conditions
while(!k2Exists && node!=null)
{
for (int i = 0; i < node.leafKeys.size(); i++)
{
if(k1Exists|| node.leafKeys.get(i).keyValue>=keyVal1)
{
if(node.leafKeys.get(i).keyValue>keyVal2)
{
k2Exists = true;
break;
}
//adds all data for a key
for(int j=0; j<node.leafKeys.get(i).getLeafData().size(); j++) {
treesearch.output.add("(" + node.leafKeys.get(i).keyValue + "," + node.leafKeys.get(i).getLeafData().get(j) + "),");
}
k1Exists=true;
}
}
node=node.rightLeafNode;
}
if(k1Exists)// if we had found a key greater than k1
{
//treesearch.output.set(treesearch.output.size()-1, treesearch.output.get(treesearch.output.size()-1).substring(0,treesearch.output.size()-1).length()-1));
}
else
{
treesearch.output.add("Null");
}
treesearch.output.add("NL");//for new search command
return true;
}
}