Skip to content

Commit 1851987

Browse files
committed
rebalance in a separate method
1 parent 251f0bf commit 1851987

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

bbtree.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ bbtree.prototype = {
3535
insert: function (key) {
3636

3737
var bottom = this._bottom,
38-
skew = this._skew,
39-
split = this._split,
4038
compare = this._compare,
4139
newNode = new Node(key, bottom, bottom, 1);
4240

@@ -64,8 +62,19 @@ bbtree.prototype = {
6462
}
6563
}
6664

67-
for (var i = path.length - 1, current, parent, updated; i >= 0; i--) {
68-
current = path[i];
65+
this._rebalance(path);
66+
67+
return this;
68+
},
69+
70+
_rebalance: function (path) {
71+
72+
var skew = this._skew,
73+
split = this._split,
74+
node, current, parent, updated;
75+
76+
for (var i = path.length - 1; i >= 0; i--) {
77+
node = current = path[i];
6978
updated = false;
7079

7180
if (current.level === current.left.level && current.level === current.right.level) {
@@ -78,19 +87,17 @@ bbtree.prototype = {
7887
}
7988

8089
if (node !== current) {
90+
updated = true;
8191
if (i) {
8292
parent = path[i - 1];
8393
if (parent.left === current) parent.left = node;
8494
else parent.right = node;
8595

8696
} else this.root = node;
87-
updated = true;
8897
}
8998

9099
if (!updated) break;
91100
}
92-
93-
return this;
94101
},
95102

96103
_skew: function (node) {

0 commit comments

Comments
 (0)