Skip to content

Commit 355132c

Browse files
Add Tree class functions
1 parent b6263ee commit 355132c

File tree

11 files changed

+1715
-248
lines changed

11 files changed

+1715
-248
lines changed

DOCUMENTATION.md

Lines changed: 260 additions & 103 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@ var node = new BTreeNode({ value: 10 });
2525
var nodel = new BTreeNode({ value: 15, lNode: node });
2626
```
2727
```
28-
var tree1 = new BTree({ value: 10 });
29-
var tree2 = new BTree({ root: 10 });
30-
var tree3 = new BTree(10);
28+
var tree = new BTree(10);
29+
tree.insert(20);
30+
tree.insert(30);
31+
tree.delete(30);
32+
tree.toArray(); // [{value:10,...},{value:20,...}]
33+
```
34+
```
35+
for (const node of tree) {
36+
console.log(node.value); // 10, 20
37+
}
3138
```
3239
## All Features:
3340
- All Binary Tree data structure functionality.
34-
- extra functions like toString(), toJSON(), validate() etc.
41+
- Main functions like insert(), delete(), each(), find() etc.
42+
- Extended functions like entries(), Symbol.iterator, supports `for...of` loops.
43+
- Conversion methods like fromArray(), toArray(), toString(), toJSON().
3544

3645
## Complete Documentation
3746
Checkout [DOCUMENTATION.md](DOCUMENTATION.md) for complete documentation or View Documentation online at [https://dsinjs.github.io/binary-tree/](https://dsinjs.github.io/binary-tree/)

dist/btreenode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ var BTreeNode = function () {
154154
}();
155155

156156
if (typeof module != "undefined") {
157-
module.exports = BTreeNode;
157+
module.exports = { BTreeNode: BTreeNode };
158158
}
159159
if (typeof window != "undefined") {
160160
window.DSinJS = window.DSinJS || {};

dist/dsinjs-binarytree.js

Lines changed: 190 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ var BTreeNode = function () {
155155
}();
156156

157157
if (typeof module != "undefined") {
158-
module.exports = BTreeNode;
158+
module.exports = { BTreeNode: BTreeNode };
159159
}
160160
if (typeof window != "undefined") {
161161
window.DSinJS = window.DSinJS || {};
@@ -176,7 +176,8 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
176176

177177
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
178178

179-
var BTreeNode = require('./btreenode');
179+
var _require = require('./btreenode'),
180+
BTreeNode = _require.BTreeNode;
180181

181182
/**
182183
* @typedef {{ root: any }} BTreeRootAttrStruct
@@ -255,8 +256,9 @@ var BTree = function () {
255256

256257
/**
257258
* Returns string value of given tree.
258-
* @returns {string} Returns string value of given tree.
259259
* @method toString
260+
* @member
261+
* @public
260262
* @example
261263
* var tree = new BTree(10);
262264
* tree.insert(10);
@@ -275,6 +277,9 @@ var BTree = function () {
275277
/**
276278
* Returns JSON Form.
277279
* @method toJSON
280+
* @member
281+
* @public
282+
* @returns {BTreeNodeStruct} Returns json form of a given tree.
278283
* @example
279284
* var tree = new BTree(10);
280285
* tree.insert(20);
@@ -287,11 +292,34 @@ var BTree = function () {
287292
return this.root.toJSON();
288293
}
289294

295+
/**
296+
* Returns array value.
297+
* @method toArray
298+
* @member
299+
* @public
300+
* @returns {Array<BTreeNode>} Returns array form of given tree.
301+
* @example
302+
* var tree = new BTree(10);
303+
* tree.insert(20);
304+
* tree.toArray(); // [{value:10,...},{value:20,...}]
305+
*/
306+
307+
}, {
308+
key: "toArray",
309+
value: function toArray() {
310+
var arr = [];
311+
this.each(function (node, index) {
312+
arr.push(node);
313+
});
314+
return arr;
315+
}
316+
290317
/**
291318
* Inserts the given value to the tree where first free left child node is found.
292319
* @param {any} val any type of value to be added to tree node.
293320
* @returns {BTreeNode} Returns newly created BTreeNode.
294321
* @method insert
322+
* @member
295323
* @example
296324
* var tree = new BTree(10);
297325
* tree.insert(10);
@@ -309,8 +337,9 @@ var BTree = function () {
309337
/**
310338
* Inserts the given value to the tree where first free left child node is found.
311339
* @param {any} val any type of value to be added to tree node.
312-
* @returns {BTreeNode} Returns newly created BTreeNode.
313340
* @method insertLeftMost
341+
* @member
342+
* @returns {BTreeNode} Returns newly created BTreeNode.
314343
*/
315344

316345
}, {
@@ -328,8 +357,10 @@ var BTree = function () {
328357
/**
329358
* Inserts the given value to the tree where first free right child node is found.
330359
* @param {any} val any type of value to be added to tree node.
331-
* @returns {BTreeNode} Returns newly created BTreeNode.
332360
* @method insertRightMost
361+
* @member
362+
* @public
363+
* @returns {BTreeNode} Returns newly created BTreeNode.
333364
*/
334365

335366
}, {
@@ -350,6 +381,7 @@ var BTree = function () {
350381
* @param {*} val Value to be removed.
351382
* @returns {BTreeNode} Returns removed BTreeNode.
352383
* @method delete
384+
* @member
353385
* @public
354386
*/
355387

@@ -393,6 +425,7 @@ var BTree = function () {
393425
* @param {any} val value to insert.
394426
* @param {number} index index at which to append new element to.
395427
* @method insertAt
428+
* @member
396429
* @public
397430
* @throws UnreachableError
398431
* @example
@@ -454,10 +487,11 @@ var BTree = function () {
454487

455488
/**
456489
* Breadth first search. Executes given callback functions with parameters BTreeNode and path index for each node in BFS fashion.
457-
* @param {Function} callback cb function for each execution.
490+
* @param {Function} callback A callback function for execution of each node.
458491
* @method findBFS
492+
* @member
459493
* @public
460-
* @returns void 0
494+
* @returns {undefined}
461495
*/
462496

463497
}, {
@@ -491,13 +525,161 @@ var BTree = function () {
491525
recInser(this.root, ['U']);
492526
}
493527

528+
/**
529+
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
530+
* @param {Function} callback A callback function for execution of each node.
531+
* @method find
532+
* @member
533+
* @public
534+
* @returns {undefined}
535+
*/
536+
537+
}, {
538+
key: "find",
539+
value: function find(callback) {
540+
/**
541+
*
542+
* @param {BTreeNode} currNode Currently processing node.
543+
* @param {Array<'U'|'L'|'R'>} path current path
544+
*/
545+
var recFnc = function recFnc(currNode, path) {
546+
if (currNode !== null) {
547+
callback(currNode, BTree.getIndexFromPath(path));
548+
if (currNode.lNode !== null) {
549+
var lPath = JSON.parse(JSON.stringify(path));
550+
lPath.push('L');
551+
recFnc(currNode.lNode, lPath);
552+
}
553+
if (currNode.rNode !== null) {
554+
var rPath = JSON.parse(JSON.stringify(path));
555+
rPath.push('R');
556+
recFnc(currNode.rNode, rPath);
557+
}
558+
}
559+
};
560+
561+
recFnc(this.root, ['U']);
562+
}
563+
564+
/**
565+
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
566+
* @param {Function} callback A callback function for execution of each node.
567+
* @method each
568+
* @member
569+
* @public
570+
* @returns {undefined}
571+
*/
572+
573+
}, {
574+
key: "each",
575+
value: function each(callback) {
576+
return this.find(callback);
577+
}
578+
579+
/**
580+
* Depth first search, Executes given callback functions with parameters BTreeNode and path index for each node in DFS fashion.
581+
* @param {Function} callback A callback function for execution of each node.
582+
* @method each
583+
* @member
584+
* @public
585+
* @returns {undefined}
586+
*/
587+
588+
}, {
589+
key: "forEach",
590+
value: function forEach(callback) {
591+
return this.find(callback);
592+
}
593+
594+
/**
595+
* Returns an iterable of key, value pairs for every entry in the tree.
596+
* @method [Symbol.iterator]
597+
* @member
598+
* @public
599+
* @example
600+
* var tree = new BTree(10);
601+
* for (const node of tree) {
602+
* console.log(node.value); // 10
603+
* }
604+
*/
605+
606+
}, {
607+
key: Symbol.iterator,
608+
value: function value() {
609+
var curr = -1;
610+
var arr = this.toArray();
611+
return {
612+
/**
613+
* @returns { {value: BTreeNode, done: boolean} }
614+
*/
615+
next: function next() {
616+
curr++;
617+
return {
618+
value: arr[curr] === void 0 ? void 0 : arr[curr],
619+
done: !!(curr === arr.length)
620+
};
621+
}
622+
};
623+
}
624+
625+
/**
626+
* Returns an iterable of key, value pairs for every entry in the tree.
627+
* @method entries
628+
* @member
629+
* @public
630+
* @returns {IterableIterator<[number, BTreeNode]>} Iterable for iterations.
631+
* @example
632+
* var tree = new BTree(10);
633+
* for (const [index, node] of tree.entries()) {
634+
* console.log(index, node.value); // 0, 10
635+
* }
636+
*/
637+
638+
}, {
639+
key: "entries",
640+
value: function entries() {
641+
return this.toArray().entries();
642+
}
643+
644+
/**
645+
* Maps current tree values to a new tree with modifying the values using given callback function.
646+
* @param {Function} callback callback function for value modifier.
647+
* @method map
648+
* @member
649+
* @public
650+
* @returns {BTree} A new BTree
651+
* @example
652+
* var tree = BTree.fromArray([10, 20, 30, 40]);
653+
* var tree2 = tree.map(n => n * 2);
654+
* var arr2 = tree2.toArray(); // [{value:20,...},{value:40,...},{value:80,...},{value:60,...}]
655+
*/
656+
657+
}, {
658+
key: "map",
659+
value: function map(callback) {
660+
var newTree = new BTree(callback(this.root.value));
661+
this.each(function (node, index) {
662+
if (index !== 1) {
663+
var retVal = callback(node.value);
664+
newTree.insertAt(retVal, index);
665+
}
666+
});
667+
return newTree;
668+
}
669+
670+
/* filter(callback) {
671+
}
672+
reduce() {
673+
} */
674+
494675
/**
495676
* Returns index value from given path.
496677
* @param {Array<'U'|'L'|'R'>} path Array for U L or R, which represents the quickest path to get to a node.
497678
* @returns {number} Returns index value.
498679
* @method getIndexFromPath
499680
* @public
500681
* @static
682+
* @member
501683
*/
502684

503685
}], [{
@@ -525,8 +707,8 @@ var BTree = function () {
525707
* @param {number} index Index number from which path to be calculated.
526708
* @returns {Array<'U'|'L'|'R'>} Path equivalent to the given index.
527709
* @method getPathFromIndex
528-
* @static
529710
* @public
711+
* @static
530712
*/
531713

532714
}, {

0 commit comments

Comments
 (0)