graph-typed 1.39.5 → 1.40.0
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/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +15 -27
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/interfaces/graph.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -15,11 +15,11 @@ class UndirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
|
15
15
|
* The constructor function initializes a vertex with an optional value.
|
|
16
16
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
17
17
|
* used to uniquely identify the vertex within a graph or network.
|
|
18
|
-
* @param {V} [
|
|
18
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It is used to initialize the value of the
|
|
19
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
20
|
*/
|
|
21
|
-
constructor(key,
|
|
22
|
-
super(key,
|
|
21
|
+
constructor(key, value) {
|
|
22
|
+
super(key, value);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.UndirectedVertex = UndirectedVertex;
|
|
@@ -31,18 +31,12 @@ class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
|
31
31
|
* @param {VertexKey} v2 - The parameter `v2` is a `VertexKey`, which represents the identifier of the second vertex in a
|
|
32
32
|
* graph edge.
|
|
33
33
|
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
34
|
-
* @param {E} [
|
|
34
|
+
* @param {E} [value] - The "value" parameter is an optional parameter of type E. It is used to store a value associated
|
|
35
35
|
* with the edge.
|
|
36
36
|
*/
|
|
37
|
-
constructor(v1, v2, weight,
|
|
38
|
-
super(weight,
|
|
39
|
-
this.
|
|
40
|
-
}
|
|
41
|
-
get vertices() {
|
|
42
|
-
return this._vertices;
|
|
43
|
-
}
|
|
44
|
-
set vertices(v) {
|
|
45
|
-
this._vertices = v;
|
|
37
|
+
constructor(v1, v2, weight, value) {
|
|
38
|
+
super(weight, value);
|
|
39
|
+
this.vertices = [v1, v2];
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
42
|
exports.UndirectedEdge = UndirectedEdge;
|
|
@@ -61,13 +55,13 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
61
55
|
* The function creates a new vertex with an optional value and returns it.
|
|
62
56
|
* @param {VertexKey} key - The `key` parameter is the unique identifier for the vertex. It is used to distinguish one
|
|
63
57
|
* vertex from another in the graph.
|
|
64
|
-
* @param [
|
|
58
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
65
59
|
* it will be used as the value of the vertex. If no value is provided, the `key` parameter will be used as the value of
|
|
66
60
|
* the vertex.
|
|
67
61
|
* @returns The method is returning a new instance of the `UndirectedVertex` class, casted as type `VO`.
|
|
68
62
|
*/
|
|
69
|
-
createVertex(key,
|
|
70
|
-
return new UndirectedVertex(key,
|
|
63
|
+
createVertex(key, value) {
|
|
64
|
+
return new UndirectedVertex(key, value !== null && value !== void 0 ? value : key);
|
|
71
65
|
}
|
|
72
66
|
/**
|
|
73
67
|
* The function creates an undirected edge between two vertices with an optional weight and value.
|
|
@@ -75,18 +69,18 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
75
69
|
* @param {VertexKey} v2 - The parameter `v2` represents the second vertex of the edge.
|
|
76
70
|
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
|
|
77
71
|
* no weight is provided, it defaults to 1.
|
|
78
|
-
* @param [
|
|
72
|
+
* @param [value] - The `value` parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
79
73
|
* is used to store additional information or data associated with the edge.
|
|
80
74
|
* @returns a new instance of the `UndirectedEdge` class, which is casted as type `EO`.
|
|
81
75
|
*/
|
|
82
|
-
createEdge(v1, v2, weight,
|
|
83
|
-
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1,
|
|
76
|
+
createEdge(v1, v2, weight, value) {
|
|
77
|
+
return new UndirectedEdge(v1, v2, weight !== null && weight !== void 0 ? weight : 1, value);
|
|
84
78
|
}
|
|
85
79
|
/**
|
|
86
80
|
* The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
|
|
87
|
-
* @param {VO |
|
|
81
|
+
* @param {VO | VertexKey | null} v1 - The parameter `v1` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
88
82
|
* object), `null`, or `VertexKey` (a string or number representing the ID of a vertex).
|
|
89
|
-
* @param {VO |
|
|
83
|
+
* @param {VO | VertexKey | null} v2 - The parameter `v2` represents a vertex or vertex ID. It can be of type `VO` (vertex
|
|
90
84
|
* object), `null`, or `VertexKey` (vertex ID).
|
|
91
85
|
* @returns an edge (EO) or null.
|
|
92
86
|
*/
|
|
@@ -241,12 +235,5 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
241
235
|
}
|
|
242
236
|
return true;
|
|
243
237
|
}
|
|
244
|
-
/**
|
|
245
|
-
* The function sets the edges of a graph.
|
|
246
|
-
* @param v - A map where the keys are of type VO and the values are arrays of type EO.
|
|
247
|
-
*/
|
|
248
|
-
_setEdges(v) {
|
|
249
|
-
this._edges = v;
|
|
250
|
-
}
|
|
251
238
|
}
|
|
252
239
|
exports.UndirectedGraph = UndirectedGraph;
|
|
@@ -20,24 +20,18 @@ export declare class HashMap<K, V> {
|
|
|
20
20
|
* default hash function converts the key to a string, calculates the sum of the
|
|
21
21
|
*/
|
|
22
22
|
constructor(initialCapacity?: number, loadFactor?: number, hashFn?: HashFunction<K>);
|
|
23
|
-
|
|
23
|
+
protected _initialCapacity: number;
|
|
24
24
|
get initialCapacity(): number;
|
|
25
|
-
|
|
26
|
-
private _loadFactor;
|
|
25
|
+
protected _loadFactor: number;
|
|
27
26
|
get loadFactor(): number;
|
|
28
|
-
|
|
29
|
-
private _capacityMultiplier;
|
|
27
|
+
protected _capacityMultiplier: number;
|
|
30
28
|
get capacityMultiplier(): number;
|
|
31
|
-
|
|
32
|
-
private _size;
|
|
29
|
+
protected _size: number;
|
|
33
30
|
get size(): number;
|
|
34
|
-
|
|
35
|
-
private _table;
|
|
31
|
+
protected _table: Array<Array<[K, V]>>;
|
|
36
32
|
get table(): Array<Array<[K, V]>>;
|
|
37
|
-
|
|
38
|
-
private _hashFn;
|
|
33
|
+
protected _hashFn: HashFunction<K>;
|
|
39
34
|
get hashFn(): HashFunction<K>;
|
|
40
|
-
set hashFn(value: HashFunction<K>);
|
|
41
35
|
set(key: K, value: V): void;
|
|
42
36
|
get(key: K): V | undefined;
|
|
43
37
|
delete(key: K): void;
|
|
@@ -45,12 +39,12 @@ export declare class HashMap<K, V> {
|
|
|
45
39
|
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
46
40
|
clear(): void;
|
|
47
41
|
isEmpty(): boolean;
|
|
48
|
-
|
|
42
|
+
protected _hash(key: K): number;
|
|
49
43
|
/**
|
|
50
44
|
* The `resizeTable` function resizes the table used in a hash map by creating a new table with a specified capacity and
|
|
51
45
|
* rehashing the key-value pairs from the old table into the new table.
|
|
52
46
|
* @param {number} newCapacity - The newCapacity parameter is the desired capacity for the resized table. It represents
|
|
53
47
|
* the number of buckets that the new table should have.
|
|
54
48
|
*/
|
|
55
|
-
|
|
49
|
+
protected resizeTable(newCapacity: number): void;
|
|
56
50
|
}
|
|
@@ -41,39 +41,21 @@ class HashMap {
|
|
|
41
41
|
get initialCapacity() {
|
|
42
42
|
return this._initialCapacity;
|
|
43
43
|
}
|
|
44
|
-
set initialCapacity(value) {
|
|
45
|
-
this._initialCapacity = value;
|
|
46
|
-
}
|
|
47
44
|
get loadFactor() {
|
|
48
45
|
return this._loadFactor;
|
|
49
46
|
}
|
|
50
|
-
set loadFactor(value) {
|
|
51
|
-
this._loadFactor = value;
|
|
52
|
-
}
|
|
53
47
|
get capacityMultiplier() {
|
|
54
48
|
return this._capacityMultiplier;
|
|
55
49
|
}
|
|
56
|
-
set capacityMultiplier(value) {
|
|
57
|
-
this._capacityMultiplier = value;
|
|
58
|
-
}
|
|
59
50
|
get size() {
|
|
60
51
|
return this._size;
|
|
61
52
|
}
|
|
62
|
-
set size(value) {
|
|
63
|
-
this._size = value;
|
|
64
|
-
}
|
|
65
53
|
get table() {
|
|
66
54
|
return this._table;
|
|
67
55
|
}
|
|
68
|
-
set table(value) {
|
|
69
|
-
this._table = value;
|
|
70
|
-
}
|
|
71
56
|
get hashFn() {
|
|
72
57
|
return this._hashFn;
|
|
73
58
|
}
|
|
74
|
-
set hashFn(value) {
|
|
75
|
-
this._hashFn = value;
|
|
76
|
-
}
|
|
77
59
|
set(key, value) {
|
|
78
60
|
const loadFactor = this.size / this.table.length;
|
|
79
61
|
if (loadFactor >= this.loadFactor) {
|
|
@@ -91,7 +73,7 @@ class HashMap {
|
|
|
91
73
|
}
|
|
92
74
|
}
|
|
93
75
|
this.table[index].push([key, value]);
|
|
94
|
-
this.
|
|
76
|
+
this._size++;
|
|
95
77
|
}
|
|
96
78
|
get(key) {
|
|
97
79
|
const index = this._hash(key);
|
|
@@ -113,7 +95,7 @@ class HashMap {
|
|
|
113
95
|
for (let i = 0; i < this.table[index].length; i++) {
|
|
114
96
|
if (this.table[index][i][0] === key) {
|
|
115
97
|
this.table[index].splice(i, 1);
|
|
116
|
-
this.
|
|
98
|
+
this._size--;
|
|
117
99
|
// Check if the table needs to be resized down
|
|
118
100
|
const loadFactor = this.size / this.table.length;
|
|
119
101
|
if (loadFactor < this.loadFactor / this.capacityMultiplier) {
|
|
@@ -136,8 +118,8 @@ class HashMap {
|
|
|
136
118
|
return this.entries();
|
|
137
119
|
}
|
|
138
120
|
clear() {
|
|
139
|
-
this.
|
|
140
|
-
this.
|
|
121
|
+
this._size = 0;
|
|
122
|
+
this._table = new Array(this.initialCapacity);
|
|
141
123
|
}
|
|
142
124
|
isEmpty() {
|
|
143
125
|
return this.size === 0;
|
|
@@ -7,36 +7,33 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class HashTableNode<K, V> {
|
|
9
9
|
key: K;
|
|
10
|
-
|
|
10
|
+
value: V;
|
|
11
11
|
next: HashTableNode<K, V> | null;
|
|
12
|
-
constructor(key: K,
|
|
12
|
+
constructor(key: K, value: V);
|
|
13
13
|
}
|
|
14
14
|
import { HashFunction } from '../../types';
|
|
15
15
|
export declare class HashTable<K, V> {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
protected static readonly DEFAULT_CAPACITY = 16;
|
|
17
|
+
protected static readonly LOAD_FACTOR = 0.75;
|
|
18
18
|
constructor(capacity?: number, hashFn?: HashFunction<K>);
|
|
19
|
-
|
|
19
|
+
protected _capacity: number;
|
|
20
20
|
get capacity(): number;
|
|
21
|
-
|
|
22
|
-
private _size;
|
|
21
|
+
protected _size: number;
|
|
23
22
|
get size(): number;
|
|
24
|
-
|
|
23
|
+
protected _buckets: Array<HashTableNode<K, V> | null>;
|
|
25
24
|
get buckets(): Array<HashTableNode<K, V> | null>;
|
|
26
|
-
|
|
27
|
-
private _hashFn;
|
|
25
|
+
protected _hashFn: HashFunction<K>;
|
|
28
26
|
get hashFn(): HashFunction<K>;
|
|
29
|
-
set hashFn(value: HashFunction<K>);
|
|
30
27
|
/**
|
|
31
28
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
32
29
|
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
33
30
|
* table. It is of type K, which is a generic type representing the key's data type.
|
|
34
|
-
* @param {V}
|
|
31
|
+
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
35
32
|
* table.
|
|
36
33
|
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
37
34
|
* value.
|
|
38
35
|
*/
|
|
39
|
-
set(key: K,
|
|
36
|
+
set(key: K, value: V): void;
|
|
40
37
|
/**
|
|
41
38
|
* The `get` function retrieves the value associated with a given key from a hash table.
|
|
42
39
|
* @param {K} key - The `key` parameter represents the key of the element that we want to retrieve from the data
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.HashTable = exports.HashTableNode = void 0;
|
|
11
11
|
class HashTableNode {
|
|
12
|
-
constructor(key,
|
|
12
|
+
constructor(key, value) {
|
|
13
13
|
this.key = key;
|
|
14
|
-
this.
|
|
14
|
+
this.value = value;
|
|
15
15
|
this.next = null;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -26,36 +26,27 @@ class HashTable {
|
|
|
26
26
|
get capacity() {
|
|
27
27
|
return this._capacity;
|
|
28
28
|
}
|
|
29
|
-
set capacity(value) {
|
|
30
|
-
this._capacity = value;
|
|
31
|
-
}
|
|
32
29
|
get size() {
|
|
33
30
|
return this._size;
|
|
34
31
|
}
|
|
35
32
|
get buckets() {
|
|
36
33
|
return this._buckets;
|
|
37
34
|
}
|
|
38
|
-
set buckets(value) {
|
|
39
|
-
this._buckets = value;
|
|
40
|
-
}
|
|
41
35
|
get hashFn() {
|
|
42
36
|
return this._hashFn;
|
|
43
37
|
}
|
|
44
|
-
set hashFn(value) {
|
|
45
|
-
this._hashFn = value;
|
|
46
|
-
}
|
|
47
38
|
/**
|
|
48
39
|
* The set function adds a key-value pair to the hash table, handling collisions and resizing if necessary.
|
|
49
40
|
* @param {K} key - The key parameter represents the key of the key-value pair that you want to insert into the hash
|
|
50
41
|
* table. It is of type K, which is a generic type representing the key's data type.
|
|
51
|
-
* @param {V}
|
|
42
|
+
* @param {V} value - The parameter `value` represents the value that you want to associate with the given key in the hash
|
|
52
43
|
* table.
|
|
53
44
|
* @returns Nothing is being returned. The return type of the `put` method is `void`, which means it does not return any
|
|
54
45
|
* value.
|
|
55
46
|
*/
|
|
56
|
-
set(key,
|
|
47
|
+
set(key, value) {
|
|
57
48
|
const index = this._hash(key);
|
|
58
|
-
const newNode = new HashTableNode(key,
|
|
49
|
+
const newNode = new HashTableNode(key, value);
|
|
59
50
|
if (!this._buckets[index]) {
|
|
60
51
|
this._buckets[index] = newNode;
|
|
61
52
|
}
|
|
@@ -65,7 +56,7 @@ class HashTable {
|
|
|
65
56
|
while (currentNode) {
|
|
66
57
|
if (currentNode.key === key) {
|
|
67
58
|
// If the key already exists, update the value
|
|
68
|
-
currentNode.
|
|
59
|
+
currentNode.value = value;
|
|
69
60
|
return;
|
|
70
61
|
}
|
|
71
62
|
if (!currentNode.next) {
|
|
@@ -94,7 +85,7 @@ class HashTable {
|
|
|
94
85
|
let currentNode = this._buckets[index];
|
|
95
86
|
while (currentNode) {
|
|
96
87
|
if (currentNode.key === key) {
|
|
97
|
-
return currentNode.
|
|
88
|
+
return currentNode.value;
|
|
98
89
|
}
|
|
99
90
|
currentNode = currentNode.next;
|
|
100
91
|
}
|
|
@@ -222,7 +213,7 @@ class HashTable {
|
|
|
222
213
|
let currentNode = bucket;
|
|
223
214
|
while (currentNode) {
|
|
224
215
|
const newIndex = this._hash(currentNode.key);
|
|
225
|
-
const newNode = new HashTableNode(currentNode.key, currentNode.
|
|
216
|
+
const newNode = new HashTableNode(currentNode.key, currentNode.value);
|
|
226
217
|
if (!newBuckets[newIndex]) {
|
|
227
218
|
newBuckets[newIndex] = newNode;
|
|
228
219
|
}
|
|
@@ -6,12 +6,14 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
8
|
export declare class Heap<E = any> {
|
|
9
|
-
protected nodes: E[];
|
|
10
|
-
protected readonly comparator: Comparator<E>;
|
|
11
9
|
constructor(options: {
|
|
12
10
|
comparator: Comparator<E>;
|
|
13
11
|
nodes?: E[];
|
|
14
12
|
});
|
|
13
|
+
protected _nodes: E[];
|
|
14
|
+
get nodes(): E[];
|
|
15
|
+
protected _comparator: Comparator<E>;
|
|
16
|
+
get comparator(): Comparator<E>;
|
|
15
17
|
/**
|
|
16
18
|
* Get the size (number of elements) of the heap.
|
|
17
19
|
*/
|
|
@@ -123,11 +125,15 @@ export declare class FibonacciHeapNode<E> {
|
|
|
123
125
|
constructor(element: E, degree?: number);
|
|
124
126
|
}
|
|
125
127
|
export declare class FibonacciHeap<E> {
|
|
126
|
-
root?: FibonacciHeapNode<E>;
|
|
127
|
-
size: number;
|
|
128
|
-
protected min?: FibonacciHeapNode<E>;
|
|
129
|
-
protected readonly comparator: Comparator<E>;
|
|
130
128
|
constructor(comparator?: Comparator<E>);
|
|
129
|
+
protected _root?: FibonacciHeapNode<E>;
|
|
130
|
+
get root(): FibonacciHeapNode<E> | undefined;
|
|
131
|
+
protected _size: number;
|
|
132
|
+
get size(): number;
|
|
133
|
+
protected _min?: FibonacciHeapNode<E>;
|
|
134
|
+
get min(): FibonacciHeapNode<E> | undefined;
|
|
135
|
+
protected _comparator: Comparator<E>;
|
|
136
|
+
get comparator(): Comparator<E>;
|
|
131
137
|
/**
|
|
132
138
|
* Get the size (number of elements) of the heap.
|
|
133
139
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
@@ -9,13 +9,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
10
|
class Heap {
|
|
11
11
|
constructor(options) {
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
12
|
+
this._nodes = [];
|
|
13
|
+
this._comparator = options.comparator;
|
|
14
14
|
if (options.nodes && options.nodes.length > 0) {
|
|
15
|
-
this.
|
|
15
|
+
this._nodes = options.nodes;
|
|
16
16
|
this.fix();
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
+
get nodes() {
|
|
20
|
+
return this._nodes;
|
|
21
|
+
}
|
|
22
|
+
get comparator() {
|
|
23
|
+
return this._comparator;
|
|
24
|
+
}
|
|
19
25
|
/**
|
|
20
26
|
* Get the size (number of elements) of the heap.
|
|
21
27
|
*/
|
|
@@ -98,14 +104,14 @@ class Heap {
|
|
|
98
104
|
* Reset the nodes of the heap. Make the nodes empty.
|
|
99
105
|
*/
|
|
100
106
|
clear() {
|
|
101
|
-
this.
|
|
107
|
+
this._nodes = [];
|
|
102
108
|
}
|
|
103
109
|
/**
|
|
104
110
|
* Clear and add nodes of the heap
|
|
105
111
|
* @param nodes
|
|
106
112
|
*/
|
|
107
113
|
refill(nodes) {
|
|
108
|
-
this.
|
|
114
|
+
this._nodes = nodes;
|
|
109
115
|
this.fix();
|
|
110
116
|
}
|
|
111
117
|
/**
|
|
@@ -162,7 +168,7 @@ class Heap {
|
|
|
162
168
|
*/
|
|
163
169
|
clone() {
|
|
164
170
|
const clonedHeap = new Heap({ comparator: this.comparator });
|
|
165
|
-
clonedHeap.
|
|
171
|
+
clonedHeap._nodes = [...this.nodes];
|
|
166
172
|
return clonedHeap;
|
|
167
173
|
}
|
|
168
174
|
/**
|
|
@@ -239,21 +245,33 @@ class FibonacciHeapNode {
|
|
|
239
245
|
exports.FibonacciHeapNode = FibonacciHeapNode;
|
|
240
246
|
class FibonacciHeap {
|
|
241
247
|
constructor(comparator) {
|
|
242
|
-
this.
|
|
248
|
+
this._size = 0;
|
|
243
249
|
this.clear();
|
|
244
|
-
this.
|
|
250
|
+
this._comparator = comparator || this.defaultComparator;
|
|
245
251
|
if (typeof this.comparator !== 'function') {
|
|
246
252
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
247
253
|
}
|
|
248
254
|
}
|
|
255
|
+
get root() {
|
|
256
|
+
return this._root;
|
|
257
|
+
}
|
|
258
|
+
get size() {
|
|
259
|
+
return this._size;
|
|
260
|
+
}
|
|
261
|
+
get min() {
|
|
262
|
+
return this._min;
|
|
263
|
+
}
|
|
264
|
+
get comparator() {
|
|
265
|
+
return this._comparator;
|
|
266
|
+
}
|
|
249
267
|
/**
|
|
250
268
|
* Get the size (number of elements) of the heap.
|
|
251
269
|
* @returns {number} The size of the heap. Returns 0 if the heap is empty. Returns -1 if the heap is invalid.
|
|
252
270
|
*/
|
|
253
271
|
clear() {
|
|
254
|
-
this.
|
|
255
|
-
this.
|
|
256
|
-
this.
|
|
272
|
+
this._root = undefined;
|
|
273
|
+
this._min = undefined;
|
|
274
|
+
this._size = 0;
|
|
257
275
|
}
|
|
258
276
|
/**
|
|
259
277
|
* O(1) time operation.
|
|
@@ -276,9 +294,9 @@ class FibonacciHeap {
|
|
|
276
294
|
node.right = node;
|
|
277
295
|
this.mergeWithRoot(node);
|
|
278
296
|
if (!this.min || this.comparator(node.element, this.min.element) <= 0) {
|
|
279
|
-
this.
|
|
297
|
+
this._min = node;
|
|
280
298
|
}
|
|
281
|
-
this.
|
|
299
|
+
this._size++;
|
|
282
300
|
return this;
|
|
283
301
|
}
|
|
284
302
|
/**
|
|
@@ -358,14 +376,14 @@ class FibonacciHeap {
|
|
|
358
376
|
}
|
|
359
377
|
this.removeFromRoot(z);
|
|
360
378
|
if (z === z.right) {
|
|
361
|
-
this.
|
|
362
|
-
this.
|
|
379
|
+
this._min = undefined;
|
|
380
|
+
this._root = undefined;
|
|
363
381
|
}
|
|
364
382
|
else {
|
|
365
|
-
this.
|
|
383
|
+
this._min = z.right;
|
|
366
384
|
this.consolidate();
|
|
367
385
|
}
|
|
368
|
-
this.
|
|
386
|
+
this._size--;
|
|
369
387
|
return z.element;
|
|
370
388
|
}
|
|
371
389
|
/**
|
|
@@ -390,10 +408,10 @@ class FibonacciHeap {
|
|
|
390
408
|
}
|
|
391
409
|
// Update the minimum node
|
|
392
410
|
if (!this.min || (heapToMerge.min && this.comparator(heapToMerge.min.element, this.min.element) < 0)) {
|
|
393
|
-
this.
|
|
411
|
+
this._min = heapToMerge.min;
|
|
394
412
|
}
|
|
395
413
|
// Update the size
|
|
396
|
-
this.
|
|
414
|
+
this._size += heapToMerge.size;
|
|
397
415
|
// Clear the heap that was merged
|
|
398
416
|
heapToMerge.clear();
|
|
399
417
|
}
|
|
@@ -424,7 +442,7 @@ class FibonacciHeap {
|
|
|
424
442
|
*/
|
|
425
443
|
mergeWithRoot(node) {
|
|
426
444
|
if (!this.root) {
|
|
427
|
-
this.
|
|
445
|
+
this._root = node;
|
|
428
446
|
}
|
|
429
447
|
else {
|
|
430
448
|
node.right = this.root.right;
|
|
@@ -441,7 +459,7 @@ class FibonacciHeap {
|
|
|
441
459
|
*/
|
|
442
460
|
removeFromRoot(node) {
|
|
443
461
|
if (this.root === node)
|
|
444
|
-
this.
|
|
462
|
+
this._root = node.right;
|
|
445
463
|
if (node.left)
|
|
446
464
|
node.left.right = node.right;
|
|
447
465
|
if (node.right)
|
|
@@ -489,7 +507,7 @@ class FibonacciHeap {
|
|
|
489
507
|
}
|
|
490
508
|
for (let i = 0; i < this.size; i++) {
|
|
491
509
|
if (A[i] && this.comparator(A[i].element, this.min.element) <= 0) {
|
|
492
|
-
this.
|
|
510
|
+
this._min = A[i];
|
|
493
511
|
}
|
|
494
512
|
}
|
|
495
513
|
}
|