Skip to content

Commit 93ac719

Browse files
authored
Merge pull request #1 from datastructures-js/development
v1.0.0
2 parents 0e421b4 + cff74b4 commit 93ac719

File tree

11 files changed

+719
-1
lines changed

11 files changed

+719
-1
lines changed

.eslintrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"rules": {
3+
max-len: ["error", { "code": 80, "ignoreComments": true }],
4+
"comma-dangle": ["error", {
5+
"functions": "ignore"
6+
}]
7+
},
8+
"env": {
9+
"mocha": true,
10+
"node": true
11+
},
12+
"extends": ["airbnb-base"]
13+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
coverage
3+
.DS_Store

.gitignore/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.git*
2+
*.spec.js
3+
.travis.yml
4+
Gruntfile.js
5+
coverage/

.travis.yml

Whitespace-only changes.

Gruntfile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = (grunt) => {
2+
grunt.initConfig({
3+
eslint: {
4+
src: ['./*.js', './*.spec.js']
5+
},
6+
mochaTest: {
7+
files: ['./*.spec.js']
8+
},
9+
mocha_istanbul: {
10+
coverage: {
11+
src: './',
12+
options: {
13+
mask: '*.spec.js'
14+
}
15+
}
16+
}
17+
});
18+
19+
grunt.loadNpmTasks('grunt-eslint');
20+
grunt.loadNpmTasks('grunt-mocha-test');
21+
grunt.loadNpmTasks('grunt-mocha-istanbul');
22+
23+
grunt.registerTask('lint', ['eslint']);
24+
grunt.registerTask('test', ['mochaTest']);
25+
grunt.registerTask('coverage', ['mocha_istanbul']);
26+
grunt.registerTask('build', ['lint', 'coverage']);
27+
};

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Eyas Ranjous <eyas.ranjous@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# @datastrucures-js/binary-search-tree
2+
3+
[![build:?](https://travis-ci.org/eyas-ranjous/datatructures-js/binary-search-tree.svg?branch=master)](https://travis-ci.org/eyas-ranjous/datatructures-js/binary-search-tree)
4+
[![npm](https://img.shields.io/npm/v/@datastructures-js/binary-search-tree.svg)](https://www.npmjs.com/package/@datastructures-js/binary-search-tree)
5+
[![npm](https://img.shields.io/npm/dm/@datastructures-js/binary-search-tree.svg)](https://www.npmjs.com/packages/@datastructures-js/binary-search-tree) [![npm](https://img.shields.io/badge/node-%3E=%206.0-blue.svg)](https://www.npmjs.com/package/@datastructures-js/binary-search-tree)
6+
7+
node's data type: **string**, **number**.
8+
9+
<img width="413" alt="Binary Search Tree" src="https://user-images.githubusercontent.com/6517308/35762621-74a72626-085f-11e8-8934-ef6facdd6e10.png">
10+
11+
## Usage
12+
```js
13+
const binarySearchTree = require('@datastructures-js/binary-search-tree');
14+
const bst = binarySearchTree();
15+
```
16+
17+
## API
18+
19+
**.node(value, parent, left, right)**
20+
21+
creates a bst node.
22+
23+
* **.setValue(value)** sets the node's value.
24+
* **.getValue(value)** gets the node's value.
25+
* **.setParent(parent)** sets the parent node.
26+
* **.getParent(parent)** gets the parent node.
27+
* **.setLeft(left)** sets the node's left child.
28+
* **.getLeft()** gets the node's left child.
29+
* **.setRight(left)** sets the node's right child.
30+
* **.getRight()** gets the node's right child.
31+
32+
```js
33+
const n = bst.node('test', null,);
34+
console.log(n.getValue()); // test
35+
console.log(n.getParent()); // null
36+
console.log(n.getLeft()); // null
37+
console.log(n.getRight()); // null
38+
```
39+
40+
**.insert(value)**
41+
42+
inserts a value into the tree.
43+
```javascript
44+
bst.insert(50);
45+
bst.insert(80);
46+
bst.insert(30);
47+
bst.insert(90);
48+
bst.insert(60);
49+
bst.insert(40);
50+
bst.insert(20);
51+
```
52+
53+
**.root()**
54+
55+
gets the root node
56+
```javascript
57+
console.log(bst.root().getValue()); // 90
58+
```
59+
60+
**.min()**
61+
62+
finds the min value node (most left).
63+
```javascript
64+
console.log(bst.min().getValue()); // 20
65+
```
66+
67+
**.max()**
68+
69+
finds the min value node (most right).
70+
```javascript
71+
console.log(bst.max().getValue()); // 90
72+
```
73+
74+
**.count()**
75+
76+
gets nodes count.
77+
```javascript
78+
console.log(bst.count()); // 7
79+
```
80+
81+
**.find(value)**
82+
83+
finds the value's node or returns null if not found.
84+
```javascript
85+
let n = bst.find(30);
86+
console.log(n.getValue()); // 30
87+
console.log(n.getRight().getValue()); // 40
88+
console.log(n.getLeft().getValue()); // 20
89+
```
90+
91+
**.traverseInOrder(cb)**
92+
```js
93+
// in-order traverse (left-parent-right)
94+
bst.traverseInOrder(node => console.log(node.getValue()));
95+
96+
// 20
97+
// 30
98+
// 40
99+
// 50
100+
// 60
101+
// 80
102+
// 90
103+
```
104+
105+
**.traversePreOrder(cb)**
106+
107+
```js
108+
// pre-order traverse (parent-left-right)
109+
bst.traversePreOrder(node => console.log(node.getValue()));
110+
111+
// 50
112+
// 30
113+
// 20
114+
// 40
115+
// 80
116+
// 60
117+
// 90
118+
```
119+
120+
**.traversePostOrder(cb)**
121+
122+
```js
123+
// post-order traverse (left-right-parent)
124+
bst.traverse(node => console.log(node.getValue()));
125+
126+
// 20
127+
// 40
128+
// 30
129+
// 60
130+
// 90
131+
// 80
132+
// 50
133+
```
134+
135+
**.traverse(cb, order)**
136+
137+
traverse the tree in the defined order and apply a callback on each node.
138+
139+
order values: `inOrder`, `preOrder` OR `postOrder`. default is `inOrder`
140+
141+
```js
142+
bst.traverse(node => console.log(node.getValue())); // in-order
143+
144+
// 20
145+
// 30
146+
// 40
147+
// 50
148+
// 60
149+
// 80
150+
// 90
151+
152+
bst.traverse(node => console.log(node.getValue()), 'preOrder');
153+
154+
// 50
155+
// 30
156+
// 20
157+
// 40
158+
// 80
159+
// 60
160+
// 90
161+
```
162+
163+
164+
**.remove(value)**
165+
166+
removes a value's node (if exists) from the tree.
167+
```javascript
168+
bst.remove(30);
169+
let n50 = bst.find(50);
170+
let n40 = bst.find(40);
171+
console.log(n50.getLeft().getValue()); // 40
172+
console.log(n40.getLeft().getValue()); // 20
173+
```
174+
175+
**.clear()**
176+
177+
clears the tree.
178+
```javascript
179+
bst.clear();
180+
console.log(bst.count()); // 0
181+
```
182+
183+
## Build
184+
```
185+
grunt build
186+
```
187+
188+
## License
189+
The MIT License. Full License is [here](https://github.com/datastructures-js/binary-search-tree/blob/master/LICENSE)

0 commit comments

Comments
 (0)