You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Search the item relatives to the key that satisfy the condition represented by the callback function.
121
121
* @param key {Number} The key to find.
122
122
* @param [node = root] {RBNode} The node from which start the search.
123
+
* @param [callback = function(node,index){return(node.keys[index]===key);}] The condition to satisfy. The callback must accept the current node to check and optionally the position of the key.
123
124
* @return {*} The item found or undefined if there isn't the key in the tree.
@@ -134,7 +138,7 @@ BTree.prototype.search = function (key, node) {
134
138
else
135
139
i=m+1;
136
140
}
137
-
if(i<n&&key===node.keys[i])
141
+
if(i<n&&callback(node,i))
138
142
returnnode.items[i];
139
143
elseif(!node.childs.length)
140
144
returnundefined;
@@ -354,10 +358,24 @@ BTree.prototype.augmentChild = function (node, index) {
354
358
/**
355
359
* Checks if the tree contains the key.
356
360
* @param key {number} The key to find.
361
+
* @param [callback = function(node,index){return(node.keys[index]===key);}] The condition to satisfy. The callback must accept the current node to check and optionally the position of the key.
357
362
* @return {boolean} True if the tree contains the key.
358
363
*/
359
-
BTree.prototype.contains=function(key){
360
-
returnthis.search(key)!==undefined;
364
+
BTree.prototype.contains=function(key,callback){
365
+
returnthis.search(key,null,callback)!==undefined;
366
+
};
367
+
368
+
/**
369
+
* Checks if the tree contains a node that satisfy the condition represented by the callback function.
370
+
* This method check all the tree avoiding the binary search.
371
+
* @param callback {function} The condition to satisfy. The callback must accept the current node to check.
372
+
* @return {boolean} True if the tree contains the node that satisfy the condition, false otherwise.
373
+
*/
374
+
BTree.prototype.fullContains=function(callback){
375
+
varkey=this.minimumKey();
376
+
while(key!==null&&!callback(this.search(key)))
377
+
key=this.successor(key);
378
+
returnkey!==null;
361
379
};
362
380
363
381
/**
@@ -439,18 +457,22 @@ BTree.prototype.minimumKey = function () {
439
457
varnode=this.root;
440
458
while(node.childs.length)
441
459
node=node.childs[0];
442
-
returnnode.keys[0];
460
+
if(node)
461
+
returnnode.keys[0];
462
+
returnnull;
443
463
};
444
464
445
465
/**
446
466
* Gets the maximum key stored in the tree.
447
-
* @return {node} The key found.
467
+
* @return {number} The key found.
448
468
*/
449
469
BTree.prototype.maximumKey=function(){
450
470
varnode=this.root;
451
471
while(node.childs.length)
452
472
node=node.childs[node.childs.length-1];
453
-
returnnode.keys[node.keys-1];
473
+
if(node)
474
+
returnnode.keys[node.keys.length-1];
475
+
returnnull;
454
476
};
455
477
456
478
/**
@@ -472,7 +494,7 @@ BTree.prototype.maximum = function () {
472
494
varnode=this.root;
473
495
while(node.childs.length)
474
496
node=node.childs[node.childs.length-1];
475
-
returnnode.items[node.items-1];
497
+
returnnode.items[node.items.length-1];
476
498
};
477
499
478
500
/**
@@ -522,11 +544,14 @@ BTree.prototype.clear = function () {
0 commit comments