deque-typed 2.2.1 → 2.2.3

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.
Files changed (40) hide show
  1. package/README.md +106 -72
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs.map +1 -1
  4. package/dist/esm/index.mjs.map +1 -1
  5. package/dist/esm-legacy/index.mjs.map +1 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  7. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  8. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  9. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  10. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  11. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  12. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  13. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  14. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  15. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  16. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  17. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  18. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  19. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  20. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  21. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  22. package/dist/umd/deque-typed.js.map +1 -1
  23. package/dist/umd/deque-typed.min.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  26. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  27. package/src/data-structures/binary-tree/bst.ts +322 -13
  28. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  29. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  30. package/src/data-structures/graph/directed-graph.ts +126 -1
  31. package/src/data-structures/graph/undirected-graph.ts +160 -1
  32. package/src/data-structures/hash/hash-map.ts +110 -27
  33. package/src/data-structures/heap/heap.ts +107 -58
  34. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  35. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  36. package/src/data-structures/queue/deque.ts +95 -67
  37. package/src/data-structures/queue/queue.ts +90 -34
  38. package/src/data-structures/stack/stack.ts +58 -40
  39. package/src/data-structures/trie/trie.ts +109 -47
  40. package/src/interfaces/binary-tree.ts +2 -0
package/README.md CHANGED
@@ -34,96 +34,130 @@ yarn add deque-typed
34
34
 
35
35
  [//]: # (No deletion!!! Start of Example Replace Section)
36
36
 
37
- ### prize roulette
37
+ ### basic Deque creation and push/pop operations
38
38
  ```typescript
39
- class PrizeRoulette {
40
- private deque: Deque<string>;
39
+ // Create a simple Deque with initial values
40
+ const deque = new Deque([1, 2, 3, 4, 5]);
41
41
 
42
- constructor(prizes: string[]) {
43
- // Initialize the deque with prizes
44
- this.deque = new Deque<string>(prizes);
45
- }
42
+ // Verify the deque maintains insertion order
43
+ console.log([...deque]); // [1, 2, 3, 4, 5];
46
44
 
47
- // Rotate clockwise to the right (forward)
48
- rotateClockwise(steps: number): void {
49
- const n = this.deque.length;
50
- if (n === 0) return;
45
+ // Check length
46
+ console.log(deque.length); // 5;
51
47
 
52
- for (let i = 0; i < steps; i++) {
53
- const last = this.deque.pop(); // Remove the last element
54
- this.deque.unshift(last!); // Add it to the front
55
- }
56
- }
48
+ // Push to the end
49
+ deque.push(6);
50
+ console.log(deque.length); // 6;
57
51
 
58
- // Rotate counterclockwise to the left (backward)
59
- rotateCounterClockwise(steps: number): void {
60
- const n = this.deque.length;
61
- if (n === 0) return;
52
+ // Pop from the end
53
+ const last = deque.pop();
54
+ console.log(last); // 6;
55
+ ```
62
56
 
63
- for (let i = 0; i < steps; i++) {
64
- const first = this.deque.shift(); // Remove the first element
65
- this.deque.push(first!); // Add it to the back
66
- }
67
- }
57
+ ### Deque shift and unshift operations
58
+ ```typescript
59
+ const deque = new Deque<number>([20, 30, 40]);
68
60
 
69
- // Display the current prize at the head
70
- display() {
71
- return this.deque.first;
72
- }
73
- }
61
+ // Unshift adds to the front
62
+ deque.unshift(10);
63
+ console.log([...deque]); // [10, 20, 30, 40];
64
+
65
+ // Shift removes from the front (O(1) complexity!)
66
+ const first = deque.shift();
67
+ console.log(first); // 10;
68
+
69
+ // Verify remaining elements
70
+ console.log([...deque]); // [20, 30, 40];
71
+ console.log(deque.length); // 3;
72
+ ```
73
+
74
+ ### Deque peek at both ends
75
+ ```typescript
76
+ const deque = new Deque<number>([10, 20, 30, 40, 50]);
77
+
78
+ // Get first element without removing
79
+ const first = deque.at(0);
80
+ console.log(first); // 10;
74
81
 
75
- // Example usage
76
- const prizes = ['Car', 'Bike', 'Laptop', 'Phone', 'Watch', 'Headphones']; // Initialize the prize list
77
- const roulette = new PrizeRoulette(prizes);
82
+ // Get last element without removing
83
+ const last = deque.at(deque.length - 1);
84
+ console.log(last); // 50;
78
85
 
79
- // Display the initial state
80
- console.log(roulette.display()); // 'Car' // Car
86
+ // Length unchanged
87
+ console.log(deque.length); // 5;
88
+ ```
89
+
90
+ ### Deque for...of iteration and reverse
91
+ ```typescript
92
+ const deque = new Deque<string>(['A', 'B', 'C', 'D']);
81
93
 
82
- // Rotate clockwise by 3 steps
83
- roulette.rotateClockwise(3);
84
- console.log(roulette.display()); // 'Phone' // Phone
94
+ // Iterate forward
95
+ const forward: string[] = [];
96
+ for (const item of deque) {
97
+ forward.push(item);
98
+ }
99
+ console.log(forward); // ['A', 'B', 'C', 'D'];
85
100
 
86
- // Rotate counterclockwise by 2 steps
87
- roulette.rotateCounterClockwise(2);
88
- console.log(roulette.display()); // 'Headphones'
101
+ // Reverse the deque
102
+ deque.reverse();
103
+ const backward: string[] = [];
104
+ for (const item of deque) {
105
+ backward.push(item);
106
+ }
107
+ console.log(backward); // ['D', 'C', 'B', 'A'];
89
108
  ```
90
109
 
91
- ### sliding window
110
+ ### Deque as sliding window for stream processing
92
111
  ```typescript
93
- // Maximum function of sliding window
94
- function maxSlidingWindow(nums: number[], k: number): number[] {
95
- const n = nums.length;
96
- if (n * k === 0) return [];
97
-
98
- const deq = new Deque<number>();
99
- const result: number[] = [];
100
-
101
- for (let i = 0; i < n; i++) {
102
- // Delete indexes in the queue that are not within the window range
103
- if (deq.length > 0 && deq.first! === i - k) {
104
- deq.shift();
105
- }
106
-
107
- // Remove all indices less than the current value from the tail of the queue
108
- while (deq.length > 0 && nums[deq.last!] < nums[i]) {
109
- deq.pop();
110
- }
111
-
112
- // Add the current index to the end of the queue
113
- deq.push(i);
114
-
115
- // Add the maximum value of the window to the results
116
- if (i >= k - 1) {
117
- result.push(nums[deq.first!]);
118
- }
112
+ interface DataPoint {
113
+ timestamp: number;
114
+ value: number;
115
+ sensor: string;
116
+ }
117
+
118
+ // Create a deque-based sliding window for real-time data aggregation
119
+ const windowSize = 3;
120
+ const dataWindow = new Deque<DataPoint>();
121
+
122
+ // Simulate incoming sensor data stream
123
+ const incomingData: DataPoint[] = [
124
+ { timestamp: 1000, value: 25.5, sensor: 'temp-01' },
125
+ { timestamp: 1100, value: 26.2, sensor: 'temp-01' },
126
+ { timestamp: 1200, value: 25.8, sensor: 'temp-01' },
127
+ { timestamp: 1300, value: 27.1, sensor: 'temp-01' },
128
+ { timestamp: 1400, value: 26.9, sensor: 'temp-01' }
129
+ ];
130
+
131
+ const windowResults: Array<{ avgValue: number; windowSize: number }> = [];
132
+
133
+ for (const dataPoint of incomingData) {
134
+ // Add new data to the end
135
+ dataWindow.push(dataPoint);
136
+
137
+ // Remove oldest data when window exceeds size (O(1) from front)
138
+ if (dataWindow.length > windowSize) {
139
+ dataWindow.shift();
140
+ }
141
+
142
+ // Calculate average of current window
143
+ let sum = 0;
144
+ for (const point of dataWindow) {
145
+ sum += point.value;
119
146
  }
147
+ const avg = sum / dataWindow.length;
120
148
 
121
- return result;
149
+ windowResults.push({
150
+ avgValue: Math.round(avg * 10) / 10,
151
+ windowSize: dataWindow.length
152
+ });
122
153
  }
123
154
 
124
- const nums = [1, 3, -1, -3, 5, 3, 6, 7];
125
- const k = 3;
126
- console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
155
+ // Verify sliding window behavior
156
+ console.log(windowResults.length); // 5;
157
+ console.log(windowResults[0].windowSize); // 1; // First window has 1 element
158
+ console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
159
+ console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
160
+ console.log(dataWindow.length); // 3;
127
161
  ```
128
162
 
129
163
  [//]: # (No deletion!!! End of Example Replace Section)