-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbTree.js
146 lines (130 loc) · 3.4 KB
/
bTree.js
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
function indexOfSorted(array, key) {
let min = 0,
max = array.length - 1;
while (min <= max) {
let mid = min + Math.floor((max - min) / 2);
if (array[mid] === key) {
return mid;
} else if (array[mid] < key) {
min = mid + 1;
} else if (array[mid] > key) {
max = mid - 1;
}
}
return -1;
}
class Node {
constructor(keys=[], children=[]) {
this._keys = keys;
this._children = children;
this.parent = null;
}
keyCount() {
return this._keys.length;
}
children() {
return this._children;
}
includes(key) {
if (indexOfSorted(this._keys, key) !== -1) {
return true;
}
const childIdx = this._childIdxFor(key);
if (childIdx < this._children.length) {
return this._children[childIdx].includes(key);
}
return false;
}
insert(key) {
const childIdx = this._childIdxFor(key);
if (childIdx !== null) {
this._keys.splice(childIdx, 0, key);
}
}
leafFor(key) {
if (this._children.length === 0) {
return this;
}
const childIdx = this._childIdxFor(key);
if (childIdx !== null) {
return this._children[childIdx].leafFor(key);
} else {
return null;
}
}
split() {
const medianIdx = Math.floor(this._keys.length / 2),
leftNode = new Node(this._keys.slice(0, medianIdx), this._children.slice(0, medianIdx + 1)),
rightNode = new Node(this._keys.slice(medianIdx + 1), this._children.slice(medianIdx + 1));
leftNode._children.forEach(c => c.parent = leftNode);
rightNode._children.forEach(c => c.parent = rightNode);
return [this._keys[medianIdx], leftNode, rightNode];
}
insertChildBefore(key, node) {
this._children[indexOfSorted(this._keys, key)] = node;
node.parent = this;
}
insertChildAfter(key, node) {
const idx = indexOfSorted(this._keys, key);
this._children.splice(idx+1, 0, node);
node.parent = this;
}
_childIdxFor(key) {
let min = 0,
max = this._keys.length - 1;
while (min <= max) {
const idx = min + Math.floor((max - min) / 2);
if (this._keys[idx] === key) {
return null;
} else if (this._keys[idx] > key) {
if (idx === 0 || this._keys[idx-1] < key) {
return idx;
} else {
max = idx - 1;
}
} else if (this._keys[idx] < key) {
if (idx === this._keys.length - 1) {
return this._keys.length;
} else {
min = idx + 1;
}
}
}
return this._keys.length;
}
}
export default class BTree {
// order = max # of children
constructor(order) {
this.order = order;
this.root = new Node([]);
}
insert(key) {
const leaf = this.root.leafFor(key);
if (leaf === null) {
return;
}
leaf.insert(key);
this._splitUp(leaf);
}
includes(key) {
return this.root.includes(key);
}
toString() {
return JSON.stringify(this.root, ["_keys", "_children"], " ");
}
_splitUp(node) {
while (node !== null && node.keyCount() >= this.order) {
const [splitKey, leftNode, rightNode] = node.split();
if (node.parent === null) {
node.parent = new Node([]);
this.root.parent = node.parent;
this.root = node.parent;
}
node.parent.insert(splitKey);
node.parent.insertChildBefore(splitKey, leftNode);
node.parent.insertChildAfter(splitKey, rightNode);
node = node.parent;
}
}
}