Skip to content

Commit fedf84d

Browse files
committed
Added BTreeNode find method
1 parent 54432b4 commit fedf84d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/data-structures/tree/b-tree/BTreeNode.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import Comparator from '../../../utils/comparator/Comparator';
2+
13
export default class BTreeNode {
24
constructor(degree = 0, isLeaf = true) {
35
this.degree = degree;
46
this.isLeaf = isLeaf;
57
this.keys = new Array((2 * this.degree) - 1);
68
this.children = new Array(2 * this.degree);
79
this.numKeys = 0;
10+
this.nodeComparator = new Comparator();
811
}
912

1013
traversal() {
@@ -21,4 +24,18 @@ export default class BTreeNode {
2124
}
2225
return traverse;
2326
}
27+
28+
find(value) {
29+
let i = 0;
30+
while (i < this.numKeys && value > this.keys[i]) {
31+
i += 1;
32+
}
33+
if (this.nodeComparator.equal(value, this.keys[i])) {
34+
return this;
35+
}
36+
if (this.isLeaf) {
37+
return null;
38+
}
39+
return this.children[i].find(value);
40+
}
2441
}

0 commit comments

Comments
 (0)