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

Skip to content

Commit efeda27

Browse files
committed
Add binarySearch function.
1 parent 51b9215 commit efeda27

File tree

2 files changed

+110
-52
lines changed

2 files changed

+110
-52
lines changed

src/data-structures/size-balanced-tree.js

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
/**
24
* Size balanced tree is a data structure which is
35
* a type of self-balancing binary search tree that use
@@ -29,47 +31,8 @@
2931
*
3032
* @module data-structures/size-balanced-tree
3133
*/
32-
(function (exports) {
33-
'use strict';
34-
35-
/**
36-
* Node of the Size-Balanced tree.
37-
*
38-
* @private
39-
* @constructor
40-
* @param {Object} value Value assigned to the node.
41-
* @param {Node} parent Parent node.
42-
* @param {Node} left Left node.
43-
* @param {Node} right Right node.
44-
* @param {Number} size Node's, means the Node count of this subtree.
45-
*/
46-
function Node(value, parent, left, right, size) {
47-
this.value = value;
48-
this.parent = parent;
49-
this.left = left;
50-
this.right = right;
51-
this.size = size;
52-
this.height = 0;
53-
}
54-
55-
/**
56-
* Update node's size.
57-
*
58-
* @private
59-
* @method
60-
*/
61-
Node.prototype.updateSize = function () {
62-
this.size = this.left.size + this.right.size + 1;
63-
this.height = Math.max(this.left.height, this.right.height) + 1;
64-
};
65-
66-
exports.Node = Node;
67-
var Nil = new Node(null, null, null, null, 0);
68-
Nil.parent = Nil;
69-
Nil.left = Nil;
70-
Nil.right = Nil;
71-
exports.Nil = Nil;
7234

35+
function CreateSBTreeClass (Node, Nil) {
7336
function updateChild(node, newChild) {
7437
var parent = node.parent;
7538
if (parent !== Nil) {
@@ -84,7 +47,6 @@
8447
newChild.parent = parent;
8548
}
8649
}
87-
exports.updateChild = updateChild;
8850

8951
function LeftRotate(node, childNode) {
9052
/*
@@ -212,30 +174,49 @@
212174
}
213175

214176
/**
215-
* Red-Black Tree.
177+
* Size Balanced Tree.
216178
*
217179
* @public
218180
* @constructor
219181
*/
220-
exports.SBTree = function () {
221-
this._root = Nil;
222-
};
182+
var SBTree = function () {};
223183

224-
exports.SBTree.prototype = {
184+
SBTree.prototype = {
185+
_root: Nil,
186+
updateChild: updateChild,
225187
get size() {
226188
return this._root.size;
227189
},
190+
191+
get root() {
192+
return this._root;
193+
},
194+
195+
binarySearch: function (cmp, value) {
196+
var left = -1;
197+
var right = this.size;
198+
while (left + 1 < right) {
199+
var middle = (left + right) >> 1; // jshint ignore:line
200+
var result = cmp(this.get(middle).value, value);
201+
if (result <= 0) {
202+
left = middle;
203+
} else {
204+
right = middle;
205+
}
206+
}
207+
return left + 1;
208+
},
228209
};
229210

230211
/**
231-
* Push a value to the end of tree.<br><br>
212+
* Push a value to the end of tree.
232213
* Complexity: O(log N).
233214
*
234215
* @public
235216
* @method
236217
* @param {Object} value Value.
237218
*/
238-
exports.SBTree.prototype.push = function (value) {
219+
SBTree.prototype.push = function (value) {
239220
var node = findRightMost(this._root);
240221
var newNode = new Node(value, node, Nil, Nil, 1);
241222
if (node !== Nil) {
@@ -245,14 +226,14 @@
245226
return newNode;
246227
};
247228

248-
exports.SBTree.prototype.get = function (pos) {
229+
SBTree.prototype.get = function (pos) {
249230
if (pos >= this._root.size) {
250231
return Nil;
251232
}
252233
return findNodeAtPos(this._root, pos);
253234
};
254235

255-
exports.SBTree.prototype.getIndex = function (node) {
236+
SBTree.prototype.getIndex = function (node) {
256237
var index = node.left.size;
257238
while (node !== this._root) {
258239
var parent = node.parent;
@@ -264,7 +245,7 @@
264245
return index;
265246
};
266247

267-
exports.SBTree.prototype.insert = function (pos, value) {
248+
SBTree.prototype.insert = function (pos, value) {
268249
if (pos >= this._root.size) {
269250
return this.push(value);
270251
}
@@ -282,7 +263,7 @@
282263
return newNode;
283264
};
284265

285-
exports.SBTree.prototype.remove = function (pos) {
266+
SBTree.prototype.remove = function (pos) {
286267
if (pos >= this._root.size) {
287268
return Nil; // There is no element to remove
288269
}
@@ -343,4 +324,66 @@
343324
return node;
344325
};
345326

327+
return SBTree;
328+
}
329+
330+
(function (exports) {
331+
332+
/**
333+
* Node constructor of the Size-Balanced tree.
334+
*
335+
* @private
336+
* @constructor
337+
* @param {Object} value Value assigned to the node.
338+
* @param {Node} parent Parent node.
339+
* @param {Node} left Left node.
340+
* @param {Node} right Right node.
341+
* @param {Number} size Node's, means the Node count of this .
342+
*/
343+
var NodeConstructor = function (value, parent, left, right, size) {
344+
this.value = value;
345+
this.parent = parent;
346+
this.left = left;
347+
this.right = right;
348+
this.size = size;
349+
this.height = 0;
350+
};
351+
352+
/**
353+
* Update node's size.
354+
*
355+
* @private
356+
* @method
357+
*/
358+
var updateSize = function () {
359+
this.size = this.left.size + this.right.size + 1;
360+
this.height = Math.max(this.left.height, this.right.height) + 1;
361+
};
362+
363+
var createNil = function (Node, value) {
364+
var Nil = new Node(value, null, null, null, 0);
365+
Nil.parent = Nil;
366+
Nil.left = Nil;
367+
Nil.right = Nil;
368+
return Nil;
369+
};
370+
371+
var Node = function () {
372+
NodeConstructor.apply(this, arguments);
373+
};
374+
375+
Node.prototype.updateSize = updateSize;
376+
377+
var Nil = createNil(Node, null);
378+
379+
exports.NodeConstructor = NodeConstructor;
380+
exports.createNil = createNil;
381+
exports.updateSize = updateSize;
382+
exports.CreateSBTreeClass = CreateSBTreeClass;
383+
384+
exports.Node = Node;
385+
exports.Nil = Nil;
386+
exports.SBTree = CreateSBTreeClass(Node, Nil);
387+
exports.updateChild = exports.SBTree.prototype.updateChild;
388+
346389
})(typeof module === 'undefined' ? window : module.exports);

test/data-structures/size-balanced-tree.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,19 @@ describe('SBTree', function () {
163163
expect(sTree.getIndex(item)).toBe(i);
164164
}
165165
});
166+
167+
it('test binary search', function () {
168+
var sTree = new SBTree();
169+
for (var i = 0; i < 10000; ++i) {
170+
sTree.push(i);
171+
}
172+
var cmp = function (a, b) {
173+
return a - b;
174+
}
175+
expect(sTree.binarySearch(cmp, 10.5)).toBe(11)
176+
expect(sTree.binarySearch(cmp, 0)).toBe(1)
177+
expect(sTree.binarySearch(cmp, -1)).toBe(0)
178+
expect(sTree.binarySearch(cmp, 9999)).toBe(10000)
179+
expect(sTree.binarySearch(cmp, 10000)).toBe(10000)
180+
});
166181
});

0 commit comments

Comments
 (0)