Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8530563

Browse files
committed
Fix the insert operation
1 parent 05ca413 commit 8530563

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/data-structures/interval-tree.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
var child = new Node(interval[0], interval[1]);
2020
child.parentNode = node;
2121
node[side] = child;
22-
if (side === 'right' && node.max < interval[1]) {
22+
if (node.max < interval[1]) {
2323
while (child) {
2424
if (child.max < interval[1]) {
2525
child.max = interval[1];
@@ -167,7 +167,10 @@
167167
}
168168
} else {
169169
this.root = node[side];
170-
this.root.parentNode = null;
170+
// last node removed
171+
if (this.root) {
172+
this.root.parentNode = null;
173+
}
171174
}
172175
}
173176
// Adjust the max value
@@ -208,25 +211,26 @@ var t = new IntervalTree();
208211

209212
t.add([1, 2]);
210213
t.add([-1, 8]);
211-
t.add([-1, 18]);
212-
t.add([2, 4]);
213-
t.add([8, 13]);
214-
t.add([2, 10]);
215-
t.add([-2, 10]);
216-
t.add([-4, 15]);
217-
t.add([-6, 15]);
218-
219-
t.remove([1, 2]);
220-
t.remove([-1, 8]);
221-
t.remove([-1, 18]);
222-
t.remove([2, 4]);
223-
t.remove([8, 13]);
224-
t.remove([2, 10]);
225-
t.remove([-2, 10]);
226-
t.remove([-4, 15]);
227-
214+
// t.add([-1, 18]);
215+
// t.add([2, 4]);
216+
// t.add([8, 13]);
217+
// t.add([2, 10]);
218+
// t.add([-2, 10]);
219+
// t.add([-4, 15]);
220+
// t.add([-6, 15]);
221+
222+
// t.remove([1, 2]);
223+
// t.remove([-1, 8]);
224+
// t.remove([-1, 18]);
225+
// t.remove([2, 4]);
226+
// t.remove([8, 13]);
227+
// t.remove([2, 10]);
228+
// t.remove([-2, 10]);
229+
// t.remove([-4, 15]);
230+
// t.remove([-6, 15]);
231+
232+
console.log(t.height());
228233
console.log(t.root);
229-
console.log(t.intersects([17, 29]));
230234

231235
//console.log(t.intersects([19, 29]));
232236
//console.log(t.contains(16));

0 commit comments

Comments
 (0)