Skip to content

Commit d88d463

Browse files
authored
Merge pull request #11 from datastructures-js/development
v2.0.0
2 parents e73f4ee + 6821218 commit d88d463

File tree

6 files changed

+239
-211
lines changed

6 files changed

+239
-211
lines changed

Gruntfile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module.exports = (grunt) => {
22
grunt.initConfig({
33
eslint: {
4-
src: ['./*.js', './*.spec.js']
4+
src: ['./*.js', './*.test.js']
55
},
66
mochaTest: {
7-
files: ['./*.spec.js']
7+
files: ['./*.test.js']
88
},
99
mocha_istanbul: {
1010
coverage: {
1111
src: './',
1212
options: {
13-
mask: '*.spec.js'
13+
mask: '*.test.js'
1414
}
1515
}
1616
}

README.md

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,103 @@
44
[![npm](https://img.shields.io/npm/v/@datastructures-js/binary-search-tree.svg)](https://www.npmjs.com/package/@datastructures-js/binary-search-tree)
55
[![npm](https://img.shields.io/npm/dm/@datastructures-js/binary-search-tree.svg)](https://www.npmjs.com/package/@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)
66

7-
node's data type: **string**, **number**.
7+
node's **key** data type: **string**, **number**.
8+
node's **value** data type: any.
89

910
<img width="413" alt="Binary Search Tree" src="https://user-images.githubusercontent.com/6517308/35762621-74a72626-085f-11e8-8934-ef6facdd6e10.png">
1011

1112
## Usage
13+
```
14+
npm install --save @datastructures-js/binary-search-tree
15+
```
16+
17+
then
18+
1219
```js
1320
const binarySearchTree = require('@datastructures-js/binary-search-tree');
1421
const bst = binarySearchTree();
1522
```
1623

1724
## API
1825

19-
**.node(value, parent, left, right)**
20-
21-
creates a bst node.
26+
### .node(key, value, parent, left, right)
27+
creates a bst node with the following api.
2228

23-
* **.setValue(value)** sets the node's value.
24-
* **.getValue()** gets the node's value.
25-
* **.setParent(parent)** sets the parent node.
26-
* **.getParent()** gets the parent node.
27-
* **.setLeft(left)** sets the node's left child.
28-
* **.getLeft()** gets the node's left child.
29-
* **.setRight(right)** sets the node's right child.
30-
* **.getRight()** gets the node's right child.
29+
* .setKey(key)
30+
* .getKey()
31+
* .setValue(value)
32+
* .getValue()
33+
* .setParent(node)
34+
* .getParent()
35+
* .setLeft(node)
36+
* .getLeft()
37+
* .setRight(node)
38+
* .getRight()
3139

3240
```js
33-
const n = bst.node('test');
41+
const n = bst.node(1, 'test');
42+
console.log(n.getKey()); // 1
3443
console.log(n.getValue()); // test
3544
console.log(n.getParent()); // null
3645
console.log(n.getLeft()); // null
3746
console.log(n.getRight()); // null
3847
```
3948

40-
**.insert(value)**
49+
### .insert(key, value)
4150

42-
inserts a value into the tree.
51+
inserts a node with key/value into the tree.
4352
```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);
53+
bst.insert(50, 'v1');
54+
bst.insert(80, 'v2');
55+
bst.insert(30, 'v3');
56+
bst.insert(90, 'v4');
57+
bst.insert(60, 'v5');
58+
bst.insert(40, 'v6');
59+
bst.insert(20, 'v7');
5160
```
5261

53-
**.root()**
62+
### .root()
5463

5564
gets the root node
5665
```javascript
57-
console.log(bst.root().getValue()); // 50
66+
console.log(bst.root().getKey()); // 50
5867
```
5968

60-
**.min()**
69+
### .min()
6170

62-
finds the min value node (most left).
71+
finds the min key node (most left).
6372
```javascript
64-
console.log(bst.min().getValue()); // 20
73+
console.log(bst.min().getKey()); // 20
6574
```
6675

67-
**.max()**
76+
### .max()
6877

69-
finds the min value node (most right).
78+
finds the max key node (most right).
7079
```javascript
71-
console.log(bst.max().getValue()); // 90
80+
console.log(bst.max().getKey()); // 90
7281
```
7382

74-
**.count()**
83+
### .count()
7584

7685
gets nodes count.
7786
```javascript
7887
console.log(bst.count()); // 7
7988
```
8089

81-
**.find(value)**
90+
### .search(key)
8291

83-
finds the value's node or returns null if not found.
92+
finds a node by key or returns null if not found.
8493
```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
94+
const n = bst.search(30);
95+
console.log(n.getKey()); // 30
96+
console.log(n.getRight().getKey()); // 40
97+
console.log(n.getLeft().getKey()); // 20
8998
```
9099

91-
**.traverseInOrder(cb)**
100+
### .traverseInOrder(cb)
92101
```js
93102
// in-order traverse (left-parent-right)
94-
bst.traverseInOrder(node => console.log(node.getValue()));
103+
bst.traverseInOrder(node => console.log(node.getKey()));
95104

96105
// 20
97106
// 30
@@ -102,11 +111,11 @@ bst.traverseInOrder(node => console.log(node.getValue()));
102111
// 90
103112
```
104113

105-
**.traversePreOrder(cb)**
114+
### .traversePreOrder(cb)
106115

107116
```js
108117
// pre-order traverse (parent-left-right)
109-
bst.traversePreOrder(node => console.log(node.getValue()));
118+
bst.traversePreOrder(node => console.log(node.getKey()));
110119

111120
// 50
112121
// 30
@@ -117,11 +126,11 @@ bst.traversePreOrder(node => console.log(node.getValue()));
117126
// 90
118127
```
119128

120-
**.traversePostOrder(cb)**
129+
### .traversePostOrder(cb)
121130

122131
```js
123132
// post-order traverse (left-right-parent)
124-
bst.traverse(node => console.log(node.getValue()));
133+
bst.traverse(node => console.log(node.getKey()));
125134

126135
// 20
127136
// 40
@@ -132,14 +141,14 @@ bst.traverse(node => console.log(node.getValue()));
132141
// 50
133142
```
134143

135-
**.traverse(cb, order)**
144+
### .traverse(cb, order)
136145

137146
traverse the tree in the defined order and apply a callback on each node.
138147

139148
order values: `inOrder`, `preOrder` OR `postOrder`. default is `inOrder`
140149

141150
```js
142-
bst.traverse(node => console.log(node.getValue())); // in-order
151+
bst.traverse(node => console.log(node.getKey())); // in-order
143152

144153
// 20
145154
// 30
@@ -149,7 +158,7 @@ bst.traverse(node => console.log(node.getValue())); // in-order
149158
// 80
150159
// 90
151160

152-
bst.traverse(node => console.log(node.getValue()), 'preOrder');
161+
bst.traverse(node => console.log(node.getKey()), 'preOrder');
153162

154163
// 50
155164
// 30
@@ -161,16 +170,16 @@ bst.traverse(node => console.log(node.getValue()), 'preOrder');
161170
```
162171

163172

164-
**.remove(value)**
173+
### .remove(value)
165174

166175
removes a value's node (if exists) from the tree.
167176
```javascript
168-
console.log(bst.find(30).getValue()); // 30
177+
console.log(bst.search(30).getKey()); // 30
169178
bst.remove(30);
170-
console.log(bst.find(30)); // null
179+
console.log(bst.search(30)); // null
171180
```
172181

173-
**.clear()**
182+
### .clear()
174183

175184
clears the tree.
176185
```javascript

0 commit comments

Comments
 (0)