@@ -316,7 +316,7 @@ var BTree = function () {
316316 * @example
317317 * var tree = new BTree(10);
318318 * tree.insert(20);
319- * tree.toArray(); // [{value:10,...},{value:20,...}]
319+ * tree.toArray(); // => [{value:10,...},{value:20,...}]
320320 */
321321
322322 } , {
@@ -329,6 +329,28 @@ var BTree = function () {
329329 return arr ;
330330 }
331331
332+ /**
333+ * Returns array of values of the tree.
334+ * @method toFlatArray
335+ * @member
336+ * @public
337+ * @returns {Array<any> } Returns array form of given tree.
338+ * @example
339+ * var tree = new BTree(10);
340+ * tree.insert(20);
341+ * tree.toFlatArray(); // => [10,20]
342+ */
343+
344+ } , {
345+ key : "toFlatArray" ,
346+ value : function toFlatArray ( ) {
347+ var arr = [ ] ;
348+ this . each ( function ( node , index ) {
349+ arr . push ( node . value ) ;
350+ } ) ;
351+ return arr ;
352+ }
353+
332354 /**
333355 * Inserts the given value to the tree where first free left child node is found.
334356 * @param {any } val any type of value to be added to tree node.
@@ -503,15 +525,15 @@ var BTree = function () {
503525 /**
504526 * Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
505527 * @param {Function } callback A callback function for execution of each node.
506- * @method findBFS
528+ * @method traverseBFS
507529 * @member
508530 * @public
509531 * @returns {undefined } no value.
510532 */
511533
512534 } , {
513- key : "findBFS " ,
514- value : function findBFS ( callback ) {
535+ key : "traverseBFS " ,
536+ value : function traverseBFS ( callback ) {
515537 var currCount = 0 ;
516538 var children = [ ] ;
517539
@@ -544,15 +566,15 @@ var BTree = function () {
544566 /**
545567 * Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
546568 * @param {Function } callback A callback function for execution of each node.
547- * @method find
569+ * @method traverseDFS
548570 * @member
549571 * @public
550572 * @returns {undefined } no value.
551573 */
552574
553575 } , {
554- key : "find " ,
555- value : function find ( callback ) {
576+ key : "traverseDFS " ,
577+ value : function traverseDFS ( callback ) {
556578 /**
557579 *
558580 * @param {BTreeNode } currNode Currently processing node.
@@ -590,7 +612,7 @@ var BTree = function () {
590612 } , {
591613 key : "each" ,
592614 value : function each ( callback ) {
593- return this . findBFS ( callback ) ;
615+ return this . traverseBFS ( callback ) ;
594616 }
595617
596618 /**
@@ -605,7 +627,7 @@ var BTree = function () {
605627 } , {
606628 key : "forEach" ,
607629 value : function forEach ( callback ) {
608- return this . findBFS ( callback ) ;
630+ return this . traverseBFS ( callback ) ;
609631 }
610632
611633 /**
@@ -828,8 +850,8 @@ var BTree = function () {
828850 * @returns {boolean } Returns true if it is present, otherwise false.
829851 * @example
830852 * var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
831- * tree.includes (30); // true
832- * tree.includes (51); // false
853+ * tree.exists (30); // true
854+ * tree.exists (51); // false
833855 */
834856
835857 } , {
@@ -847,8 +869,8 @@ var BTree = function () {
847869 * @returns {boolean } Returns true if it is present, otherwise false.
848870 * @example
849871 * var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
850- * tree.includes (30); // true
851- * tree.includes (51); // false
872+ * tree.has (30); // true
873+ * tree.has (51); // false
852874 */
853875
854876 } , {
@@ -857,6 +879,139 @@ var BTree = function () {
857879 return this . indexOf ( value ) !== - 1 ;
858880 }
859881
882+ /**
883+ * Sorts the tree based on compare function, Has option to sort only at children level.
884+ * @param {Function } compareFnc Function used to determine the order of the elements. It is expected to return
885+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
886+ * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
887+ * ```ts
888+ * (a, b) => a - b)
889+ * ```
890+ * @param {boolean } atOnlyFirstChildLevel Optiona, Flag to specify if first child of each node should sorted. Default is `false`.
891+ * @method sort
892+ * @member
893+ * @public
894+ * @returns {undefined } Returns undefined.
895+ * @example
896+ * var tree = BTree.fromArray([10, 200, 100, 50, 60, 90, 5, 3]);
897+ * tree.sort().toFlatArray(); // => [3,5,10,50,60,90,100,200]
898+ */
899+
900+ } , {
901+ key : "sort" ,
902+ value : function sort ( ) {
903+ var compareFnc = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : function ( ) {
904+ var a = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : 0 ;
905+ var b = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : 0 ;
906+ return a < b ? - 1 : a == b ? 0 : 1 ;
907+ } ;
908+ var atOnlyFirstChildLevel = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : false ;
909+
910+ if ( atOnlyFirstChildLevel ) {
911+ var DFS = function DFS ( node ) {
912+ if ( node !== null ) {
913+ var out = compareFnc ( node . lNode , node . rNode ) ;
914+ if ( out > 0 ) {
915+ var temp = node . lNode ;
916+ node . lNode = node . rNode ;
917+ node . rNode = temp ;
918+ }
919+ DFS ( node . lNode ) ;
920+ DFS ( node . rNode ) ;
921+ }
922+ } ;
923+ DFS ( this . root ) ;
924+ } else {
925+ var arr = [ ] ;
926+ var arrBFS = [ ] ;
927+ var counter = 0 ;
928+ var children = [ ] ;
929+ var BFS = function BFS ( node ) {
930+
931+ if ( node !== null && node . lNode !== null ) {
932+ children . push ( node . lNode ) ;
933+ }
934+ if ( node !== null && node . rNode !== null ) {
935+ children . push ( node . rNode ) ;
936+ }
937+ if ( node !== null ) {
938+ arrBFS . push ( node ) ;
939+ arr . push ( node . value ) ;
940+ }
941+
942+ if ( children . length !== 0 ) {
943+ var first = children [ 0 ] ;
944+ children . splice ( 0 , 1 ) ;
945+ BFS ( first ) ;
946+ }
947+ } ;
948+ BFS ( this . root ) ;
949+
950+ while ( arr . length !== 0 ) {
951+ var min = arr [ 0 ] ;
952+ var minIndex = 0 ;
953+ for ( var i = 1 ; i < arr . length ; i ++ ) {
954+ var out = compareFnc ( min , arr [ i ] ) ;
955+ if ( out > 0 ) {
956+ min = arr [ i ] ;
957+ minIndex = i ;
958+ }
959+ }
960+ arrBFS [ counter ] . value = min ;
961+ arr . splice ( minIndex , 1 ) ;
962+ counter ++ ;
963+ }
964+ }
965+ }
966+
967+ /**
968+ * Prints entire tree on the console, useful for logging and checking status.
969+ * @method print
970+ * @member
971+ * @public
972+ * @returns {undefined } Returns undefined.
973+ * @example
974+ * var tree = BTree.fromArray([1, 2, 3]);
975+ * tree.print();
976+ * 1 (1)
977+ * |- 2 (2)
978+ * |- 3 (3)
979+ */
980+
981+ } , {
982+ key : "print" ,
983+ value : function print ( ) {
984+ var isit = false ;
985+ this . traverseDFS ( function ( node , index ) {
986+ var len = BTree . getPathFromIndex ( index ) . length ;
987+ var isFirst = isit ? " |-" . repeat ( len - 1 ) : "" ;
988+ console . log ( isFirst , node . value , "(" + index + ")" ) ;
989+ isit = true ;
990+ } ) ;
991+ }
992+
993+ /**
994+ * Returns the first matched tree node. Traverses using BFS.
995+ * @param {any } item any value to find inside the tree.
996+ * @method find
997+ * @member
998+ * @public
999+ * @returns {BTreeNode } Returns the first matched tree node, if not found, returns null.
1000+ * @example
1001+ */
1002+
1003+ } , {
1004+ key : "find" ,
1005+ value : function find ( item ) {
1006+ var retNode = null ;
1007+ this . each ( function ( node , index ) {
1008+ if ( node . value === item && retNode === null ) {
1009+ retNode = node ;
1010+ }
1011+ } ) ;
1012+ return retNode ;
1013+ }
1014+
8601015 /**
8611016 * Returns index value from given path.
8621017 * @param {Array<'U'|'L'|'R'> } path Array for U L or R, which represents the quickest path to get to a node.
0 commit comments