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
|
@@ -33,27 +33,15 @@ class SkipList {
|
|
|
33
33
|
get head() {
|
|
34
34
|
return this._head;
|
|
35
35
|
}
|
|
36
|
-
set head(value) {
|
|
37
|
-
this._head = value;
|
|
38
|
-
}
|
|
39
36
|
get level() {
|
|
40
37
|
return this._level;
|
|
41
38
|
}
|
|
42
|
-
set level(value) {
|
|
43
|
-
this._level = value;
|
|
44
|
-
}
|
|
45
39
|
get maxLevel() {
|
|
46
40
|
return this._maxLevel;
|
|
47
41
|
}
|
|
48
|
-
set maxLevel(value) {
|
|
49
|
-
this._maxLevel = value;
|
|
50
|
-
}
|
|
51
42
|
get probability() {
|
|
52
43
|
return this._probability;
|
|
53
44
|
}
|
|
54
|
-
set probability(value) {
|
|
55
|
-
this._probability = value;
|
|
56
|
-
}
|
|
57
45
|
/**
|
|
58
46
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
59
47
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -61,7 +49,7 @@ class SkipList {
|
|
|
61
49
|
* List.
|
|
62
50
|
*/
|
|
63
51
|
add(key, value) {
|
|
64
|
-
const newNode = new SkipListNode(key, value, this.
|
|
52
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
65
53
|
const update = new Array(this.maxLevel).fill(this.head);
|
|
66
54
|
let current = this.head;
|
|
67
55
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
@@ -75,7 +63,7 @@ class SkipList {
|
|
|
75
63
|
update[i].forward[i] = newNode;
|
|
76
64
|
}
|
|
77
65
|
if (newNode.forward[0] !== null) {
|
|
78
|
-
this.
|
|
66
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
79
67
|
}
|
|
80
68
|
}
|
|
81
69
|
/**
|
|
@@ -97,6 +85,9 @@ class SkipList {
|
|
|
97
85
|
}
|
|
98
86
|
return undefined;
|
|
99
87
|
}
|
|
88
|
+
has(key) {
|
|
89
|
+
return this.get(key) !== undefined;
|
|
90
|
+
}
|
|
100
91
|
/**
|
|
101
92
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
102
93
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -121,17 +112,71 @@ class SkipList {
|
|
|
121
112
|
update[i].forward[i] = current.forward[i];
|
|
122
113
|
}
|
|
123
114
|
while (this.level > 0 && this.head.forward[this.level - 1] === null) {
|
|
124
|
-
this.
|
|
115
|
+
this._level--;
|
|
125
116
|
}
|
|
126
117
|
return true;
|
|
127
118
|
}
|
|
128
119
|
return false;
|
|
129
120
|
}
|
|
130
121
|
/**
|
|
131
|
-
*
|
|
122
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
123
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
124
|
+
*/
|
|
125
|
+
getFirst() {
|
|
126
|
+
const firstNode = this.head.forward[0];
|
|
127
|
+
return firstNode ? firstNode.value : undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
131
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
132
|
+
*/
|
|
133
|
+
getLast() {
|
|
134
|
+
let current = this.head;
|
|
135
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
136
|
+
while (current.forward[i]) {
|
|
137
|
+
current = current.forward[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return current.value;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
144
|
+
* @param key - the given key.
|
|
145
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
146
|
+
*/
|
|
147
|
+
higher(key) {
|
|
148
|
+
let current = this.head;
|
|
149
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
150
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
151
|
+
current = current.forward[i];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const nextNode = current.forward[0];
|
|
155
|
+
return nextNode ? nextNode.value : undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
159
|
+
* @param key - the given key.
|
|
160
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
161
|
+
*/
|
|
162
|
+
lower(key) {
|
|
163
|
+
let current = this.head;
|
|
164
|
+
let lastLess = null;
|
|
165
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
166
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
167
|
+
current = current.forward[i];
|
|
168
|
+
}
|
|
169
|
+
if (current.key < key) {
|
|
170
|
+
lastLess = current;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return lastLess ? lastLess.value : undefined;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
132
177
|
* @returns the level, which is a number.
|
|
133
178
|
*/
|
|
134
|
-
|
|
179
|
+
_randomLevel() {
|
|
135
180
|
let level = 1;
|
|
136
181
|
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
137
182
|
level++;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export declare class MatrixNTI2D<V = any> {
|
|
9
|
-
|
|
9
|
+
protected readonly _matrix: Array<Array<V>>;
|
|
10
10
|
/**
|
|
11
11
|
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
12
12
|
* given initial value or 0 if not provided.
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Vector2D } from './vector2d';
|
|
9
9
|
export declare class Matrix2D {
|
|
10
|
-
|
|
10
|
+
protected readonly _matrix: number[][];
|
|
11
11
|
/**
|
|
12
12
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
13
13
|
* or Vector2D object.
|
|
@@ -21,10 +21,10 @@ export declare class Character {
|
|
|
21
21
|
}
|
|
22
22
|
export declare class Navigator<T = number> {
|
|
23
23
|
onMove: (cur: [number, number]) => void;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
protected readonly _matrix: T[][];
|
|
25
|
+
protected readonly _cur: [number, number];
|
|
26
|
+
protected _character: Character;
|
|
27
|
+
protected readonly _VISITED: T;
|
|
28
28
|
/**
|
|
29
29
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
30
30
|
* in the matrix.
|
|
@@ -10,20 +10,19 @@ export declare class Deque<E = any> extends DoublyLinkedList<E> {
|
|
|
10
10
|
}
|
|
11
11
|
export declare class ObjectDeque<E = number> {
|
|
12
12
|
constructor(capacity?: number);
|
|
13
|
-
|
|
13
|
+
protected _nodes: {
|
|
14
|
+
[key: number]: E;
|
|
15
|
+
};
|
|
14
16
|
get nodes(): {
|
|
15
17
|
[p: number]: E;
|
|
16
18
|
};
|
|
17
|
-
|
|
19
|
+
protected _capacity: number;
|
|
18
20
|
get capacity(): number;
|
|
19
|
-
|
|
20
|
-
private _first;
|
|
21
|
+
protected _first: number;
|
|
21
22
|
get first(): number;
|
|
22
|
-
|
|
23
|
-
private _last;
|
|
23
|
+
protected _last: number;
|
|
24
24
|
get last(): number;
|
|
25
|
-
|
|
26
|
-
private _size;
|
|
25
|
+
protected _size: number;
|
|
27
26
|
get size(): number;
|
|
28
27
|
/**
|
|
29
28
|
* The "addFirst" function adds a value to the beginning of an array-like data structure.
|
|
@@ -69,13 +68,10 @@ export declare class ObjectDeque<E = number> {
|
|
|
69
68
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
70
69
|
*/
|
|
71
70
|
isEmpty(): boolean;
|
|
72
|
-
protected _seNodes(value: {
|
|
73
|
-
[p: number]: E;
|
|
74
|
-
}): void;
|
|
75
|
-
protected _setSize(value: number): void;
|
|
76
71
|
}
|
|
77
72
|
export declare class ArrayDeque<E> {
|
|
78
73
|
protected _nodes: E[];
|
|
74
|
+
get nodes(): E[];
|
|
79
75
|
get size(): number;
|
|
80
76
|
/**
|
|
81
77
|
* O(n) time complexity of adding at the beginning and the end
|
|
@@ -33,21 +33,12 @@ class ObjectDeque {
|
|
|
33
33
|
get capacity() {
|
|
34
34
|
return this._capacity;
|
|
35
35
|
}
|
|
36
|
-
set capacity(value) {
|
|
37
|
-
this._capacity = value;
|
|
38
|
-
}
|
|
39
36
|
get first() {
|
|
40
37
|
return this._first;
|
|
41
38
|
}
|
|
42
|
-
set first(value) {
|
|
43
|
-
this._first = value;
|
|
44
|
-
}
|
|
45
39
|
get last() {
|
|
46
40
|
return this._last;
|
|
47
41
|
}
|
|
48
|
-
set last(value) {
|
|
49
|
-
this._last = value;
|
|
50
|
-
}
|
|
51
42
|
get size() {
|
|
52
43
|
return this._size;
|
|
53
44
|
}
|
|
@@ -57,15 +48,15 @@ class ObjectDeque {
|
|
|
57
48
|
* structure.
|
|
58
49
|
*/
|
|
59
50
|
addFirst(value) {
|
|
60
|
-
if (this.
|
|
61
|
-
const mid = Math.floor(this.
|
|
51
|
+
if (this.size === 0) {
|
|
52
|
+
const mid = Math.floor(this.capacity / 2);
|
|
62
53
|
this._first = mid;
|
|
63
54
|
this._last = mid;
|
|
64
55
|
}
|
|
65
56
|
else {
|
|
66
57
|
this._first--;
|
|
67
58
|
}
|
|
68
|
-
this.
|
|
59
|
+
this.nodes[this.first] = value;
|
|
69
60
|
this._size++;
|
|
70
61
|
}
|
|
71
62
|
/**
|
|
@@ -73,15 +64,15 @@ class ObjectDeque {
|
|
|
73
64
|
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
74
65
|
*/
|
|
75
66
|
addLast(value) {
|
|
76
|
-
if (this.
|
|
77
|
-
const mid = Math.floor(this.
|
|
67
|
+
if (this.size === 0) {
|
|
68
|
+
const mid = Math.floor(this.capacity / 2);
|
|
78
69
|
this._first = mid;
|
|
79
70
|
this._last = mid;
|
|
80
71
|
}
|
|
81
72
|
else {
|
|
82
73
|
this._last++;
|
|
83
74
|
}
|
|
84
|
-
this.
|
|
75
|
+
this.nodes[this.last] = value;
|
|
85
76
|
this._size++;
|
|
86
77
|
}
|
|
87
78
|
/**
|
|
@@ -89,10 +80,10 @@ class ObjectDeque {
|
|
|
89
80
|
* @returns The value of the first element in the data structure.
|
|
90
81
|
*/
|
|
91
82
|
popFirst() {
|
|
92
|
-
if (!this.
|
|
83
|
+
if (!this.size)
|
|
93
84
|
return;
|
|
94
85
|
const value = this.getFirst();
|
|
95
|
-
delete this.
|
|
86
|
+
delete this.nodes[this.first];
|
|
96
87
|
this._first++;
|
|
97
88
|
this._size--;
|
|
98
89
|
return value;
|
|
@@ -102,18 +93,18 @@ class ObjectDeque {
|
|
|
102
93
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
94
|
*/
|
|
104
95
|
getFirst() {
|
|
105
|
-
if (this.
|
|
106
|
-
return this.
|
|
96
|
+
if (this.size)
|
|
97
|
+
return this.nodes[this.first];
|
|
107
98
|
}
|
|
108
99
|
/**
|
|
109
100
|
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
101
|
* @returns The value that was removed from the data structure.
|
|
111
102
|
*/
|
|
112
103
|
popLast() {
|
|
113
|
-
if (!this.
|
|
104
|
+
if (!this.size)
|
|
114
105
|
return;
|
|
115
106
|
const value = this.getLast();
|
|
116
|
-
delete this.
|
|
107
|
+
delete this.nodes[this.last];
|
|
117
108
|
this._last--;
|
|
118
109
|
this._size--;
|
|
119
110
|
return value;
|
|
@@ -123,8 +114,8 @@ class ObjectDeque {
|
|
|
123
114
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
115
|
*/
|
|
125
116
|
getLast() {
|
|
126
|
-
if (this.
|
|
127
|
-
return this.
|
|
117
|
+
if (this.size)
|
|
118
|
+
return this.nodes[this.last];
|
|
128
119
|
}
|
|
129
120
|
/**
|
|
130
121
|
* The get function returns the element at the specified index in an array-like data structure.
|
|
@@ -134,20 +125,14 @@ class ObjectDeque {
|
|
|
134
125
|
* index, `null` is returned.
|
|
135
126
|
*/
|
|
136
127
|
get(index) {
|
|
137
|
-
return this.
|
|
128
|
+
return this.nodes[this.first + index] || null;
|
|
138
129
|
}
|
|
139
130
|
/**
|
|
140
131
|
* The function checks if the size of a data structure is less than or equal to zero.
|
|
141
132
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
142
133
|
*/
|
|
143
134
|
isEmpty() {
|
|
144
|
-
return this.
|
|
145
|
-
}
|
|
146
|
-
_seNodes(value) {
|
|
147
|
-
this._nodes = value;
|
|
148
|
-
}
|
|
149
|
-
_setSize(value) {
|
|
150
|
-
this._size = value;
|
|
135
|
+
return this.size <= 0;
|
|
151
136
|
}
|
|
152
137
|
}
|
|
153
138
|
exports.ObjectDeque = ObjectDeque;
|
|
@@ -157,8 +142,11 @@ class ArrayDeque {
|
|
|
157
142
|
constructor() {
|
|
158
143
|
this._nodes = [];
|
|
159
144
|
}
|
|
145
|
+
get nodes() {
|
|
146
|
+
return this._nodes;
|
|
147
|
+
}
|
|
160
148
|
get size() {
|
|
161
|
-
return this.
|
|
149
|
+
return this.nodes.length;
|
|
162
150
|
}
|
|
163
151
|
/**
|
|
164
152
|
* O(n) time complexity of adding at the beginning and the end
|
|
@@ -169,7 +157,7 @@ class ArrayDeque {
|
|
|
169
157
|
* @returns The return value is the new length of the array after the value has been added.
|
|
170
158
|
*/
|
|
171
159
|
addLast(value) {
|
|
172
|
-
return this.
|
|
160
|
+
return this.nodes.push(value);
|
|
173
161
|
}
|
|
174
162
|
/**
|
|
175
163
|
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
@@ -177,7 +165,7 @@ class ArrayDeque {
|
|
|
177
165
|
*/
|
|
178
166
|
popLast() {
|
|
179
167
|
var _a;
|
|
180
|
-
return (_a = this.
|
|
168
|
+
return (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
181
169
|
}
|
|
182
170
|
/**
|
|
183
171
|
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
@@ -186,7 +174,7 @@ class ArrayDeque {
|
|
|
186
174
|
*/
|
|
187
175
|
popFirst() {
|
|
188
176
|
var _a;
|
|
189
|
-
return (_a = this.
|
|
177
|
+
return (_a = this.nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
190
178
|
}
|
|
191
179
|
/**
|
|
192
180
|
* O(n) time complexity of adding at the beginning and the end
|
|
@@ -198,7 +186,7 @@ class ArrayDeque {
|
|
|
198
186
|
* `value` at the beginning.
|
|
199
187
|
*/
|
|
200
188
|
addFirst(value) {
|
|
201
|
-
return this.
|
|
189
|
+
return this.nodes.unshift(value);
|
|
202
190
|
}
|
|
203
191
|
/**
|
|
204
192
|
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
@@ -207,7 +195,7 @@ class ArrayDeque {
|
|
|
207
195
|
*/
|
|
208
196
|
getFirst() {
|
|
209
197
|
var _a;
|
|
210
|
-
return (_a = this.
|
|
198
|
+
return (_a = this.nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
211
199
|
}
|
|
212
200
|
/**
|
|
213
201
|
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
@@ -215,7 +203,7 @@ class ArrayDeque {
|
|
|
215
203
|
*/
|
|
216
204
|
getLast() {
|
|
217
205
|
var _a;
|
|
218
|
-
return (_a = this.
|
|
206
|
+
return (_a = this.nodes[this.nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
219
207
|
}
|
|
220
208
|
/**
|
|
221
209
|
* O(1) time complexity of obtaining the value
|
|
@@ -229,7 +217,7 @@ class ArrayDeque {
|
|
|
229
217
|
*/
|
|
230
218
|
get(index) {
|
|
231
219
|
var _a;
|
|
232
|
-
return (_a = this.
|
|
220
|
+
return (_a = this.nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
233
221
|
}
|
|
234
222
|
/**
|
|
235
223
|
* The set function assigns a value to a specific index in an array.
|
|
@@ -240,7 +228,7 @@ class ArrayDeque {
|
|
|
240
228
|
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
241
229
|
*/
|
|
242
230
|
set(index, value) {
|
|
243
|
-
return (this.
|
|
231
|
+
return (this.nodes[index] = value);
|
|
244
232
|
}
|
|
245
233
|
/**
|
|
246
234
|
* The insert function adds a value at a specified index in an array.
|
|
@@ -253,7 +241,7 @@ class ArrayDeque {
|
|
|
253
241
|
* are being removed, an empty array will be returned.
|
|
254
242
|
*/
|
|
255
243
|
insert(index, value) {
|
|
256
|
-
return this.
|
|
244
|
+
return this.nodes.splice(index, 0, value);
|
|
257
245
|
}
|
|
258
246
|
/**
|
|
259
247
|
* The delete function removes an element from an array at a specified index.
|
|
@@ -262,7 +250,7 @@ class ArrayDeque {
|
|
|
262
250
|
* @returns The method is returning an array containing the removed element.
|
|
263
251
|
*/
|
|
264
252
|
delete(index) {
|
|
265
|
-
return this.
|
|
253
|
+
return this.nodes.splice(index, 1);
|
|
266
254
|
}
|
|
267
255
|
/**
|
|
268
256
|
* The function checks if an array called "_nodes" is empty.
|
|
@@ -270,7 +258,7 @@ class ArrayDeque {
|
|
|
270
258
|
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
271
259
|
*/
|
|
272
260
|
isEmpty() {
|
|
273
|
-
return this.
|
|
261
|
+
return this.nodes.length === 0;
|
|
274
262
|
}
|
|
275
263
|
}
|
|
276
264
|
exports.ArrayDeque = ArrayDeque;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
8
8
|
/**
|
|
9
9
|
* The enqueue function adds a value to the end of an array.
|
|
10
10
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -15,6 +15,11 @@ export declare class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
15
15
|
* @returns The method is returning the element at the front of the queue, or null if the queue is empty.
|
|
16
16
|
*/
|
|
17
17
|
dequeue(): E | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
21
|
+
*/
|
|
22
|
+
getFirst(): E | undefined;
|
|
18
23
|
/**
|
|
19
24
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
25
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
@@ -29,12 +34,10 @@ export declare class Queue<E = any> {
|
|
|
29
34
|
* initialized as an empty array.
|
|
30
35
|
*/
|
|
31
36
|
constructor(elements?: E[]);
|
|
32
|
-
|
|
37
|
+
protected _nodes: E[];
|
|
33
38
|
get nodes(): E[];
|
|
34
|
-
|
|
35
|
-
private _offset;
|
|
39
|
+
protected _offset: number;
|
|
36
40
|
get offset(): number;
|
|
37
|
-
set offset(value: number);
|
|
38
41
|
/**
|
|
39
42
|
* The size function returns the number of elements in an array.
|
|
40
43
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -61,6 +64,12 @@ export declare class Queue<E = any> {
|
|
|
61
64
|
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
|
|
62
65
|
*/
|
|
63
66
|
shift(): E | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
69
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
70
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
71
|
+
*/
|
|
72
|
+
getFirst(): E | undefined;
|
|
64
73
|
/**
|
|
65
74
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
66
75
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
@@ -73,6 +82,12 @@ export declare class Queue<E = any> {
|
|
|
73
82
|
* array is empty, it returns `null`.
|
|
74
83
|
*/
|
|
75
84
|
getLast(): E | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
87
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
88
|
+
* array is empty, it returns `null`.
|
|
89
|
+
*/
|
|
90
|
+
peekLast(): E | undefined;
|
|
76
91
|
/**
|
|
77
92
|
* The enqueue function adds a value to the end of a queue.
|
|
78
93
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Queue = exports.
|
|
3
|
+
exports.Queue = exports.LinkedListQueue = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
6
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
9
|
const linked_list_1 = require("../linked-list");
|
|
10
|
-
class
|
|
10
|
+
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
11
11
|
/**
|
|
12
12
|
* The enqueue function adds a value to the end of an array.
|
|
13
13
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -22,16 +22,23 @@ class SkipQueue extends linked_list_1.SinglyLinkedList {
|
|
|
22
22
|
dequeue() {
|
|
23
23
|
return this.shift();
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
+
*/
|
|
29
|
+
getFirst() {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
32
|
+
}
|
|
25
33
|
/**
|
|
26
34
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
35
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
36
|
*/
|
|
29
37
|
peek() {
|
|
30
|
-
|
|
31
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
|
|
38
|
+
return this.getFirst();
|
|
32
39
|
}
|
|
33
40
|
}
|
|
34
|
-
exports.
|
|
41
|
+
exports.LinkedListQueue = LinkedListQueue;
|
|
35
42
|
class Queue {
|
|
36
43
|
/**
|
|
37
44
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
@@ -46,15 +53,9 @@ class Queue {
|
|
|
46
53
|
get nodes() {
|
|
47
54
|
return this._nodes;
|
|
48
55
|
}
|
|
49
|
-
set nodes(value) {
|
|
50
|
-
this._nodes = value;
|
|
51
|
-
}
|
|
52
56
|
get offset() {
|
|
53
57
|
return this._offset;
|
|
54
58
|
}
|
|
55
|
-
set offset(value) {
|
|
56
|
-
this._offset = value;
|
|
57
|
-
}
|
|
58
59
|
/**
|
|
59
60
|
* The size function returns the number of elements in an array.
|
|
60
61
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -90,23 +91,31 @@ class Queue {
|
|
|
90
91
|
shift() {
|
|
91
92
|
if (this.size === 0)
|
|
92
93
|
return undefined;
|
|
93
|
-
const first = this.
|
|
94
|
-
this.
|
|
94
|
+
const first = this.getFirst();
|
|
95
|
+
this._offset += 1;
|
|
95
96
|
if (this.offset * 2 < this.nodes.length)
|
|
96
97
|
return first;
|
|
97
98
|
// only delete dequeued elements when reaching half size
|
|
98
99
|
// to decrease latency of shifting elements.
|
|
99
|
-
this.
|
|
100
|
-
this.
|
|
100
|
+
this._nodes = this.nodes.slice(this.offset);
|
|
101
|
+
this._offset = 0;
|
|
101
102
|
return first;
|
|
102
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
106
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
107
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
108
|
+
*/
|
|
109
|
+
getFirst() {
|
|
110
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
111
|
+
}
|
|
103
112
|
/**
|
|
104
113
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
105
114
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
106
115
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
107
116
|
*/
|
|
108
117
|
peek() {
|
|
109
|
-
return this.
|
|
118
|
+
return this.getFirst();
|
|
110
119
|
}
|
|
111
120
|
/**
|
|
112
121
|
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
@@ -116,6 +125,14 @@ class Queue {
|
|
|
116
125
|
getLast() {
|
|
117
126
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
118
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
130
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
131
|
+
* array is empty, it returns `null`.
|
|
132
|
+
*/
|
|
133
|
+
peekLast() {
|
|
134
|
+
return this.getLast();
|
|
135
|
+
}
|
|
119
136
|
/**
|
|
120
137
|
* The enqueue function adds a value to the end of a queue.
|
|
121
138
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -151,8 +168,8 @@ class Queue {
|
|
|
151
168
|
* The clear function resets the nodes array and offset to their initial values.
|
|
152
169
|
*/
|
|
153
170
|
clear() {
|
|
154
|
-
this.
|
|
155
|
-
this.
|
|
171
|
+
this._nodes = [];
|
|
172
|
+
this._offset = 0;
|
|
156
173
|
}
|
|
157
174
|
/**
|
|
158
175
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export declare class Stack<E = any> {
|
|
7
|
-
protected _elements: E[];
|
|
8
7
|
/**
|
|
9
8
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
10
9
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -12,6 +11,8 @@ export declare class Stack<E = any> {
|
|
|
12
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
13
12
|
*/
|
|
14
13
|
constructor(elements?: E[]);
|
|
14
|
+
protected _elements: E[];
|
|
15
|
+
get elements(): E[];
|
|
15
16
|
/**
|
|
16
17
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
17
18
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|