bst-typed 1.53.5 → 1.53.7
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 +57 -3
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +49 -34
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +144 -62
- package/dist/data-structures/linked-list/singly-linked-list.js +201 -97
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +139 -121
- package/src/data-structures/linked-list/singly-linked-list.ts +219 -98
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -99,7 +99,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
99
99
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
100
100
|
*/
|
|
101
101
|
createTree(options) {
|
|
102
|
-
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
102
|
+
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn }, options));
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
105
|
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
@@ -126,7 +126,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
126
126
|
if (this.isKey(key))
|
|
127
127
|
return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
128
128
|
}
|
|
129
|
-
if (this.
|
|
129
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
130
130
|
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
131
131
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
132
132
|
if (this.isKey(key))
|
|
@@ -19,7 +19,7 @@ import { IterableElementBase } from '../base';
|
|
|
19
19
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
20
20
|
* @example
|
|
21
21
|
* // Use Heap to sort an array
|
|
22
|
-
*
|
|
22
|
+
* function heapSort(arr: number[]): number[] {
|
|
23
23
|
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
24
24
|
* const sorted: number[] = [];
|
|
25
25
|
* while (!heap.isEmpty()) {
|
|
@@ -32,7 +32,7 @@ import { IterableElementBase } from '../base';
|
|
|
32
32
|
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
33
33
|
* @example
|
|
34
34
|
* // Use Heap to solve top k problems
|
|
35
|
-
*
|
|
35
|
+
* function topKElements(arr: number[], k: number): number[] {
|
|
36
36
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
37
37
|
* arr.forEach(num => {
|
|
38
38
|
* heap.add(num);
|
|
@@ -45,7 +45,7 @@ import { IterableElementBase } from '../base';
|
|
|
45
45
|
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
46
46
|
* @example
|
|
47
47
|
* // Use Heap to merge sorted sequences
|
|
48
|
-
*
|
|
48
|
+
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
49
49
|
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
50
50
|
* comparator: (a, b) => a.value - b.value // Min heap
|
|
51
51
|
* });
|
|
@@ -82,7 +82,7 @@ import { IterableElementBase } from '../base';
|
|
|
82
82
|
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
83
83
|
* @example
|
|
84
84
|
* // Use Heap to dynamically maintain the median
|
|
85
|
-
*
|
|
85
|
+
* class MedianFinder {
|
|
86
86
|
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
87
87
|
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
88
88
|
*
|
|
@@ -119,7 +119,7 @@ import { IterableElementBase } from '../base';
|
|
|
119
119
|
* console.log(medianFinder.findMedian()); // 30
|
|
120
120
|
* @example
|
|
121
121
|
* // Use Heap for load balancing
|
|
122
|
-
*
|
|
122
|
+
* function loadBalance(requests: number[], servers: number): number[] {
|
|
123
123
|
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
124
124
|
* const serverLoads = new Array(servers).fill(0);
|
|
125
125
|
*
|
|
@@ -141,7 +141,7 @@ import { IterableElementBase } from '../base';
|
|
|
141
141
|
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
142
142
|
* @example
|
|
143
143
|
* // Use Heap to schedule tasks
|
|
144
|
-
*
|
|
144
|
+
* type Task = [string, number];
|
|
145
145
|
*
|
|
146
146
|
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
147
147
|
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
@@ -21,7 +21,7 @@ const base_1 = require("../base");
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
* @example
|
|
23
23
|
* // Use Heap to sort an array
|
|
24
|
-
*
|
|
24
|
+
* function heapSort(arr: number[]): number[] {
|
|
25
25
|
* const heap = new Heap<number>(arr, { comparator: (a, b) => a - b });
|
|
26
26
|
* const sorted: number[] = [];
|
|
27
27
|
* while (!heap.isEmpty()) {
|
|
@@ -34,7 +34,7 @@ const base_1 = require("../base");
|
|
|
34
34
|
* console.log(heapSort(array)); // [1, 2, 3, 4, 5, 8]
|
|
35
35
|
* @example
|
|
36
36
|
* // Use Heap to solve top k problems
|
|
37
|
-
*
|
|
37
|
+
* function topKElements(arr: number[], k: number): number[] {
|
|
38
38
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
39
39
|
* arr.forEach(num => {
|
|
40
40
|
* heap.add(num);
|
|
@@ -47,7 +47,7 @@ const base_1 = require("../base");
|
|
|
47
47
|
* console.log(topKElements(numbers, 3)); // [15, 10, 5]
|
|
48
48
|
* @example
|
|
49
49
|
* // Use Heap to merge sorted sequences
|
|
50
|
-
*
|
|
50
|
+
* function mergeSortedSequences(sequences: number[][]): number[] {
|
|
51
51
|
* const heap = new Heap<{ value: number; seqIndex: number; itemIndex: number }>([], {
|
|
52
52
|
* comparator: (a, b) => a.value - b.value // Min heap
|
|
53
53
|
* });
|
|
@@ -84,7 +84,7 @@ const base_1 = require("../base");
|
|
|
84
84
|
* console.log(mergeSortedSequences(sequences)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
85
85
|
* @example
|
|
86
86
|
* // Use Heap to dynamically maintain the median
|
|
87
|
-
*
|
|
87
|
+
* class MedianFinder {
|
|
88
88
|
* private low: MaxHeap<number>; // Max heap, stores the smaller half
|
|
89
89
|
* private high: MinHeap<number>; // Min heap, stores the larger half
|
|
90
90
|
*
|
|
@@ -121,7 +121,7 @@ const base_1 = require("../base");
|
|
|
121
121
|
* console.log(medianFinder.findMedian()); // 30
|
|
122
122
|
* @example
|
|
123
123
|
* // Use Heap for load balancing
|
|
124
|
-
*
|
|
124
|
+
* function loadBalance(requests: number[], servers: number): number[] {
|
|
125
125
|
* const serverHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // min heap
|
|
126
126
|
* const serverLoads = new Array(servers).fill(0);
|
|
127
127
|
*
|
|
@@ -143,7 +143,7 @@ const base_1 = require("../base");
|
|
|
143
143
|
* console.log(loadBalance(requests, 3)); // [12, 8, 5]
|
|
144
144
|
* @example
|
|
145
145
|
* // Use Heap to schedule tasks
|
|
146
|
-
*
|
|
146
|
+
* type Task = [string, number];
|
|
147
147
|
*
|
|
148
148
|
* function scheduleTasks(tasks: Task[], machines: number): Map<number, Task[]> {
|
|
149
149
|
* const machineHeap = new Heap<{ id: number; load: number }>([], { comparator: (a, b) => a.load - b.load }); // Min heap
|
|
@@ -55,13 +55,13 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
55
55
|
set prev(value: DoublyLinkedListNode<E> | undefined);
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
59
59
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
60
60
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
61
61
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
62
62
|
* @example
|
|
63
63
|
* // text editor operation history
|
|
64
|
-
*
|
|
64
|
+
* const actions = [
|
|
65
65
|
* { type: 'insert', content: 'first line of text' },
|
|
66
66
|
* { type: 'insert', content: 'second line of text' },
|
|
67
67
|
* { type: 'delete', content: 'delete the first line' }
|
|
@@ -73,7 +73,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
73
73
|
* console.log(editorHistory.last?.type); // 'insert'
|
|
74
74
|
* @example
|
|
75
75
|
* // Browser history
|
|
76
|
-
*
|
|
76
|
+
* const browserHistory = new DoublyLinkedList<string>();
|
|
77
77
|
*
|
|
78
78
|
* browserHistory.push('home page');
|
|
79
79
|
* browserHistory.push('search page');
|
|
@@ -84,7 +84,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
84
84
|
* console.log(browserHistory.last); // 'search page'
|
|
85
85
|
* @example
|
|
86
86
|
* // Use DoublyLinkedList to implement music player
|
|
87
|
-
*
|
|
87
|
+
* // Define the Song interface
|
|
88
88
|
* interface Song {
|
|
89
89
|
* title: string;
|
|
90
90
|
* artist: string;
|
|
@@ -209,7 +209,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
209
209
|
* // ]
|
|
210
210
|
* @example
|
|
211
211
|
* // Use DoublyLinkedList to implement LRU cache
|
|
212
|
-
*
|
|
212
|
+
* interface CacheEntry<K, V> {
|
|
213
213
|
* key: K;
|
|
214
214
|
* value: V;
|
|
215
215
|
* }
|
|
@@ -371,7 +371,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
371
371
|
* console.log(cache.isEmpty); // true
|
|
372
372
|
* @example
|
|
373
373
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
374
|
-
*
|
|
374
|
+
* // Create a DoublyLinkedList to store song lyrics with timestamps
|
|
375
375
|
* const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
|
|
376
376
|
*
|
|
377
377
|
* // Detailed lyrics with precise timestamps (in milliseconds)
|
|
@@ -411,7 +411,7 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
411
411
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
412
412
|
* @example
|
|
413
413
|
* // cpu process schedules
|
|
414
|
-
*
|
|
414
|
+
* class Process {
|
|
415
415
|
* constructor(
|
|
416
416
|
* public id: number,
|
|
417
417
|
* public priority: number
|
|
@@ -487,6 +487,16 @@ export declare class DoublyLinkedListNode<E = any> {
|
|
|
487
487
|
* console.log(scheduler.listProcesses()); // []
|
|
488
488
|
*/
|
|
489
489
|
export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
|
|
490
|
+
/**
|
|
491
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
492
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
493
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
494
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
495
|
+
* empty iterable.
|
|
496
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
497
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
498
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
499
|
+
*/
|
|
490
500
|
constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);
|
|
491
501
|
protected _head: DoublyLinkedListNode<E> | undefined;
|
|
492
502
|
/**
|
|
@@ -709,22 +719,18 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
709
719
|
* Time Complexity: O(n)
|
|
710
720
|
* Space Complexity: O(1)
|
|
711
721
|
*
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
719
|
-
* node. If the element or node is not found in the list, the method returns -1.
|
|
722
|
+
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
723
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
724
|
+
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
725
|
+
* can be one of the following:
|
|
726
|
+
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
727
|
+
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
720
728
|
*/
|
|
721
|
-
indexOf(
|
|
729
|
+
indexOf(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
|
|
722
730
|
/**
|
|
723
731
|
* Time Complexity: O(n)
|
|
724
732
|
* Space Complexity: O(1)
|
|
725
733
|
*
|
|
726
|
-
*/
|
|
727
|
-
/**
|
|
728
734
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
729
735
|
* node or predicate.
|
|
730
736
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -733,7 +739,7 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
733
739
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
734
740
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
735
741
|
*/
|
|
736
|
-
|
|
742
|
+
search(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
|
|
737
743
|
/**
|
|
738
744
|
* Time Complexity: O(n)
|
|
739
745
|
* Space Complexity: O(1)
|
|
@@ -820,6 +826,12 @@ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementB
|
|
|
820
826
|
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
821
827
|
*/
|
|
822
828
|
map<EM, RM>(callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
|
|
829
|
+
/**
|
|
830
|
+
* Time Complexity: O(n)
|
|
831
|
+
* Space Complexity: O(1)
|
|
832
|
+
*
|
|
833
|
+
*/
|
|
834
|
+
countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
|
|
823
835
|
/**
|
|
824
836
|
* Time Complexity: O(n)
|
|
825
837
|
* Space Complexity: O(n)
|
|
@@ -64,13 +64,13 @@ class DoublyLinkedListNode {
|
|
|
64
64
|
}
|
|
65
65
|
exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
66
66
|
/**
|
|
67
|
-
*
|
|
67
|
+
*1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
|
68
68
|
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
|
69
69
|
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
|
70
70
|
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
|
71
71
|
* @example
|
|
72
72
|
* // text editor operation history
|
|
73
|
-
*
|
|
73
|
+
* const actions = [
|
|
74
74
|
* { type: 'insert', content: 'first line of text' },
|
|
75
75
|
* { type: 'insert', content: 'second line of text' },
|
|
76
76
|
* { type: 'delete', content: 'delete the first line' }
|
|
@@ -82,7 +82,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
82
82
|
* console.log(editorHistory.last?.type); // 'insert'
|
|
83
83
|
* @example
|
|
84
84
|
* // Browser history
|
|
85
|
-
*
|
|
85
|
+
* const browserHistory = new DoublyLinkedList<string>();
|
|
86
86
|
*
|
|
87
87
|
* browserHistory.push('home page');
|
|
88
88
|
* browserHistory.push('search page');
|
|
@@ -93,7 +93,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
93
93
|
* console.log(browserHistory.last); // 'search page'
|
|
94
94
|
* @example
|
|
95
95
|
* // Use DoublyLinkedList to implement music player
|
|
96
|
-
*
|
|
96
|
+
* // Define the Song interface
|
|
97
97
|
* interface Song {
|
|
98
98
|
* title: string;
|
|
99
99
|
* artist: string;
|
|
@@ -218,7 +218,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
218
218
|
* // ]
|
|
219
219
|
* @example
|
|
220
220
|
* // Use DoublyLinkedList to implement LRU cache
|
|
221
|
-
*
|
|
221
|
+
* interface CacheEntry<K, V> {
|
|
222
222
|
* key: K;
|
|
223
223
|
* value: V;
|
|
224
224
|
* }
|
|
@@ -380,7 +380,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
380
380
|
* console.log(cache.isEmpty); // true
|
|
381
381
|
* @example
|
|
382
382
|
* // finding lyrics by timestamp in Coldplay's "Fix You"
|
|
383
|
-
*
|
|
383
|
+
* // Create a DoublyLinkedList to store song lyrics with timestamps
|
|
384
384
|
* const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
|
|
385
385
|
*
|
|
386
386
|
* // Detailed lyrics with precise timestamps (in milliseconds)
|
|
@@ -420,7 +420,7 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
420
420
|
* console.log(lateTimeLyric?.text); // 'And I will try to fix you'
|
|
421
421
|
* @example
|
|
422
422
|
* // cpu process schedules
|
|
423
|
-
*
|
|
423
|
+
* class Process {
|
|
424
424
|
* constructor(
|
|
425
425
|
* public id: number,
|
|
426
426
|
* public priority: number
|
|
@@ -496,6 +496,16 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
|
|
|
496
496
|
* console.log(scheduler.listProcesses()); // []
|
|
497
497
|
*/
|
|
498
498
|
class DoublyLinkedList extends base_1.IterableElementBase {
|
|
499
|
+
/**
|
|
500
|
+
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
501
|
+
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
502
|
+
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
503
|
+
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
504
|
+
* empty iterable.
|
|
505
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
506
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
507
|
+
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
508
|
+
*/
|
|
499
509
|
constructor(elements = [], options) {
|
|
500
510
|
super(options);
|
|
501
511
|
this._head = undefined;
|
|
@@ -784,13 +794,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
784
794
|
* node was not found.
|
|
785
795
|
*/
|
|
786
796
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
else {
|
|
792
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
793
|
-
}
|
|
797
|
+
const existingNode = this.isNode(existingElementOrNode)
|
|
798
|
+
? existingElementOrNode
|
|
799
|
+
: this.getNode(existingElementOrNode);
|
|
794
800
|
if (existingNode) {
|
|
795
801
|
const newNode = this._ensureNode(newElementOrNode);
|
|
796
802
|
newNode.prev = existingNode.prev;
|
|
@@ -824,13 +830,9 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
824
830
|
* was not found in the linked list.
|
|
825
831
|
*/
|
|
826
832
|
addAfter(existingElementOrNode, newElementOrNode) {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
}
|
|
831
|
-
else {
|
|
832
|
-
existingNode = this.getNode(existingElementOrNode);
|
|
833
|
-
}
|
|
833
|
+
const existingNode = this.isNode(existingElementOrNode)
|
|
834
|
+
? existingElementOrNode
|
|
835
|
+
: this.getNode(existingElementOrNode);
|
|
834
836
|
if (existingNode) {
|
|
835
837
|
const newNode = this._ensureNode(newElementOrNode);
|
|
836
838
|
newNode.next = existingNode.next;
|
|
@@ -936,17 +938,15 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
936
938
|
* Time Complexity: O(n)
|
|
937
939
|
* Space Complexity: O(1)
|
|
938
940
|
*
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
*
|
|
943
|
-
*
|
|
944
|
-
*
|
|
945
|
-
* list. If the element or node is found in the list, the method returns the index of that element or
|
|
946
|
-
* node. If the element or node is not found in the list, the method returns -1.
|
|
941
|
+
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
942
|
+
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
943
|
+
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
944
|
+
* can be one of the following:
|
|
945
|
+
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
946
|
+
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
947
947
|
*/
|
|
948
|
-
indexOf(
|
|
949
|
-
const predicate = this._ensurePredicate(
|
|
948
|
+
indexOf(elementNodeOrPredicate) {
|
|
949
|
+
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
950
950
|
let index = 0;
|
|
951
951
|
let current = this.head;
|
|
952
952
|
while (current) {
|
|
@@ -962,8 +962,6 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
962
962
|
* Time Complexity: O(n)
|
|
963
963
|
* Space Complexity: O(1)
|
|
964
964
|
*
|
|
965
|
-
*/
|
|
966
|
-
/**
|
|
967
965
|
* This function retrieves an element from a doubly linked list based on a given element
|
|
968
966
|
* node or predicate.
|
|
969
967
|
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
@@ -972,7 +970,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
972
970
|
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
973
971
|
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
974
972
|
*/
|
|
975
|
-
|
|
973
|
+
search(elementNodeOrPredicate) {
|
|
976
974
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
977
975
|
let current = this.head;
|
|
978
976
|
while (current) {
|
|
@@ -1122,6 +1120,23 @@ class DoublyLinkedList extends base_1.IterableElementBase {
|
|
|
1122
1120
|
}
|
|
1123
1121
|
return mappedList;
|
|
1124
1122
|
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Time Complexity: O(n)
|
|
1125
|
+
* Space Complexity: O(1)
|
|
1126
|
+
*
|
|
1127
|
+
*/
|
|
1128
|
+
countOccurrences(elementOrNode) {
|
|
1129
|
+
const predicate = this._ensurePredicate(elementOrNode);
|
|
1130
|
+
let count = 0;
|
|
1131
|
+
let current = this.head;
|
|
1132
|
+
while (current) {
|
|
1133
|
+
if (predicate(current)) {
|
|
1134
|
+
count++;
|
|
1135
|
+
}
|
|
1136
|
+
current = current.next;
|
|
1137
|
+
}
|
|
1138
|
+
return count;
|
|
1139
|
+
}
|
|
1125
1140
|
/**
|
|
1126
1141
|
* Time Complexity: O(n)
|
|
1127
1142
|
* Space Complexity: O(n)
|