|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * Size balanced tree is a data structure which is
|
3 | 5 | * a type of self-balancing binary search tree that use
|
|
29 | 31 | *
|
30 | 32 | *@module data-structures/size-balanced-tree
|
31 | 33 | */
|
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; |
72 | 34 |
|
| 35 | +functionCreateSBTreeClass(Node,Nil){ |
73 | 36 | functionupdateChild(node,newChild){
|
74 | 37 | varparent=node.parent;
|
75 | 38 | if(parent!==Nil){
|
|
84 | 47 | newChild.parent=parent;
|
85 | 48 | }
|
86 | 49 | }
|
87 |
| -exports.updateChild=updateChild; |
88 | 50 |
|
89 | 51 | functionLeftRotate(node,childNode){
|
90 | 52 | /*
|
|
212 | 174 | }
|
213 | 175 |
|
214 | 176 | /**
|
215 |
| - *Red-Black Tree. |
| 177 | + *Size Balanced Tree. |
216 | 178 | *
|
217 | 179 | *@public
|
218 | 180 | *@constructor
|
219 | 181 | */
|
220 |
| -exports.SBTree=function(){ |
221 |
| -this._root=Nil; |
222 |
| -}; |
| 182 | +varSBTree=function(){}; |
223 | 183 |
|
224 |
| -exports.SBTree.prototype={ |
| 184 | +SBTree.prototype={ |
| 185 | +_root:Nil, |
| 186 | +updateChild:updateChild, |
225 | 187 | getsize(){
|
226 | 188 | returnthis._root.size;
|
227 | 189 | },
|
| 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 | +}, |
228 | 209 | };
|
229 | 210 |
|
230 | 211 | /**
|
231 |
| - * Push a value to the end of tree.<br><br> |
| 212 | + * Push a value to the end of tree. |
232 | 213 | * Complexity: O(log N).
|
233 | 214 | *
|
234 | 215 | *@public
|
235 | 216 | *@method
|
236 | 217 | *@param {Object} value Value.
|
237 | 218 | */
|
238 |
| -exports.SBTree.prototype.push=function(value){ |
| 219 | +SBTree.prototype.push=function(value){ |
239 | 220 | varnode=findRightMost(this._root);
|
240 | 221 | varnewNode=newNode(value,node,Nil,Nil,1);
|
241 | 222 | if(node!==Nil){
|
|
245 | 226 | returnnewNode;
|
246 | 227 | };
|
247 | 228 |
|
248 |
| -exports.SBTree.prototype.get=function(pos){ |
| 229 | +SBTree.prototype.get=function(pos){ |
249 | 230 | if(pos>=this._root.size){
|
250 | 231 | returnNil;
|
251 | 232 | }
|
252 | 233 | returnfindNodeAtPos(this._root,pos);
|
253 | 234 | };
|
254 | 235 |
|
255 |
| -exports.SBTree.prototype.getIndex=function(node){ |
| 236 | +SBTree.prototype.getIndex=function(node){ |
256 | 237 | varindex=node.left.size;
|
257 | 238 | while(node!==this._root){
|
258 | 239 | varparent=node.parent;
|
|
264 | 245 | returnindex;
|
265 | 246 | };
|
266 | 247 |
|
267 |
| -exports.SBTree.prototype.insert=function(pos,value){ |
| 248 | +SBTree.prototype.insert=function(pos,value){ |
268 | 249 | if(pos>=this._root.size){
|
269 | 250 | returnthis.push(value);
|
270 | 251 | }
|
|
282 | 263 | returnnewNode;
|
283 | 264 | };
|
284 | 265 |
|
285 |
| -exports.SBTree.prototype.remove=function(pos){ |
| 266 | +SBTree.prototype.remove=function(pos){ |
286 | 267 | if(pos>=this._root.size){
|
287 | 268 | returnNil;// There is no element to remove
|
288 | 269 | }
|
|
343 | 324 | returnnode;
|
344 | 325 | };
|
345 | 326 |
|
| 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 | + |
346 | 389 | })(typeofmodule==='undefined' ?window :module.exports);
|