Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitefeda27

Browse files
committed
Add binarySearch function.
1 parent51b9215 commitefeda27

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-
functionNode(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-
varNil=newNode(null,null,null,null,0);
68-
Nil.parent=Nil;
69-
Nil.left=Nil;
70-
Nil.right=Nil;
71-
exports.Nil=Nil;
7234

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

8951
functionLeftRotate(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+
varSBTree=function(){};
223183

224-
exports.SBTree.prototype={
184+
SBTree.prototype={
185+
_root:Nil,
186+
updateChild:updateChild,
225187
getsize(){
226188
returnthis._root.size;
227189
},
190+
191+
getroot(){
192+
returnthis._root;
193+
},
194+
195+
binarySearch:function(cmp,value){
196+
varleft=-1;
197+
varright=this.size;
198+
while(left+1<right){
199+
varmiddle=(left+right)>>1;// jshint ignore:line
200+
varresult=cmp(this.get(middle).value,value);
201+
if(result<=0){
202+
left=middle;
203+
}else{
204+
right=middle;
205+
}
206+
}
207+
returnleft+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
varnode=findRightMost(this._root);
240221
varnewNode=newNode(value,node,Nil,Nil,1);
241222
if(node!==Nil){
@@ -245,14 +226,14 @@
245226
returnnewNode;
246227
};
247228

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

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

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

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

327+
returnSBTree;
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+
varNodeConstructor=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+
varupdateSize=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+
varcreateNil=function(Node,value){
364+
varNil=newNode(value,null,null,null,0);
365+
Nil.parent=Nil;
366+
Nil.left=Nil;
367+
Nil.right=Nil;
368+
returnNil;
369+
};
370+
371+
varNode=function(){
372+
NodeConstructor.apply(this,arguments);
373+
};
374+
375+
Node.prototype.updateSize=updateSize;
376+
377+
varNil=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
})(typeofmodule==='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+
varsTree=newSBTree();
169+
for(vari=0;i<10000;++i){
170+
sTree.push(i);
171+
}
172+
varcmp=function(a,b){
173+
returna-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)

[8]ページ先頭

©2009-2025 Movatter.jp