2023-11-23 10:40:30 +00:00
|
|
|
import {
|
2023-11-23 13:43:45 +00:00
|
|
|
AVLTree,
|
|
|
|
BinaryTree,
|
|
|
|
BST,
|
2023-11-23 10:40:30 +00:00
|
|
|
Deque,
|
|
|
|
DoublyLinkedList,
|
2023-11-25 14:32:09 +00:00
|
|
|
HashMap,
|
|
|
|
Heap,
|
2023-11-23 10:40:30 +00:00
|
|
|
MaxHeap,
|
|
|
|
MaxPriorityQueue,
|
|
|
|
MinHeap,
|
|
|
|
MinPriorityQueue,
|
|
|
|
Queue,
|
2023-11-23 13:43:45 +00:00
|
|
|
RedBlackTree,
|
2023-11-23 10:40:30 +00:00
|
|
|
SinglyLinkedList,
|
2023-11-23 13:43:45 +00:00
|
|
|
Stack,
|
2024-01-08 14:22:41 +00:00
|
|
|
TreeMultiMap,
|
2023-11-25 14:32:09 +00:00
|
|
|
Trie
|
2023-11-23 10:40:30 +00:00
|
|
|
} from '../../src';
|
2023-12-21 02:26:45 +00:00
|
|
|
import { isDebugTest } from '../config';
|
2023-11-23 10:40:30 +00:00
|
|
|
|
2023-11-23 13:43:45 +00:00
|
|
|
const isDebug = isDebugTest;
|
2023-11-23 10:40:30 +00:00
|
|
|
const orgArr: number[] = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
2023-11-25 14:32:09 +00:00
|
|
|
const orgStrArr: string[] = [
|
2023-12-21 02:26:45 +00:00
|
|
|
'trie',
|
|
|
|
'trial',
|
|
|
|
'trick',
|
|
|
|
'trip',
|
|
|
|
'tree',
|
|
|
|
'trend',
|
|
|
|
'triangle',
|
|
|
|
'track',
|
|
|
|
'trace',
|
|
|
|
'transmit'
|
|
|
|
];
|
2023-12-29 08:42:20 +00:00
|
|
|
const entries: [number, string][] = [
|
|
|
|
[6, '6'],
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[7, '7'],
|
|
|
|
[5, '5'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[9, '9'],
|
|
|
|
[8, '8']
|
2023-11-25 14:32:09 +00:00
|
|
|
];
|
2023-11-23 13:43:45 +00:00
|
|
|
|
2023-11-23 10:40:30 +00:00
|
|
|
describe('conversions', () => {
|
|
|
|
it('Array to Queue', () => {
|
|
|
|
const q = new Queue<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) q.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...q]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to Deque', () => {
|
|
|
|
const dq = new Deque<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) dq.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...dq]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to SinglyLinkedList', () => {
|
|
|
|
const sl = new SinglyLinkedList<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) sl.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...sl]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to DoublyLinkedList', () => {
|
|
|
|
const dl = new DoublyLinkedList<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) dl.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...dl]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to Stack', () => {
|
|
|
|
const stack = new Stack<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) stack.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...stack]).toEqual([6, 1, 2, 7, 5, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to MinHeap', () => {
|
|
|
|
const minHeap = new MinHeap<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) minHeap.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...minHeap]).toEqual([1, 5, 2, 7, 6, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to MaxHeap', () => {
|
|
|
|
const maxHeap = new MaxHeap<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) maxHeap.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...maxHeap]).toEqual([9, 8, 4, 7, 5, 2, 3, 1, 6]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to MinPriorityQueue', () => {
|
|
|
|
const minPQ = new MinPriorityQueue<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) minPQ.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...minPQ]).toEqual([1, 5, 2, 7, 6, 3, 4, 9, 8]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 10:40:30 +00:00
|
|
|
|
|
|
|
it('Array to MaxPriorityQueue', () => {
|
|
|
|
const maxPQ = new MaxPriorityQueue<number>(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) maxPQ.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...maxPQ]).toEqual([9, 8, 4, 7, 5, 2, 3, 1, 6]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 13:43:45 +00:00
|
|
|
|
|
|
|
it('Entry Array to BinaryTree', () => {
|
|
|
|
const biTree = new BinaryTree<number>(entries);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) biTree.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...biTree]).toEqual([
|
|
|
|
[9, '9'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[1, '1'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[3, '3'],
|
|
|
|
[2, '2'],
|
|
|
|
[4, '4']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 13:43:45 +00:00
|
|
|
|
|
|
|
it('Entry Array to BST', () => {
|
2023-11-25 14:32:09 +00:00
|
|
|
const bst = new BST<number>(entries);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(bst.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) bst.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...bst]).toEqual([
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[9, '9']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 13:43:45 +00:00
|
|
|
|
|
|
|
it('Entry Array to RedBlackTree', () => {
|
2023-11-25 14:32:09 +00:00
|
|
|
const rbTree = new RedBlackTree<number>(entries);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(rbTree.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) rbTree.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...rbTree]).toEqual([
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[9, '9']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 13:43:45 +00:00
|
|
|
|
|
|
|
it('Entry Array to AVLTree', () => {
|
2023-11-25 14:32:09 +00:00
|
|
|
const avl = new AVLTree<number>(entries);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(avl.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) avl.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...avl]).toEqual([
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[9, '9']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-23 13:43:45 +00:00
|
|
|
|
2024-01-08 14:22:41 +00:00
|
|
|
it('Entry Array to TreeMultiMap', () => {
|
|
|
|
const treeMulti = new TreeMultiMap<number>(entries);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(treeMulti.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) treeMulti.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...treeMulti]).toEqual([
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[9, '9']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-25 14:32:09 +00:00
|
|
|
|
|
|
|
it('HashMap to RedBlackTree', () => {
|
|
|
|
const hm = new HashMap(entries);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) hm.print();
|
2023-11-25 14:32:09 +00:00
|
|
|
const rbTree = new RedBlackTree<number>(hm);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(rbTree.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) rbTree.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...rbTree]).toEqual([
|
|
|
|
[1, '1'],
|
|
|
|
[2, '2'],
|
|
|
|
[3, '3'],
|
|
|
|
[4, '4'],
|
|
|
|
[5, '5'],
|
|
|
|
[6, '6'],
|
|
|
|
[7, '7'],
|
|
|
|
[8, '8'],
|
|
|
|
[9, '9']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-25 14:32:09 +00:00
|
|
|
|
|
|
|
it('PriorityQueue to BST', () => {
|
|
|
|
const pq = new MinPriorityQueue(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) pq.print();
|
2023-11-25 14:32:09 +00:00
|
|
|
const bst = new BST<number>(pq);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(bst.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) bst.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...bst]).toEqual([
|
|
|
|
[1, undefined],
|
|
|
|
[2, undefined],
|
|
|
|
[3, undefined],
|
|
|
|
[4, undefined],
|
|
|
|
[5, undefined],
|
|
|
|
[6, undefined],
|
|
|
|
[7, undefined],
|
|
|
|
[8, undefined],
|
|
|
|
[9, undefined]
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-25 14:32:09 +00:00
|
|
|
|
|
|
|
it('Deque to RedBlackTree', () => {
|
|
|
|
const dq = new Deque(orgArr);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) dq.print();
|
2023-11-25 14:32:09 +00:00
|
|
|
const rbTree = new RedBlackTree<number>(dq);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(rbTree.size).toBe(9);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) rbTree.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...rbTree]).toEqual([
|
|
|
|
[1, undefined],
|
|
|
|
[2, undefined],
|
|
|
|
[3, undefined],
|
|
|
|
[4, undefined],
|
|
|
|
[5, undefined],
|
|
|
|
[6, undefined],
|
|
|
|
[7, undefined],
|
|
|
|
[8, undefined],
|
|
|
|
[9, undefined]
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
2023-11-25 14:32:09 +00:00
|
|
|
|
|
|
|
it('Trie to Heap to Deque', () => {
|
|
|
|
const trie = new Trie(orgStrArr);
|
|
|
|
expect(trie.size).toBe(10);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) trie.print();
|
2024-08-26 22:56:04 +00:00
|
|
|
const heap = new Heap<string>(trie, {
|
|
|
|
comparator: (a, b) => Number(a) - Number(b)
|
|
|
|
});
|
2023-11-25 14:32:09 +00:00
|
|
|
expect(heap.size).toBe(10);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) heap.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...heap]).toEqual([
|
|
|
|
'transmit',
|
|
|
|
'trace',
|
|
|
|
'tree',
|
|
|
|
'trend',
|
|
|
|
'track',
|
|
|
|
'trial',
|
|
|
|
'trip',
|
|
|
|
'trie',
|
|
|
|
'trick',
|
|
|
|
'triangle'
|
|
|
|
]);
|
2023-11-25 14:32:09 +00:00
|
|
|
const dq = new Deque<string>(heap);
|
|
|
|
expect(dq.size).toBe(10);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) dq.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...dq]).toEqual([
|
|
|
|
'transmit',
|
|
|
|
'trace',
|
|
|
|
'tree',
|
|
|
|
'trend',
|
|
|
|
'track',
|
|
|
|
'trial',
|
|
|
|
'trip',
|
|
|
|
'trie',
|
|
|
|
'trick',
|
|
|
|
'triangle'
|
|
|
|
]);
|
2023-11-25 14:32:09 +00:00
|
|
|
const entries = dq.map((el, i) => <[number, string]>[i, el]);
|
2023-12-04 13:58:41 +00:00
|
|
|
const avl = new AVLTree<number, string>(entries);
|
2023-12-21 02:26:45 +00:00
|
|
|
expect(avl.size).toBe(10);
|
2024-10-30 02:30:28 +00:00
|
|
|
if (isDebug) avl.print();
|
2023-12-29 08:42:20 +00:00
|
|
|
expect([...avl]).toEqual([
|
|
|
|
[0, 'transmit'],
|
|
|
|
[1, 'trace'],
|
|
|
|
[2, 'tree'],
|
|
|
|
[3, 'trend'],
|
|
|
|
[4, 'track'],
|
|
|
|
[5, 'trial'],
|
|
|
|
[6, 'trip'],
|
|
|
|
[7, 'trie'],
|
|
|
|
[8, 'trick'],
|
|
|
|
[9, 'triangle']
|
|
|
|
]);
|
2023-12-21 02:26:45 +00:00
|
|
|
});
|
|
|
|
});
|