bst-typed 1.19.5 → 1.19.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +204 -10
- package/dist/bst.d.ts +1 -0
- package/dist/bst.js +6 -0
- package/dist/index.d.ts +1 -8
- package/dist/index.js +15 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,30 +1,180 @@
|
|
|
1
1
|
# What
|
|
2
|
+
|
|
2
3
|
## Brief
|
|
3
|
-
|
|
4
|
+
Javascript & TypeScript Data Structure Library.
|
|
5
|
+
|
|
6
|
+
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
|
|
7
|
+
|
|
8
|
+
## Algorithms list only a few out, you can discover more in API docs
|
|
4
9
|
|
|
10
|
+
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
11
|
+
|
|
12
|
+
## Code design
|
|
13
|
+
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
5
14
|
|
|
6
15
|
# How
|
|
7
16
|
|
|
8
17
|
## install
|
|
9
|
-
###
|
|
18
|
+
### yarn
|
|
19
|
+
|
|
10
20
|
```bash
|
|
11
|
-
|
|
21
|
+
yarn add data-structure-typed
|
|
12
22
|
```
|
|
13
|
-
|
|
23
|
+
|
|
24
|
+
### npm
|
|
25
|
+
|
|
14
26
|
```bash
|
|
15
|
-
|
|
27
|
+
npm install data-structure-typed
|
|
16
28
|
```
|
|
17
29
|
|
|
18
|
-
### snippet
|
|
30
|
+
### Binary Search Tree (BST) snippet
|
|
31
|
+
|
|
19
32
|
#### TS
|
|
20
33
|
```typescript
|
|
34
|
+
import {BST, BSTNode} from 'data-structure-typed';
|
|
35
|
+
|
|
36
|
+
const bst = new BST();
|
|
37
|
+
bst.add(11);
|
|
38
|
+
bst.add(3);
|
|
39
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
40
|
+
bst.size === 16; // true
|
|
41
|
+
bst.has(6); // true
|
|
42
|
+
const node6 = bst.get(6);
|
|
43
|
+
bst.getHeight(6) === 2; // true
|
|
44
|
+
bst.getHeight() === 5; // true
|
|
45
|
+
bst.getDepth(6) === 3; // true
|
|
46
|
+
const leftMost = bst.getLeftMost();
|
|
47
|
+
leftMost?.id === 1; // true
|
|
48
|
+
expect(leftMost?.id).toBe(1);
|
|
49
|
+
bst.remove(6);
|
|
50
|
+
bst.get(6); // null
|
|
51
|
+
bst.isAVLBalanced(); // true or false
|
|
52
|
+
const bfsIDs = bst.BFS();
|
|
53
|
+
bfsIDs[0] === 11; // true
|
|
54
|
+
expect(bfsIDs[0]).toBe(11);
|
|
55
|
+
|
|
56
|
+
const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
|
|
57
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
58
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
59
|
+
|
|
60
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
61
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
62
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
63
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
64
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
65
|
+
|
|
66
|
+
objBST.remove(11);
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const avlTree = new AVLTree();
|
|
70
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
71
|
+
avlTree.isAVLBalanced(); // true
|
|
72
|
+
avlTree.remove(10);
|
|
73
|
+
avlTree.isAVLBalanced(); // true
|
|
21
74
|
|
|
22
75
|
```
|
|
23
76
|
#### JS
|
|
24
77
|
```javascript
|
|
78
|
+
const {BST, BSTNode} = require('data-structure-typed');
|
|
79
|
+
|
|
80
|
+
const bst = new BST();
|
|
81
|
+
bst.add(11);
|
|
82
|
+
bst.add(3);
|
|
83
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
84
|
+
bst.size === 16; // true
|
|
85
|
+
bst.has(6); // true
|
|
86
|
+
const node6 = bst.get(6);
|
|
87
|
+
bst.getHeight(6) === 2; // true
|
|
88
|
+
bst.getHeight() === 5; // true
|
|
89
|
+
bst.getDepth(6) === 3; // true
|
|
90
|
+
const leftMost = bst.getLeftMost();
|
|
91
|
+
leftMost?.id === 1; // true
|
|
92
|
+
expect(leftMost?.id).toBe(1);
|
|
93
|
+
bst.remove(6);
|
|
94
|
+
bst.get(6); // null
|
|
95
|
+
bst.isAVLBalanced(); // true or false
|
|
96
|
+
const bfsIDs = bst.BFS();
|
|
97
|
+
bfsIDs[0] === 11; // true
|
|
98
|
+
expect(bfsIDs[0]).toBe(11);
|
|
99
|
+
|
|
100
|
+
const objBST = new BST();
|
|
101
|
+
objBST.add(11, {id: 11, keyA: 11});
|
|
102
|
+
objBST.add(3, {id: 3, keyA: 3});
|
|
103
|
+
|
|
104
|
+
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
105
|
+
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
106
|
+
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
107
|
+
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
108
|
+
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
109
|
+
|
|
110
|
+
objBST.remove(11);
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
const avlTree = new AVLTree();
|
|
114
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
115
|
+
avlTree.isAVLBalanced(); // true
|
|
116
|
+
avlTree.remove(10);
|
|
117
|
+
avlTree.isAVLBalanced(); // true
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Directed Graph simple snippet
|
|
25
122
|
|
|
123
|
+
#### TS or JS
|
|
124
|
+
```typescript
|
|
125
|
+
import {DirectedGraph} from 'data-structure-typed';
|
|
126
|
+
|
|
127
|
+
const graph = new DirectedGraph();
|
|
128
|
+
|
|
129
|
+
graph.addVertex('A');
|
|
130
|
+
graph.addVertex('B');
|
|
131
|
+
|
|
132
|
+
graph.hasVertex('A'); // true
|
|
133
|
+
graph.hasVertex('B'); // true
|
|
134
|
+
graph.hasVertex('C'); // false
|
|
135
|
+
|
|
136
|
+
graph.addEdge('A', 'B');
|
|
137
|
+
graph.hasEdge('A', 'B'); // true
|
|
138
|
+
graph.hasEdge('B', 'A'); // false
|
|
139
|
+
|
|
140
|
+
graph.removeEdgeSrcToDest('A', 'B');
|
|
141
|
+
graph.hasEdge('A', 'B'); // false
|
|
142
|
+
|
|
143
|
+
graph.addVertex('C');
|
|
144
|
+
|
|
145
|
+
graph.addEdge('A', 'B');
|
|
146
|
+
graph.addEdge('B', 'C');
|
|
147
|
+
|
|
148
|
+
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Undirected Graph snippet
|
|
152
|
+
|
|
153
|
+
#### TS or JS
|
|
154
|
+
```typescript
|
|
155
|
+
import {UndirectedGraph} from 'data-structure-typed';
|
|
156
|
+
|
|
157
|
+
const graph = new UndirectedGraph();
|
|
158
|
+
graph.addVertex('A');
|
|
159
|
+
graph.addVertex('B');
|
|
160
|
+
graph.addVertex('C');
|
|
161
|
+
graph.addVertex('D');
|
|
162
|
+
graph.removeVertex('C');
|
|
163
|
+
graph.addEdge('A', 'B');
|
|
164
|
+
graph.addEdge('B', 'D');
|
|
165
|
+
|
|
166
|
+
const dijkstraResult = graph.dijkstra('A');
|
|
167
|
+
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
26
168
|
```
|
|
27
169
|
|
|
170
|
+

|
|
171
|
+
|
|
172
|
+

|
|
173
|
+
|
|
174
|
+

|
|
175
|
+
|
|
176
|
+

|
|
177
|
+
|
|
28
178
|
|
|
29
179
|
## API docs & Examples
|
|
30
180
|
|
|
@@ -32,6 +182,10 @@ yarn add bst-typed
|
|
|
32
182
|
|
|
33
183
|
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
34
184
|
|
|
185
|
+
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
186
|
+
|
|
187
|
+
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
188
|
+
|
|
35
189
|
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
36
190
|
|
|
37
191
|
## Data Structures
|
|
@@ -49,10 +203,10 @@ yarn add bst-typed
|
|
|
49
203
|
<tbody>
|
|
50
204
|
<tr>
|
|
51
205
|
<td>Binary Tree</td>
|
|
52
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
53
|
-
</td>
|
|
54
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
55
|
-
</td>
|
|
206
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
|
|
207
|
+
</img></td>
|
|
208
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt="">
|
|
209
|
+
</img></td>
|
|
56
210
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
|
|
57
211
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
58
212
|
</tr>
|
|
@@ -499,6 +653,46 @@ yarn add bst-typed
|
|
|
499
653
|
|
|
500
654
|

|
|
501
655
|
|
|
656
|
+
[//]: # ()
|
|
657
|
+
|
|
658
|
+
[//]: # ()
|
|
659
|
+
[//]: # ()
|
|
660
|
+
|
|
661
|
+
[//]: # ()
|
|
662
|
+
[//]: # ()
|
|
663
|
+
|
|
664
|
+
[//]: # ()
|
|
665
|
+
[//]: # ()
|
|
666
|
+
|
|
667
|
+
[//]: # ()
|
|
668
|
+
[//]: # ()
|
|
669
|
+
|
|
670
|
+
[//]: # ()
|
|
671
|
+
[//]: # ()
|
|
672
|
+
|
|
673
|
+
[//]: # ()
|
|
674
|
+
[//]: # ()
|
|
675
|
+
|
|
676
|
+
[//]: # ()
|
|
677
|
+
[//]: # ()
|
|
678
|
+
|
|
679
|
+
[//]: # ()
|
|
680
|
+
[//]: # ()
|
|
681
|
+
|
|
682
|
+
[//]: # ()
|
|
683
|
+
[//]: # ()
|
|
684
|
+
|
|
685
|
+
[//]: # ()
|
|
686
|
+
[//]: # ()
|
|
687
|
+
|
|
688
|
+
[//]: # ()
|
|
689
|
+
[//]: # ()
|
|
690
|
+
|
|
691
|
+
[//]: # ()
|
|
692
|
+
|
|
693
|
+
[//]: # ()
|
|
694
|
+
|
|
695
|
+
[//]: # ()
|
|
502
696
|
|
|
503
697
|
|
|
504
698
|
|
package/dist/bst.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BSTNode, BST } from 'data-structure-typed';
|
package/dist/bst.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BST = exports.BSTNode = void 0;
|
|
4
|
+
var data_structure_typed_1 = require("data-structure-typed");
|
|
5
|
+
Object.defineProperty(exports, "BSTNode", { enumerable: true, get: function () { return data_structure_typed_1.BSTNode; } });
|
|
6
|
+
Object.defineProperty(exports, "BST", { enumerable: true, get: function () { return data_structure_typed_1.BST; } });
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* data-structure-typed
|
|
6
|
-
*
|
|
7
|
-
* @author Tyler Zeng
|
|
8
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
-
* @license MIT License
|
|
10
|
-
*/
|
|
11
|
-
var data_structure_typed_1 = require("data-structure-typed");
|
|
12
|
-
Object.defineProperty(exports, "BSTNode", { enumerable: true, get: function () { return data_structure_typed_1.BSTNode; } });
|
|
13
|
-
Object.defineProperty(exports, "BST", { enumerable: true, get: function () { return data_structure_typed_1.BST; } });
|
|
17
|
+
__exportStar(require("./bst"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bst-typed",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.45",
|
|
4
4
|
"description": "BST (Binary Search Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"typescript": "^4.9.5"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"data-structure-typed": "^1.19.
|
|
68
|
+
"data-structure-typed": "^1.19.4",
|
|
69
69
|
"zod": "^3.22.2"
|
|
70
70
|
}
|
|
71
71
|
}
|