typescript-ds-lib 0.3.6 → 0.3.8
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 +12 -1
- package/dist/lib/binary-search-tree.d.ts +1 -2
- package/dist/lib/binary-search-tree.js +14 -14
- package/dist/lib/binary-search-tree.js.map +1 -1
- package/dist/lib/set.d.ts +4 -5
- package/dist/lib/set.js +12 -81
- package/dist/lib/set.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,7 +54,18 @@ See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master
|
|
|
54
54
|
- Stack
|
|
55
55
|
- Graph (coming soon)
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
|
|
58
|
+
## `Map-Set` vs Native JavaScript `Map-Set`
|
|
59
|
+
The library's `Map` and `Set` data structures are implemented as Red-Black Tree and Binary Search Tree respectively.
|
|
60
|
+
Native JavaScript `Map` and `Set` are implemented as Hash Table and Hash Set respectively.
|
|
61
|
+
|
|
62
|
+
When to use the library's `Map` and `Set`?
|
|
63
|
+
- If CPU consumption is important for you as RBT and BST do not do any kind of CPU intensive hashing.
|
|
64
|
+
- If your goal is to have a balanced tree with O(log n) complexity for all the operations in Map.
|
|
65
|
+
- If memory efficiency is important for you as RBT and BST are more memory efficient than Hash Table and HashSet.
|
|
66
|
+
|
|
67
|
+
You can consider the library's `Map` and `Set` as ordered map and set, and native JavaScript `Map` and `Set` as unordered map and set.
|
|
68
|
+
|
|
58
69
|
|
|
59
70
|
## Contributions
|
|
60
71
|
Contributions are welcome and can be made by submitting GitHub pull requests
|
|
@@ -7,11 +7,11 @@ export interface BinarySearchTree<T> {
|
|
|
7
7
|
min(): T | undefined;
|
|
8
8
|
max(): T | undefined;
|
|
9
9
|
forEach(callback: (element: T) => void, traversal?: 'inorder' | 'preorder' | 'postorder'): void;
|
|
10
|
-
count(): number;
|
|
11
10
|
}
|
|
12
11
|
export declare class BinarySearchTree<T> extends BaseCollection<T> implements BinarySearchTree<T> {
|
|
13
12
|
private root;
|
|
14
13
|
private comparator;
|
|
14
|
+
private nodeCount;
|
|
15
15
|
constructor(comparator?: Comparator<T>);
|
|
16
16
|
private removeNode;
|
|
17
17
|
private findMin;
|
|
@@ -31,5 +31,4 @@ export declare class BinarySearchTree<T> extends BaseCollection<T> implements Bi
|
|
|
31
31
|
* Returns the number of nodes in the BST.
|
|
32
32
|
*/
|
|
33
33
|
size(): number;
|
|
34
|
-
private countNodes;
|
|
35
34
|
}
|
|
@@ -15,25 +15,33 @@ class TreeNode {
|
|
|
15
15
|
class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
16
16
|
root;
|
|
17
17
|
comparator;
|
|
18
|
+
nodeCount;
|
|
18
19
|
constructor(comparator = (a, b) => a < b) {
|
|
19
20
|
super();
|
|
20
21
|
this.root = null;
|
|
21
22
|
this.comparator = comparator;
|
|
23
|
+
this.nodeCount = 0;
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
|
-
* Inserts a value into the BST
|
|
26
|
+
* Inserts a value into the BST if it doesn't already exist
|
|
25
27
|
*/
|
|
26
28
|
insert(value) {
|
|
27
29
|
const newNode = new TreeNode(value);
|
|
28
30
|
if (!this.root) {
|
|
29
31
|
this.root = newNode;
|
|
32
|
+
this.nodeCount++;
|
|
30
33
|
return;
|
|
31
34
|
}
|
|
32
35
|
let current = this.root;
|
|
33
36
|
while (true) {
|
|
37
|
+
if (this.isEqual(value, current.value)) {
|
|
38
|
+
// Value already exists, don't insert
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
34
41
|
if (this.comparator(value, current.value)) {
|
|
35
42
|
if (current.left === null) {
|
|
36
43
|
current.left = newNode;
|
|
44
|
+
this.nodeCount++;
|
|
37
45
|
break;
|
|
38
46
|
}
|
|
39
47
|
current = current.left;
|
|
@@ -41,6 +49,7 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
41
49
|
else {
|
|
42
50
|
if (current.right === null) {
|
|
43
51
|
current.right = newNode;
|
|
52
|
+
this.nodeCount++;
|
|
44
53
|
break;
|
|
45
54
|
}
|
|
46
55
|
current = current.right;
|
|
@@ -108,6 +117,7 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
108
117
|
}
|
|
109
118
|
else {
|
|
110
119
|
// Node to delete found
|
|
120
|
+
this.nodeCount--;
|
|
111
121
|
// Case 1: Leaf node
|
|
112
122
|
if (node.left === null && node.right === null) {
|
|
113
123
|
return null;
|
|
@@ -121,6 +131,7 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
121
131
|
const minNode = this.findMin(node.right);
|
|
122
132
|
node.value = minNode.value;
|
|
123
133
|
node.right = this.removeNode(node.right, minNode.value);
|
|
134
|
+
this.nodeCount++; // Increment back since the recursive call decremented
|
|
124
135
|
return node;
|
|
125
136
|
}
|
|
126
137
|
}
|
|
@@ -185,24 +196,13 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
185
196
|
*/
|
|
186
197
|
clear() {
|
|
187
198
|
this.root = null;
|
|
199
|
+
this.nodeCount = 0;
|
|
188
200
|
}
|
|
189
201
|
/**
|
|
190
202
|
* Returns the number of nodes in the BST.
|
|
191
203
|
*/
|
|
192
204
|
size() {
|
|
193
|
-
return this.
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Returns the number of nodes in the BST.
|
|
197
|
-
* TODO: I should remove this method and use size() instead.
|
|
198
|
-
*/
|
|
199
|
-
count() {
|
|
200
|
-
return this.countNodes(this.root);
|
|
201
|
-
}
|
|
202
|
-
countNodes(node) {
|
|
203
|
-
if (node === null)
|
|
204
|
-
return 0;
|
|
205
|
-
return 1 + this.countNodes(node.left) + this.countNodes(node.right);
|
|
205
|
+
return this.nodeCount;
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
exports.BinarySearchTree = BinarySearchTree;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binary-search-tree.js","sourceRoot":"","sources":["../../lib/binary-search-tree.ts"],"names":[],"mappings":";;;AACA,uDAAmD;
|
|
1
|
+
{"version":3,"file":"binary-search-tree.js","sourceRoot":"","sources":["../../lib/binary-search-tree.ts"],"names":[],"mappings":";;;AACA,uDAAmD;AAanD,MAAM,QAAQ;IACZ,KAAK,CAAI;IACT,IAAI,CAAqB;IACzB,KAAK,CAAqB;IAE1B,YAAY,KAAQ;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;CACF;AAGD,MAAa,gBAAoB,SAAQ,gCAAiB;IAChD,IAAI,CAAqB;IACzB,UAAU,CAAgB;IAC1B,SAAS,CAAS;IAE1B,YAAY,aAA4B,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC;QAC3D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAQ;QACb,MAAM,OAAO,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;YACf,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC;YACpB,IAAI,CAAC,SAAS,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QACD,IAAI,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC;QACxB,OAAO,IAAI,EAAE,CAAC;YACZ,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBACvC,qCAAqC;gBACrC,OAAO;YACT,CAAC;YACD,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK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ACjB,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;CACF;AApMD,4CAoMC"}
|
package/dist/lib/set.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { BaseCollection } from './base-collection';
|
|
2
|
+
import { Comparator } from '../types';
|
|
2
3
|
export interface Set<T> {
|
|
3
4
|
insert(element: T): void;
|
|
4
5
|
insertList(elements: T[]): void;
|
|
5
|
-
remove(element: T):
|
|
6
|
+
remove(element: T): void;
|
|
6
7
|
find(element: T): boolean;
|
|
7
8
|
has(element: T): boolean;
|
|
8
9
|
forEach(callback: (element: T) => void): void;
|
|
9
10
|
}
|
|
10
11
|
export declare class Set<T> extends BaseCollection<T> implements Set<T> {
|
|
11
|
-
private
|
|
12
|
-
|
|
13
|
-
private readonly capacity;
|
|
14
|
-
constructor(capacity?: number);
|
|
12
|
+
private tree;
|
|
13
|
+
constructor(comparator?: Comparator<T>);
|
|
15
14
|
/**
|
|
16
15
|
* Removes all elements from the set.
|
|
17
16
|
*/
|
package/dist/lib/set.js
CHANGED
|
@@ -2,72 +2,32 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Set = void 0;
|
|
4
4
|
const base_collection_1 = require("./base-collection");
|
|
5
|
-
const
|
|
6
|
-
class Node {
|
|
7
|
-
value;
|
|
8
|
-
next;
|
|
9
|
-
constructor(value) {
|
|
10
|
-
this.value = value;
|
|
11
|
-
this.next = null;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
5
|
+
const binary_search_tree_1 = require("./binary-search-tree");
|
|
14
6
|
class Set extends base_collection_1.BaseCollection {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
capacity;
|
|
18
|
-
constructor(capacity = 81920) {
|
|
7
|
+
tree;
|
|
8
|
+
constructor(comparator = (a, b) => a < b) {
|
|
19
9
|
super();
|
|
20
|
-
this.
|
|
21
|
-
this.table = new Array(this.capacity).fill(null);
|
|
22
|
-
this.count = 0;
|
|
10
|
+
this.tree = new binary_search_tree_1.BinarySearchTree(comparator);
|
|
23
11
|
}
|
|
24
12
|
/**
|
|
25
13
|
* Adds a value to the set if it's not already present.
|
|
26
|
-
*
|
|
27
|
-
* TODO: Dinamically resize the table if the collision factor is too high.
|
|
28
14
|
*/
|
|
29
15
|
insert(value) {
|
|
30
|
-
|
|
31
|
-
// Handle empty bucket case.
|
|
32
|
-
if (!this.table[index]) {
|
|
33
|
-
this.table[index] = new Node(value);
|
|
34
|
-
this.count++;
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
// Check first node for value match. If it matches, do nothing.
|
|
38
|
-
if (hash_utils_1.HashUtils.equals(this.table[index].value, value))
|
|
39
|
-
return;
|
|
40
|
-
// Traverse chain to find value or last node. If it matches, do nothing.
|
|
41
|
-
let current = this.table[index];
|
|
42
|
-
while (current?.next) {
|
|
43
|
-
if (hash_utils_1.HashUtils.equals(current.next.value, value))
|
|
44
|
-
return;
|
|
45
|
-
current = current.next;
|
|
46
|
-
}
|
|
47
|
-
// Value not found, append new node.
|
|
48
|
-
current.next = new Node(value);
|
|
49
|
-
this.count++;
|
|
16
|
+
this.tree.insert(value);
|
|
50
17
|
}
|
|
51
18
|
/**
|
|
52
19
|
* Adds multiple values to the set if they're not already present.
|
|
53
20
|
*/
|
|
54
21
|
insertList(values) {
|
|
55
22
|
for (const value of values) {
|
|
56
|
-
this.insert(value);
|
|
23
|
+
this.tree.insert(value);
|
|
57
24
|
}
|
|
58
25
|
}
|
|
59
26
|
/**
|
|
60
27
|
* Checks if a value exists in the set.
|
|
61
28
|
*/
|
|
62
29
|
find(value) {
|
|
63
|
-
|
|
64
|
-
let current = this.table[index];
|
|
65
|
-
while (current) {
|
|
66
|
-
if (hash_utils_1.HashUtils.equals(current.value, value))
|
|
67
|
-
return true;
|
|
68
|
-
current = current.next;
|
|
69
|
-
}
|
|
70
|
-
return false;
|
|
30
|
+
return this.tree.find(value);
|
|
71
31
|
}
|
|
72
32
|
has(value) {
|
|
73
33
|
return this.find(value);
|
|
@@ -76,57 +36,28 @@ class Set extends base_collection_1.BaseCollection {
|
|
|
76
36
|
* Removes a value from the set.
|
|
77
37
|
*/
|
|
78
38
|
remove(value) {
|
|
79
|
-
|
|
80
|
-
// Handle empty bucket case
|
|
81
|
-
if (!this.table[index])
|
|
82
|
-
return false;
|
|
83
|
-
// Handle first node case
|
|
84
|
-
if (hash_utils_1.HashUtils.equals(this.table[index].value, value)) {
|
|
85
|
-
this.table[index] = this.table[index].next;
|
|
86
|
-
this.count--;
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
// Handle remaining nodes
|
|
90
|
-
let current = this.table[index].next;
|
|
91
|
-
let prev = this.table[index];
|
|
92
|
-
while (current) {
|
|
93
|
-
if (hash_utils_1.HashUtils.equals(current.value, value)) {
|
|
94
|
-
prev.next = current.next;
|
|
95
|
-
this.count--;
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
prev = current;
|
|
99
|
-
current = current.next;
|
|
100
|
-
}
|
|
101
|
-
return false;
|
|
39
|
+
this.tree.remove(value);
|
|
102
40
|
}
|
|
103
41
|
forEach(callback) {
|
|
104
|
-
|
|
105
|
-
let current = node;
|
|
106
|
-
while (current) {
|
|
107
|
-
callback(current.value);
|
|
108
|
-
current = current.next;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
42
|
+
this.tree.forEach(callback);
|
|
111
43
|
}
|
|
112
44
|
/**
|
|
113
45
|
* Removes all elements from the set.
|
|
114
46
|
*/
|
|
115
47
|
clear() {
|
|
116
|
-
this.
|
|
117
|
-
this.count = 0;
|
|
48
|
+
this.tree = new binary_search_tree_1.BinarySearchTree();
|
|
118
49
|
}
|
|
119
50
|
/**
|
|
120
51
|
* Returns true if the set contains no elements.
|
|
121
52
|
*/
|
|
122
53
|
isEmpty() {
|
|
123
|
-
return this.
|
|
54
|
+
return this.tree.size() === 0;
|
|
124
55
|
}
|
|
125
56
|
/**
|
|
126
57
|
* Returns the number of elements in the set.
|
|
127
58
|
*/
|
|
128
59
|
size() {
|
|
129
|
-
return this.
|
|
60
|
+
return this.tree.size();
|
|
130
61
|
}
|
|
131
62
|
}
|
|
132
63
|
exports.Set = Set;
|
package/dist/lib/set.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"set.js","sourceRoot":"","sources":["../../lib/set.ts"],"names":[],"mappings":";;;AAAA,uDAAmD;AACnD,
|
|
1
|
+
{"version":3,"file":"set.js","sourceRoot":"","sources":["../../lib/set.ts"],"names":[],"mappings":";;;AAAA,uDAAmD;AACnD,6DAAwD;AAcxD,MAAa,GAAO,SAAQ,gCAAiB;IACnC,IAAI,CAAsB;IAElC,YAAY,aAA4B,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC;QAC3D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,qCAAgB,CAAI,UAAU,CAAC,CAAC;IAClD,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAQ;QACb,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,MAAW;QACpB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,KAAQ;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,GAAG,CAAC,KAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAQ;QACb,IAAI,CAAC,IAAI,CAAC,MA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