Skip to content

Commit b77908f

Browse files
Add Array functions
1 parent 355132c commit b77908f

File tree

10 files changed

+728
-321
lines changed

10 files changed

+728
-321
lines changed

DOCUMENTATION.md

Lines changed: 164 additions & 127 deletions
Large diffs are not rendered by default.

dist/btreenode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var bTreeNodeStruct = {};
1414

1515
/**
1616
* Binary Tree node class, contains 2 child nodes and single value.
17-
* @class
17+
* @class BTreeNode
1818
* @public
1919
* @example
2020
* var node = new BTreeNode({ value: 15 });
@@ -36,7 +36,7 @@ var BTreeNode = function () {
3636
/**
3737
* @property value
3838
* Contains actual value
39-
* @type {any}
39+
* @type {T}
4040
* @public
4141
*/
4242
this.value = attr.value || null;

dist/dsinjs-binarytree.js

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var bTreeNodeStruct = {};
1515

1616
/**
1717
* Binary Tree node class, contains 2 child nodes and single value.
18-
* @class
18+
* @class BTreeNode
1919
* @public
2020
* @example
2121
* var node = new BTreeNode({ value: 15 });
@@ -37,7 +37,7 @@ var BTreeNode = function () {
3737
/**
3838
* @property value
3939
* Contains actual value
40-
* @type {any}
40+
* @type {T}
4141
* @public
4242
*/
4343
this.value = attr.value || null;
@@ -210,6 +210,21 @@ var UnreachableError = function (_Error) {
210210
return UnreachableError;
211211
}(Error);
212212

213+
var FilteredRootError = function (_Error2) {
214+
_inherits(FilteredRootError, _Error2);
215+
216+
function FilteredRootError(msg) {
217+
_classCallCheck(this, FilteredRootError);
218+
219+
var _this2 = _possibleConstructorReturn(this, (FilteredRootError.__proto__ || Object.getPrototypeOf(FilteredRootError)).call(this, msg));
220+
221+
_this2.name = "FilteredRootError";
222+
return _this2;
223+
}
224+
225+
return FilteredRootError;
226+
}(Error);
227+
213228
/**
214229
* BTree main class
215230
* @class
@@ -491,7 +506,7 @@ var BTree = function () {
491506
* @method findBFS
492507
* @member
493508
* @public
494-
* @returns {undefined}
509+
* @returns {undefined} no value.
495510
*/
496511

497512
}, {
@@ -503,6 +518,7 @@ var BTree = function () {
503518
/**
504519
*
505520
* @param {BTreeNode} currNode current node in recursion.
521+
* @private
506522
*/
507523
var recInser = function recInser(currNode, currPath) {
508524
if (currNode != null) {
@@ -531,7 +547,7 @@ var BTree = function () {
531547
* @method find
532548
* @member
533549
* @public
534-
* @returns {undefined}
550+
* @returns {undefined} no value.
535551
*/
536552

537553
}, {
@@ -541,6 +557,7 @@ var BTree = function () {
541557
*
542558
* @param {BTreeNode} currNode Currently processing node.
543559
* @param {Array<'U'|'L'|'R'>} path current path
560+
* @private
544561
*/
545562
var recFnc = function recFnc(currNode, path) {
546563
if (currNode !== null) {
@@ -562,33 +579,33 @@ var BTree = function () {
562579
}
563580

564581
/**
565-
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
582+
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
566583
* @param {Function} callback A callback function for execution of each node.
567584
* @method each
568585
* @member
569586
* @public
570-
* @returns {undefined}
587+
* @returns {undefined} no value.
571588
*/
572589

573590
}, {
574591
key: "each",
575592
value: function each(callback) {
576-
return this.find(callback);
593+
return this.findBFS(callback);
577594
}
578595

579596
/**
580-
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
597+
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
581598
* @param {Function} callback A callback function for execution of each node.
582-
* @method each
599+
* @method forEach
583600
* @member
584601
* @public
585-
* @returns {undefined}
602+
* @returns {undefined} no value.
586603
*/
587604

588605
}, {
589606
key: "forEach",
590607
value: function forEach(callback) {
591-
return this.find(callback);
608+
return this.findBFS(callback);
592609
}
593610

594611
/**
@@ -611,6 +628,7 @@ var BTree = function () {
611628
return {
612629
/**
613630
* @returns { {value: BTreeNode, done: boolean} }
631+
* @private
614632
*/
615633
next: function next() {
616634
curr++;
@@ -643,6 +661,7 @@ var BTree = function () {
643661

644662
/**
645663
* Maps current tree values to a new tree with modifying the values using given callback function.
664+
* Uses BFS.
646665
* @param {Function} callback callback function for value modifier.
647666
* @method map
648667
* @member
@@ -651,7 +670,7 @@ var BTree = function () {
651670
* @example
652671
* var tree = BTree.fromArray([10, 20, 30, 40]);
653672
* var tree2 = tree.map(n => n * 2);
654-
* var arr2 = tree2.toArray(); // [{value:20,...},{value:40,...},{value:80,...},{value:60,...}]
673+
* var arr2 = tree2.toArray(); // [{value:20,...},{value:40,...},{value:60,...},{value:80,...}]
655674
*/
656675

657676
}, {
@@ -667,10 +686,93 @@ var BTree = function () {
667686
return newTree;
668687
}
669688

670-
/* filter(callback) {
689+
/**
690+
* Filters each item based on given filter function
691+
* @param {Function} filterFnc callback function for filtering purpose.
692+
* @method filter
693+
* @member
694+
* @public
695+
* @throws FilteredRootError, Error when root node gets filtered out.
696+
* @returns {BTree} New filtered instance of tree.
697+
* @example
698+
* var tree = BTree.fromArray([10, 20, 30, 40]);
699+
* var tree2 = tree.filter(n => !!(n % 4 === 0 || n === 10));
700+
* var arr2 = tree2.toArray(); // [{value:10,...},{value:20,...},{value:40,...}]
701+
*/
702+
703+
}, {
704+
key: "filter",
705+
value: function filter(filterFnc) {
706+
if (!filterFnc(this.root.value)) {
707+
throw new FilteredRootError("Root node cannot be filtered. If you want to filter out root node, you can use emptry BTree instance.");
671708
}
672-
reduce() {
673-
} */
709+
var newTree = new BTree(this.root.value);
710+
this.each(function (node, index) {
711+
if (index !== 1) {
712+
var canBeInserted = filterFnc(node.value);
713+
if (canBeInserted) {
714+
newTree.insertAt(node.value, index);
715+
}
716+
}
717+
});
718+
return newTree;
719+
}
720+
721+
/**
722+
* Reduces each node values using reduceFunction and returns final value.
723+
* @param {Function} reduceFunction callback function for reducing each node value to a final value.
724+
* @param {number|any} initialValue Optional, Accumulator/Initial value.
725+
* @method reduce
726+
* @member
727+
* @public
728+
* @returns {any} Returns reduceed value
729+
* @example
730+
* var tree = BTree.fromArray([10, 20, 30, 40]);
731+
* var sum = tree.reduce((acc, node) => acc + node); // => 100
732+
*/
733+
734+
}, {
735+
key: "reduce",
736+
value: function reduce(reduceFunction) {
737+
var _this3 = this;
738+
739+
var initialValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
740+
741+
var next = initialValue;
742+
this.each(function (node, index) {
743+
next = reduceFunction(next, node.value, index, _this3);
744+
});
745+
return next;
746+
}
747+
748+
/**
749+
* Reverses the current Binary Tree, Left Node becomes Right node and vise versa.
750+
* Does not return new instance, returns current tree instance.
751+
* @method reverse
752+
* @member
753+
* @public
754+
* @returns {BTree} Returns current tree instance.
755+
* @example
756+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
757+
* tree.reverse().toArray(); // [10, 30, 20, 70, 60, 50, 40, 80]
758+
*/
759+
760+
}, {
761+
key: "reverse",
762+
value: function reverse() {
763+
var trav = function trav(currNode, index) {
764+
if (currNode === null) {
765+
return;
766+
}
767+
var temp = currNode.lNode;
768+
currNode.lNode = currNode.rNode;
769+
currNode.rNode = temp;
770+
trav(currNode.lNode);
771+
trav(currNode.rNode);
772+
};
773+
trav(this.root);
774+
return this;
775+
}
674776

675777
/**
676778
* Returns index value from given path.

0 commit comments

Comments
 (0)