data-structure-typed 2.0.0 → 2.0.1
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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +14 -14
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +47 -0
- package/dist/cjs/data-structures/queue/queue.js +47 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +121 -0
- package/dist/cjs/data-structures/stack/stack.js +121 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +14 -14
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +66 -0
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +47 -0
- package/dist/esm/data-structures/queue/queue.js +47 -0
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +121 -0
- package/dist/esm/data-structures/stack/stack.js +121 -0
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
- package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
- package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
- package/dist/individuals/binary-tree/bst.mjs +3858 -0
- package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
- package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
- package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
- package/dist/individuals/graph/directed-graph.mjs +2910 -0
- package/dist/individuals/graph/undirected-graph.mjs +2745 -0
- package/dist/individuals/hash/hash-map.mjs +1040 -0
- package/dist/individuals/heap/heap.mjs +909 -0
- package/dist/individuals/heap/max-heap.mjs +671 -0
- package/dist/individuals/heap/min-heap.mjs +659 -0
- package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
- package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
- package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
- package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
- package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
- package/dist/individuals/queue/deque.mjs +1262 -0
- package/dist/individuals/queue/queue.mjs +1865 -0
- package/dist/individuals/stack/stack.mjs +415 -0
- package/dist/individuals/trie/trie.mjs +687 -0
- package/dist/umd/data-structure-typed.js +14 -14
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/graph/abstract-graph.ts +14 -14
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +66 -0
- package/src/data-structures/queue/queue.ts +47 -0
- package/src/data-structures/stack/stack.ts +121 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +72 -1
- package/test/unit/data-structures/queue/queue.test.ts +214 -0
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
|
@@ -842,3 +842,138 @@ describe('LinkedHashMap', () => {
|
|
|
842
842
|
});
|
|
843
843
|
});
|
|
844
844
|
});
|
|
845
|
+
|
|
846
|
+
describe('classic uses', () => {
|
|
847
|
+
it('@example should maintain insertion order', () => {
|
|
848
|
+
const linkedHashMap = new LinkedHashMap<number, string>();
|
|
849
|
+
linkedHashMap.set(1, 'A');
|
|
850
|
+
linkedHashMap.set(2, 'B');
|
|
851
|
+
linkedHashMap.set(3, 'C');
|
|
852
|
+
|
|
853
|
+
const result = Array.from(linkedHashMap);
|
|
854
|
+
expect(result).toEqual([
|
|
855
|
+
[1, 'A'],
|
|
856
|
+
[2, 'B'],
|
|
857
|
+
[3, 'C']
|
|
858
|
+
]);
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
it('should allow reverse iteration', () => {
|
|
862
|
+
const linkedHashMap = new LinkedHashMap<number, string>();
|
|
863
|
+
linkedHashMap.set(1, 'A');
|
|
864
|
+
linkedHashMap.set(2, 'B');
|
|
865
|
+
linkedHashMap.set(3, 'C');
|
|
866
|
+
|
|
867
|
+
const result = Array.from(linkedHashMap.reverseBegin());
|
|
868
|
+
expect(result).toEqual([
|
|
869
|
+
[3, 'C'],
|
|
870
|
+
[2, 'B'],
|
|
871
|
+
[1, 'A']
|
|
872
|
+
]);
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
it('should allow fast deletion at an index', () => {
|
|
876
|
+
const linkedHashMap = new LinkedHashMap<number, string>();
|
|
877
|
+
linkedHashMap.set(1, 'A');
|
|
878
|
+
linkedHashMap.set(2, 'B');
|
|
879
|
+
linkedHashMap.set(3, 'C');
|
|
880
|
+
|
|
881
|
+
linkedHashMap.deleteAt(1);
|
|
882
|
+
|
|
883
|
+
const result = Array.from(linkedHashMap);
|
|
884
|
+
expect(result).toEqual([
|
|
885
|
+
[1, 'A'],
|
|
886
|
+
[3, 'C']
|
|
887
|
+
]);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
it('should filter entries correctly', () => {
|
|
891
|
+
const linkedHashMap = new LinkedHashMap<number, string>();
|
|
892
|
+
linkedHashMap.set(1, 'A');
|
|
893
|
+
linkedHashMap.set(2, 'B');
|
|
894
|
+
linkedHashMap.set(3, 'C');
|
|
895
|
+
|
|
896
|
+
const filteredMap = linkedHashMap.filter((key, value) => value !== 'B');
|
|
897
|
+
|
|
898
|
+
const result = Array.from(filteredMap);
|
|
899
|
+
expect(result).toEqual([
|
|
900
|
+
[1, 'A'],
|
|
901
|
+
[3, 'C']
|
|
902
|
+
]);
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
it('should map entries to a new LinkedHashMap', () => {
|
|
906
|
+
const linkedHashMap = new LinkedHashMap<number, string>();
|
|
907
|
+
linkedHashMap.set(1, 'A');
|
|
908
|
+
linkedHashMap.set(2, 'B');
|
|
909
|
+
|
|
910
|
+
const mappedMap = linkedHashMap.map((key, value) => [value, key]);
|
|
911
|
+
|
|
912
|
+
const result = Array.from(mappedMap);
|
|
913
|
+
expect(result).toEqual([
|
|
914
|
+
['A', 1],
|
|
915
|
+
['B', 2]
|
|
916
|
+
]);
|
|
917
|
+
});
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
describe('classic uses', () => {
|
|
921
|
+
it('@example fast lookup of values by key', () => {
|
|
922
|
+
const hashMap = new HashMap<number, string>();
|
|
923
|
+
hashMap.set(1, 'A');
|
|
924
|
+
hashMap.set(2, 'B');
|
|
925
|
+
hashMap.set(3, 'C');
|
|
926
|
+
|
|
927
|
+
expect(hashMap.get(1)).toBe('A');
|
|
928
|
+
expect(hashMap.get(2)).toBe('B');
|
|
929
|
+
expect(hashMap.get(3)).toBe('C');
|
|
930
|
+
expect(hashMap.get(99)).toBeUndefined(); // Key not present
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
it('@example remove duplicates when adding multiple entries', () => {
|
|
934
|
+
const hashMap = new HashMap<number, string>();
|
|
935
|
+
hashMap.set(1, 'A');
|
|
936
|
+
hashMap.set(2, 'B');
|
|
937
|
+
hashMap.set(1, 'C'); // Update value for key 1
|
|
938
|
+
|
|
939
|
+
expect(hashMap.size).toBe(2);
|
|
940
|
+
expect(hashMap.get(1)).toBe('C');
|
|
941
|
+
expect(hashMap.get(2)).toBe('B');
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it('@example count occurrences of keys', () => {
|
|
945
|
+
const data = [1, 2, 1, 3, 2, 1];
|
|
946
|
+
|
|
947
|
+
const countMap = new HashMap<number, number>();
|
|
948
|
+
for (const key of data) {
|
|
949
|
+
countMap.set(key, (countMap.get(key) || 0) + 1);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
expect(countMap.get(1)).toBe(3);
|
|
953
|
+
expect(countMap.get(2)).toBe(2);
|
|
954
|
+
expect(countMap.get(3)).toBe(1);
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
it('should group entries by a key-derived property', () => {
|
|
958
|
+
const entries = [
|
|
959
|
+
{ id: 1, group: 'A' },
|
|
960
|
+
{ id: 2, group: 'B' },
|
|
961
|
+
{ id: 3, group: 'A' },
|
|
962
|
+
{ id: 4, group: 'B' }
|
|
963
|
+
];
|
|
964
|
+
|
|
965
|
+
const groupedMap = new HashMap<string, number[]>();
|
|
966
|
+
|
|
967
|
+
for (const entry of entries) {
|
|
968
|
+
const group = entry.group;
|
|
969
|
+
const id = entry.id;
|
|
970
|
+
if (!groupedMap.has(group)) {
|
|
971
|
+
groupedMap.set(group, []);
|
|
972
|
+
}
|
|
973
|
+
groupedMap.get(group)?.push(id);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
expect(groupedMap.get('A')).toEqual([1, 3]);
|
|
977
|
+
expect(groupedMap.get('B')).toEqual([2, 4]);
|
|
978
|
+
});
|
|
979
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SinglyLinkedList, SinglyLinkedListNode } from '../../../../src';
|
|
1
|
+
import { SinglyLinkedList, SinglyLinkedListNode, Stack } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('SinglyLinkedListNode', () => {
|
|
4
4
|
it('should SinglyLinkedList', () => {
|
|
@@ -649,3 +649,74 @@ describe('iterable methods', () => {
|
|
|
649
649
|
expect(sl.reduce((accumulator, element) => accumulator + element, 0)).toEqual(6);
|
|
650
650
|
});
|
|
651
651
|
});
|
|
652
|
+
|
|
653
|
+
describe('classic uses', () => {
|
|
654
|
+
|
|
655
|
+
it('@example implementation of a basic text editor', () => {
|
|
656
|
+
|
|
657
|
+
class TextEditor {
|
|
658
|
+
private content: SinglyLinkedList<string>;
|
|
659
|
+
private cursorIndex: number;
|
|
660
|
+
private undoStack: Stack<{ operation: string; data?: any }>;
|
|
661
|
+
|
|
662
|
+
constructor() {
|
|
663
|
+
this.content = new SinglyLinkedList<string>();
|
|
664
|
+
this.cursorIndex = 0; // Cursor starts at the beginning
|
|
665
|
+
this.undoStack = new Stack<{ operation: string; data?: any }>(); // Stack to keep track of operations for undo
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
insert(char: string) {
|
|
669
|
+
this.content.addAt(this.cursorIndex, char);
|
|
670
|
+
this.cursorIndex++;
|
|
671
|
+
this.undoStack.push({ operation: 'insert', data: { index: this.cursorIndex - 1 } });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
delete() {
|
|
675
|
+
if (this.cursorIndex === 0) return; // Nothing to delete
|
|
676
|
+
const deleted = this.content.deleteAt(this.cursorIndex - 1);
|
|
677
|
+
this.cursorIndex--;
|
|
678
|
+
this.undoStack.push({ operation: 'delete', data: { index: this.cursorIndex, char: deleted } });
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
moveCursor(index: number) {
|
|
682
|
+
this.cursorIndex = Math.max(0, Math.min(index, this.content.length));
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
undo() {
|
|
686
|
+
if (this.undoStack.size === 0) return; // No operations to undo
|
|
687
|
+
const lastAction = this.undoStack.pop();
|
|
688
|
+
|
|
689
|
+
if (lastAction!.operation === 'insert') {
|
|
690
|
+
this.content.deleteAt(lastAction!.data.index);
|
|
691
|
+
this.cursorIndex = lastAction!.data.index;
|
|
692
|
+
} else if (lastAction!.operation === 'delete') {
|
|
693
|
+
this.content.addAt(lastAction!.data.index, lastAction!.data.char);
|
|
694
|
+
this.cursorIndex = lastAction!.data.index + 1;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
getText(): string {
|
|
699
|
+
return [...this.content].join('');
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// Example Usage
|
|
704
|
+
const editor = new TextEditor();
|
|
705
|
+
editor.insert('H');
|
|
706
|
+
editor.insert('e');
|
|
707
|
+
editor.insert('l');
|
|
708
|
+
editor.insert('l');
|
|
709
|
+
editor.insert('o');
|
|
710
|
+
expect(editor.getText()).toBe('Hello'); // Output: "Hello"
|
|
711
|
+
|
|
712
|
+
editor.delete();
|
|
713
|
+
expect(editor.getText()).toBe('Hell'); // Output: "Hell"
|
|
714
|
+
|
|
715
|
+
editor.undo();
|
|
716
|
+
expect(editor.getText()).toBe('Hello'); // Output: "Hello"
|
|
717
|
+
|
|
718
|
+
editor.moveCursor(1);
|
|
719
|
+
editor.insert('a');
|
|
720
|
+
expect(editor.getText()).toBe('Haello'); // Output: "Haello"
|
|
721
|
+
});
|
|
722
|
+
});
|
|
@@ -454,3 +454,217 @@ describe('LinkedListQueue', () => {
|
|
|
454
454
|
expect(cloned.length).toBe(2);
|
|
455
455
|
});
|
|
456
456
|
});
|
|
457
|
+
|
|
458
|
+
describe('Queue', () => {
|
|
459
|
+
// Test queue initialization
|
|
460
|
+
it('should initialize correctly with no elements', () => {
|
|
461
|
+
const queue = new Queue();
|
|
462
|
+
expect(queue.isEmpty()).toBe(true);
|
|
463
|
+
expect(queue.length).toBe(0);
|
|
464
|
+
expect(queue.first).toBeUndefined();
|
|
465
|
+
expect(queue.last).toBeUndefined();
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it('should initialize correctly with given elements', () => {
|
|
469
|
+
const queue = new Queue([1, 2, 3]);
|
|
470
|
+
expect(queue.length).toBe(3);
|
|
471
|
+
expect(queue.first).toBe(1);
|
|
472
|
+
expect(queue.last).toBe(3);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// Test push and pushMany
|
|
476
|
+
it('should add elements to the queue', () => {
|
|
477
|
+
const queue = new Queue<number>();
|
|
478
|
+
queue.push(1);
|
|
479
|
+
queue.push(2);
|
|
480
|
+
expect(queue.length).toBe(2);
|
|
481
|
+
expect(queue.first).toBe(1);
|
|
482
|
+
expect(queue.last).toBe(2);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('should add multiple elements using pushMany', () => {
|
|
486
|
+
const queue = new Queue<number>();
|
|
487
|
+
queue.pushMany([1, 2, 3]);
|
|
488
|
+
expect(queue.length).toBe(3);
|
|
489
|
+
expect(queue.elements).toEqual([1, 2, 3]);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// Test shift
|
|
493
|
+
it('should remove the first element from the queue', () => {
|
|
494
|
+
const queue = new Queue([1, 2, 3]);
|
|
495
|
+
const shifted = queue.shift();
|
|
496
|
+
expect(shifted).toBe(1);
|
|
497
|
+
expect(queue.length).toBe(2);
|
|
498
|
+
expect(queue.first).toBe(2);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
// Test delete and deleteAt
|
|
502
|
+
it('should delete an element from the queue', () => {
|
|
503
|
+
const queue = new Queue([1, 2, 3]);
|
|
504
|
+
const result = queue.delete(2);
|
|
505
|
+
expect(result).toBe(true);
|
|
506
|
+
expect(queue.elements).toEqual([1, 3]);
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it('should delete an element at a specific index', () => {
|
|
510
|
+
const queue = new Queue([1, 2, 3]);
|
|
511
|
+
const deleted = queue.deleteAt(1);
|
|
512
|
+
expect(deleted).toBe(2);
|
|
513
|
+
expect(queue.elements).toEqual([1, 3]);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
// Test at
|
|
517
|
+
it('should retrieve an element by index', () => {
|
|
518
|
+
const queue = new Queue([1, 2, 3]);
|
|
519
|
+
expect(queue.at(0)).toBe(1);
|
|
520
|
+
expect(queue.at(2)).toBe(3);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
// Test reverse
|
|
524
|
+
it('should reverse the queue', () => {
|
|
525
|
+
const queue = new Queue([1, 2, 3]);
|
|
526
|
+
queue.reverse();
|
|
527
|
+
expect(queue.elements).toEqual([3, 2, 1]);
|
|
528
|
+
expect(queue.first).toBe(3);
|
|
529
|
+
expect(queue.last).toBe(1);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
// Test addAt
|
|
533
|
+
it('should add an element at a specific index', () => {
|
|
534
|
+
const queue = new Queue([1, 3]);
|
|
535
|
+
const result = queue.addAt(1, 2);
|
|
536
|
+
expect(result).toBe(true);
|
|
537
|
+
expect(queue.elements).toEqual([1, 2, 3]);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
// Test setAt
|
|
541
|
+
it('should set an element at a specific index', () => {
|
|
542
|
+
const queue = new Queue([1, 2, 3]);
|
|
543
|
+
const result = queue.setAt(1, 10);
|
|
544
|
+
expect(result).toBe(true);
|
|
545
|
+
expect(queue.elements).toEqual([1, 10, 3]);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
// Test clear
|
|
549
|
+
it('should clear the queue', () => {
|
|
550
|
+
const queue = new Queue([1, 2, 3]);
|
|
551
|
+
queue.clear();
|
|
552
|
+
expect(queue.isEmpty()).toBe(true);
|
|
553
|
+
expect(queue.length).toBe(0);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// Test compact
|
|
557
|
+
it('should compact the queue', () => {
|
|
558
|
+
const queue = new Queue([1, 2, 3]);
|
|
559
|
+
queue.shift();
|
|
560
|
+
queue.shift();
|
|
561
|
+
queue.compact();
|
|
562
|
+
expect(queue.elements).toEqual([3]);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
// Test splice
|
|
566
|
+
it('should splice elements from the queue', () => {
|
|
567
|
+
const queue = new Queue([1, 2, 3, 4]);
|
|
568
|
+
const removed = queue.splice(1, 2);
|
|
569
|
+
expect(removed.elements).toEqual([2, 3]);
|
|
570
|
+
expect(queue.elements).toEqual([1, 4]);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
// Test clone
|
|
574
|
+
it('should create a clone of the queue', () => {
|
|
575
|
+
const queue = new Queue([1, 2, 3]);
|
|
576
|
+
const clone = queue.clone();
|
|
577
|
+
expect(clone.elements).toEqual(queue.elements);
|
|
578
|
+
clone.push(4);
|
|
579
|
+
expect(queue.elements).not.toContain(4);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
// Test filter
|
|
583
|
+
it('should filter elements based on a predicate', () => {
|
|
584
|
+
const queue = new Queue([1, 2, 3, 4]);
|
|
585
|
+
const filtered = queue.filter(el => el % 2 === 0);
|
|
586
|
+
expect(filtered.elements).toEqual([2, 4]);
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
// Test map
|
|
590
|
+
it('should map elements to a new queue', () => {
|
|
591
|
+
const queue = new Queue([1, 2, 3]);
|
|
592
|
+
const mapped = queue.map(el => el * 2);
|
|
593
|
+
expect(mapped.elements).toEqual([2, 4, 6]);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
describe('classic uses', () => {
|
|
598
|
+
it('@example Sliding Window using Queue', () => {
|
|
599
|
+
const nums = [2, 3, 4, 1, 5];
|
|
600
|
+
const k = 2;
|
|
601
|
+
const queue = new Queue<number>();
|
|
602
|
+
|
|
603
|
+
let maxSum = 0;
|
|
604
|
+
let currentSum = 0;
|
|
605
|
+
|
|
606
|
+
nums.forEach((num, i) => {
|
|
607
|
+
queue.push(num);
|
|
608
|
+
currentSum += num;
|
|
609
|
+
|
|
610
|
+
if (queue.length > k) {
|
|
611
|
+
currentSum -= queue.shift()!;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
if (queue.length === k) {
|
|
615
|
+
maxSum = Math.max(maxSum, currentSum);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
expect(maxSum).toBe(7); // Maximum sum is from subarray [3, 4].
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it('@example Breadth-First Search (BFS) using Queue', () => {
|
|
623
|
+
const graph: { [key in number]: number[] } = {
|
|
624
|
+
1: [2, 3],
|
|
625
|
+
2: [4, 5],
|
|
626
|
+
3: [],
|
|
627
|
+
4: [],
|
|
628
|
+
5: []
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const queue = new Queue<number>();
|
|
632
|
+
const visited: number[] = [];
|
|
633
|
+
|
|
634
|
+
queue.push(1);
|
|
635
|
+
|
|
636
|
+
while (!queue.isEmpty()) {
|
|
637
|
+
const node = queue.shift()!;
|
|
638
|
+
if (!visited.includes(node)) {
|
|
639
|
+
visited.push(node);
|
|
640
|
+
graph[node].forEach(neighbor => queue.push(neighbor));
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
expect(visited).toEqual([1, 2, 3, 4, 5]); // Expected BFS traversal order.
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('Task Scheduling using Queue', () => {
|
|
648
|
+
const tasks = ['A', 'A', 'A', 'B', 'B', 'B'];
|
|
649
|
+
const cooldown = 2;
|
|
650
|
+
|
|
651
|
+
const taskQueue = new Queue<string>();
|
|
652
|
+
const cooldownQueue = new Queue<string>();
|
|
653
|
+
|
|
654
|
+
for (const task of tasks) {
|
|
655
|
+
while (!cooldownQueue.isEmpty() && cooldownQueue.first === task) {
|
|
656
|
+
cooldownQueue.shift();
|
|
657
|
+
taskQueue.push('idle');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
taskQueue.push(task);
|
|
661
|
+
cooldownQueue.push(task);
|
|
662
|
+
if (cooldownQueue.length > cooldown) {
|
|
663
|
+
cooldownQueue.shift();
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
const scheduled = taskQueue.elements;
|
|
668
|
+
expect(scheduled).toEqual(['A', 'idle', 'A', 'idle', 'A', 'B', 'B', 'idle', 'idle', 'B']);
|
|
669
|
+
});
|
|
670
|
+
});
|
|
@@ -162,3 +162,168 @@ describe('Stack iterative methods', () => {
|
|
|
162
162
|
}).toThrow('toElementFn must be a function type');
|
|
163
163
|
});
|
|
164
164
|
});
|
|
165
|
+
|
|
166
|
+
describe('classic uses', () => {
|
|
167
|
+
it('@example Balanced Parentheses or Brackets', () => {
|
|
168
|
+
type ValidCharacters = ')' | '(' | ']' | '[' | '}' | '{';
|
|
169
|
+
|
|
170
|
+
const stack = new Stack<string>();
|
|
171
|
+
const input: ValidCharacters[] = '[({})]'.split('') as ValidCharacters[];
|
|
172
|
+
const matches: { [key in ValidCharacters]?: ValidCharacters } = { ')': '(', ']': '[', '}': '{' };
|
|
173
|
+
for (const char of input) {
|
|
174
|
+
if ('([{'.includes(char)) {
|
|
175
|
+
stack.push(char);
|
|
176
|
+
} else if (')]}'.includes(char)) {
|
|
177
|
+
if (stack.pop() !== matches[char]) {
|
|
178
|
+
fail('Parentheses are not balanced');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
expect(stack.isEmpty()).toBe(true);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('@example Expression Evaluation and Conversion', () => {
|
|
186
|
+
const stack = new Stack<number>();
|
|
187
|
+
const expression = [5, 3, '+']; // Equivalent to 5 + 3
|
|
188
|
+
expression.forEach(token => {
|
|
189
|
+
if (typeof token === 'number') {
|
|
190
|
+
stack.push(token);
|
|
191
|
+
} else {
|
|
192
|
+
const b = stack.pop()!;
|
|
193
|
+
const a = stack.pop()!;
|
|
194
|
+
stack.push(token === '+' ? a + b : 0); // Only handling '+' here
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
expect(stack.pop()).toBe(8);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('@example Depth-First Search (DFS)', () => {
|
|
201
|
+
const stack = new Stack<number>();
|
|
202
|
+
const graph: { [key in number]: number[] } = { 1: [2, 3], 2: [4], 3: [5], 4: [], 5: [] };
|
|
203
|
+
const visited: number[] = [];
|
|
204
|
+
stack.push(1);
|
|
205
|
+
while (!stack.isEmpty()) {
|
|
206
|
+
const node = stack.pop()!;
|
|
207
|
+
if (!visited.includes(node)) {
|
|
208
|
+
visited.push(node);
|
|
209
|
+
graph[node].forEach(neighbor => stack.push(neighbor));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
expect(visited).toEqual([1, 3, 5, 2, 4]); // Example DFS order
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('@example Backtracking Algorithms', () => {
|
|
216
|
+
const stack = new Stack<[number, number]>();
|
|
217
|
+
const maze = [
|
|
218
|
+
['S', ' ', 'X'],
|
|
219
|
+
['X', ' ', 'X'],
|
|
220
|
+
[' ', ' ', 'E']
|
|
221
|
+
];
|
|
222
|
+
const start: [number, number] = [0, 0];
|
|
223
|
+
const end = [2, 2];
|
|
224
|
+
const directions = [
|
|
225
|
+
[0, 1], // To the right
|
|
226
|
+
[1, 0], // down
|
|
227
|
+
[0, -1], // left
|
|
228
|
+
[-1, 0] // up
|
|
229
|
+
];
|
|
230
|
+
|
|
231
|
+
const visited = new Set<string>(); // Used to record visited nodes
|
|
232
|
+
stack.push(start);
|
|
233
|
+
const path: number[][] = [];
|
|
234
|
+
|
|
235
|
+
while (!stack.isEmpty()) {
|
|
236
|
+
const [x, y] = stack.pop()!;
|
|
237
|
+
if (visited.has(`${x},${y}`)) continue; // Skip already visited nodes
|
|
238
|
+
visited.add(`${x},${y}`);
|
|
239
|
+
|
|
240
|
+
path.push([x, y]);
|
|
241
|
+
|
|
242
|
+
if (x === end[0] && y === end[1]) {
|
|
243
|
+
break; // Find the end point and exit
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
for (const [dx, dy] of directions) {
|
|
247
|
+
const nx = x + dx;
|
|
248
|
+
const ny = y + dy;
|
|
249
|
+
if (
|
|
250
|
+
maze[nx]?.[ny] === ' ' || // feasible path
|
|
251
|
+
maze[nx]?.[ny] === 'E' // destination
|
|
252
|
+
) {
|
|
253
|
+
stack.push([nx, ny]);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
expect(path).toContainEqual(end);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('@example Function Call Stack', () => {
|
|
262
|
+
const functionStack = new Stack<string>();
|
|
263
|
+
functionStack.push('main');
|
|
264
|
+
functionStack.push('foo');
|
|
265
|
+
functionStack.push('bar');
|
|
266
|
+
expect(functionStack.pop()).toBe('bar');
|
|
267
|
+
expect(functionStack.pop()).toBe('foo');
|
|
268
|
+
expect(functionStack.pop()).toBe('main');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('@example Simplify File Paths', () => {
|
|
272
|
+
const stack = new Stack<string>();
|
|
273
|
+
const path = '/a/./b/../../c';
|
|
274
|
+
path.split('/').forEach(segment => {
|
|
275
|
+
if (segment === '..') stack.pop();
|
|
276
|
+
else if (segment && segment !== '.') stack.push(segment);
|
|
277
|
+
});
|
|
278
|
+
expect(stack.elements.join('/')).toBe('c');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('@example Stock Span Problem', () => {
|
|
282
|
+
const stack = new Stack<number>();
|
|
283
|
+
const prices = [100, 80, 60, 70, 60, 75, 85];
|
|
284
|
+
const spans: number[] = [];
|
|
285
|
+
prices.forEach((price, i) => {
|
|
286
|
+
while (!stack.isEmpty() && prices[stack.peek()!] <= price) {
|
|
287
|
+
stack.pop();
|
|
288
|
+
}
|
|
289
|
+
spans.push(stack.isEmpty() ? i + 1 : i - stack.peek()!);
|
|
290
|
+
stack.push(i);
|
|
291
|
+
});
|
|
292
|
+
expect(spans).toEqual([1, 1, 1, 2, 1, 4, 6]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('Browser Navigation', () => {
|
|
296
|
+
const backStack = new Stack<string>();
|
|
297
|
+
const forwardStack = new Stack<string>();
|
|
298
|
+
backStack.push('Page 1');
|
|
299
|
+
backStack.push('Page 2');
|
|
300
|
+
forwardStack.push(backStack.pop()!);
|
|
301
|
+
expect(backStack.size).toBe(1);
|
|
302
|
+
expect(forwardStack.size).toBe(1);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('String Reversal', () => {
|
|
306
|
+
const stack = new Stack<string>();
|
|
307
|
+
const input = 'hello';
|
|
308
|
+
const reversed = [];
|
|
309
|
+
input.split('').forEach(char => stack.push(char));
|
|
310
|
+
while (!stack.isEmpty()) {
|
|
311
|
+
reversed.push(stack.pop());
|
|
312
|
+
}
|
|
313
|
+
expect(reversed.join('')).toBe('olleh');
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('Next Greater Element', () => {
|
|
317
|
+
const stack = new Stack<number>();
|
|
318
|
+
const array = [4, 5, 2, 25];
|
|
319
|
+
const nextGreater = new Array(array.length).fill(-1);
|
|
320
|
+
array.forEach((_, i) => {
|
|
321
|
+
while (!stack.isEmpty() && array[stack.peek()!] < array[i]) {
|
|
322
|
+
const idx = stack.pop()!;
|
|
323
|
+
nextGreater[idx] = array[i];
|
|
324
|
+
}
|
|
325
|
+
stack.push(i);
|
|
326
|
+
});
|
|
327
|
+
expect(nextGreater).toEqual([5, 25, 25, -1]);
|
|
328
|
+
});
|
|
329
|
+
});
|