Skip to content

Commit a3e6705

Browse files
Add more array functions
1 parent b77908f commit a3e6705

File tree

9 files changed

+859
-93
lines changed

9 files changed

+859
-93
lines changed

DOCUMENTATION.md

Lines changed: 189 additions & 81 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
# @dsinjs/binary-tree
2-
Data structure in your javascript code, Binary Trees.
2+
> Data structure in your JavaScript code, Binary Trees.
33
44
[![Build Status](https://travis-ci.com/dsinjs/binary-tree.svg?branch=main)](https://travis-ci.com/dsinjs/binary-tree)
55
![Node.js CI](https://github.com/dsinjs/binary-tree/workflows/Node.js%20CI/badge.svg?branch=main)
66

7-
## Installation:
7+
## Overview
8+
Binary Tree in Javascript
9+
```
10+
tree
11+
----
12+
j <-- root
13+
/ \
14+
f k
15+
/ \ \
16+
a h z <-- leaves
17+
```
18+
- Binary tree always has nodes with maximum children of 2.
19+
- Index starts with 1 such that index of j is 1, f is 2, z is 7 and so on.
20+
- Root is single node always on top of the tree, in this case its j.
21+
- Leaves nodes are the nodes who does not have any children, in this case its a, h, z.
22+
## Installation
823
Using npm
924
```
1025
npm install @dsinjs/binary-tree --save
@@ -16,7 +31,7 @@ Or directly on your browser, simply download your file from the following:
1631
<script type="application/javascript" src="dsinjs-binarytree.js"></script>
1732
<script type="application/javascript" src="dsinjs-binarytree.min.js"></script>
1833
```
19-
## Usage:
34+
## Usage
2035
```
2136
const { BTreeNode, BTree } = require('@dsinjs/binary-tree');
2237
```
@@ -32,12 +47,14 @@ tree.delete(30);
3247
tree.toArray(); // [{value:10,...},{value:20,...}]
3348
```
3449
```
50+
// Classic ES6 iterations
3551
for (const node of tree) {
3652
console.log(node.value); // 10, 20
3753
}
3854
```
3955
## All Features:
4056
- All Binary Tree data structure functionality.
57+
- 25+ Binary Tree functions.
4158
- Main functions like insert(), delete(), each(), find() etc.
4259
- Extended functions like entries(), Symbol.iterator, supports `for...of` loops.
4360
- Conversion methods like fromArray(), toArray(), toString(), toJSON().
@@ -46,6 +63,6 @@ for (const node of tree) {
4663
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/)
4764

4865

49-
## Help us expand:
66+
## Help us expand
5067
Let me know in issues/github page or on email which javascript functions to include in next release.
5168
Check all the [Contributing authors](CONTRIBUTING.md) to this library.

dist/dsinjs-binarytree.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ var BTree = function () {
754754
* @returns {BTree} Returns current tree instance.
755755
* @example
756756
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
757-
* tree.reverse().toArray(); // [10, 30, 20, 70, 60, 50, 40, 80]
757+
* tree.reverse().toArray(); // => [10, 30, 20, 70, 60, 50, 40, 80]
758758
*/
759759

760760
}, {
@@ -774,6 +774,89 @@ var BTree = function () {
774774
return this;
775775
}
776776

777+
/**
778+
* Returns first index of a value matched, if it is not present, it returns -1.
779+
* @param {any} value Any value to find.
780+
* @method indexOf
781+
* @member
782+
* @public
783+
* @returns {number} Returns index of given item.
784+
* @example
785+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
786+
* tree.indexOf(30); // => 3
787+
* tree.indexOf(51); // => -1
788+
*/
789+
790+
}, {
791+
key: "indexOf",
792+
value: function indexOf(value) {
793+
var retIndex = -1;
794+
this.each(function (node, index) {
795+
if (node.value === value && retIndex === -1) {
796+
retIndex = index;
797+
}
798+
});
799+
800+
return retIndex;
801+
}
802+
803+
/**
804+
* Checks if given item exists or not, returns boolean.
805+
* @param {any} value Any value to check if it exists or not.
806+
* @method includes
807+
* @member
808+
* @public
809+
* @returns {boolean} Returns true if it is present, otherwise false.
810+
* @example
811+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
812+
* tree.includes(30); // true
813+
* tree.includes(51); // false
814+
*/
815+
816+
}, {
817+
key: "includes",
818+
value: function includes(value) {
819+
return this.indexOf(value) !== -1;
820+
}
821+
822+
/**
823+
* Checks if given item exists or not, returns boolean.
824+
* @param {any} value Any value to check if it exists or not.
825+
* @method exists
826+
* @member
827+
* @public
828+
* @returns {boolean} Returns true if it is present, otherwise false.
829+
* @example
830+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
831+
* tree.includes(30); // true
832+
* tree.includes(51); // false
833+
*/
834+
835+
}, {
836+
key: "exists",
837+
value: function exists(value) {
838+
return this.indexOf(value) !== -1;
839+
}
840+
841+
/**
842+
* Checks if given item exists or not, returns boolean.
843+
* @param {any} value Any value to check if it exists or not.
844+
* @method has
845+
* @member
846+
* @public
847+
* @returns {boolean} Returns true if it is present, otherwise false.
848+
* @example
849+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
850+
* tree.includes(30); // true
851+
* tree.includes(51); // false
852+
*/
853+
854+
}, {
855+
key: "has",
856+
value: function has(value) {
857+
return this.indexOf(value) !== -1;
858+
}
859+
777860
/**
778861
* Returns index value from given path.
779862
* @param {Array<'U'|'L'|'R'>} path Array for U L or R, which represents the quickest path to get to a node.

dist/dsinjs-binarytree.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ var BTree = function () {
590590
* @returns {BTree} Returns current tree instance.
591591
* @example
592592
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
593-
* tree.reverse().toArray(); // [10, 30, 20, 70, 60, 50, 40, 80]
593+
* tree.reverse().toArray(); // => [10, 30, 20, 70, 60, 50, 40, 80]
594594
*/
595595

596596
}, {
@@ -610,6 +610,89 @@ var BTree = function () {
610610
return this;
611611
}
612612

613+
/**
614+
* Returns first index of a value matched, if it is not present, it returns -1.
615+
* @param {any} value Any value to find.
616+
* @method indexOf
617+
* @member
618+
* @public
619+
* @returns {number} Returns index of given item.
620+
* @example
621+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
622+
* tree.indexOf(30); // => 3
623+
* tree.indexOf(51); // => -1
624+
*/
625+
626+
}, {
627+
key: "indexOf",
628+
value: function indexOf(value) {
629+
var retIndex = -1;
630+
this.each(function (node, index) {
631+
if (node.value === value && retIndex === -1) {
632+
retIndex = index;
633+
}
634+
});
635+
636+
return retIndex;
637+
}
638+
639+
/**
640+
* Checks if given item exists or not, returns boolean.
641+
* @param {any} value Any value to check if it exists or not.
642+
* @method includes
643+
* @member
644+
* @public
645+
* @returns {boolean} Returns true if it is present, otherwise false.
646+
* @example
647+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
648+
* tree.includes(30); // true
649+
* tree.includes(51); // false
650+
*/
651+
652+
}, {
653+
key: "includes",
654+
value: function includes(value) {
655+
return this.indexOf(value) !== -1;
656+
}
657+
658+
/**
659+
* Checks if given item exists or not, returns boolean.
660+
* @param {any} value Any value to check if it exists or not.
661+
* @method exists
662+
* @member
663+
* @public
664+
* @returns {boolean} Returns true if it is present, otherwise false.
665+
* @example
666+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
667+
* tree.includes(30); // true
668+
* tree.includes(51); // false
669+
*/
670+
671+
}, {
672+
key: "exists",
673+
value: function exists(value) {
674+
return this.indexOf(value) !== -1;
675+
}
676+
677+
/**
678+
* Checks if given item exists or not, returns boolean.
679+
* @param {any} value Any value to check if it exists or not.
680+
* @method has
681+
* @member
682+
* @public
683+
* @returns {boolean} Returns true if it is present, otherwise false.
684+
* @example
685+
* var tree = BTree.fromArray([10, 20, 30, 40, 50, 60, 70, 80]);
686+
* tree.includes(30); // true
687+
* tree.includes(51); // false
688+
*/
689+
690+
}, {
691+
key: "has",
692+
value: function has(value) {
693+
return this.indexOf(value) !== -1;
694+
}
695+
613696
/**
614697
* Returns index value from given path.
615698
* @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

Comments
 (0)