data-structure-typed 2.2.2 → 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.
- package/CHANGELOG.md +1 -1
- package/README.md +311 -1687
- package/README_CN.md +509 -0
- package/SECURITY.md +962 -11
- package/SECURITY.zh-CN.md +966 -0
- package/SPECIFICATION.md +689 -30
- package/SPECIFICATION.zh-CN.md +715 -0
- package/SPONSOR.zh-CN.md +62 -0
- package/SPONSOR_POLISHED.md +62 -0
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +215 -172
- package/dist/cjs/index.cjs +163 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +164 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +163 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +164 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +164 -0
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/test/performance/benchmark-runner.ts +14 -11
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
- package/test/performance/reportor.ts +2 -1
- package/test/performance/single-suite-runner.ts +7 -4
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +766 -8
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +89 -37
- package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
- package/test/unit/data-structures/heap/heap.test.ts +182 -47
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
- package/test/unit/data-structures/queue/deque.test.ts +98 -67
- package/test/unit/data-structures/queue/queue.test.ts +85 -51
- package/test/unit/data-structures/stack/stack.test.ts +142 -33
- package/test/unit/data-structures/trie/trie.test.ts +135 -39
- package/tsup.leetcode.config.js +99 -0
- package/typedoc.json +2 -1
- package/POSTS_zh-CN.md +0 -54
- package/README_zh-CN.md +0 -1208
- package/SPECIFICATION_zh-CN.md +0 -81
|
@@ -1680,6 +1680,87 @@ describe('Coverage boosters v2 - hit remaining statements', () => {
|
|
|
1680
1680
|
});
|
|
1681
1681
|
|
|
1682
1682
|
describe('Classic usage examples', () => {
|
|
1683
|
+
it('@example basic BinaryTree creation and insertion', () => {
|
|
1684
|
+
// Create a BinaryTree with entries
|
|
1685
|
+
const entries: [number, string][] = [
|
|
1686
|
+
[6, 'six'],
|
|
1687
|
+
[1, 'one'],
|
|
1688
|
+
[2, 'two'],
|
|
1689
|
+
[7, 'seven'],
|
|
1690
|
+
[5, 'five'],
|
|
1691
|
+
[3, 'three'],
|
|
1692
|
+
[4, 'four'],
|
|
1693
|
+
[9, 'nine'],
|
|
1694
|
+
[8, 'eight']
|
|
1695
|
+
];
|
|
1696
|
+
|
|
1697
|
+
const tree = new BinaryTree(entries);
|
|
1698
|
+
|
|
1699
|
+
// Verify size
|
|
1700
|
+
expect(tree.size).toBe(9);
|
|
1701
|
+
|
|
1702
|
+
// Add new element
|
|
1703
|
+
tree.add(10, 'ten');
|
|
1704
|
+
expect(tree.size).toBe(10);
|
|
1705
|
+
});
|
|
1706
|
+
|
|
1707
|
+
it('@example BinaryTree get and has operations', () => {
|
|
1708
|
+
const tree = new BinaryTree(
|
|
1709
|
+
[
|
|
1710
|
+
[5, 'five'],
|
|
1711
|
+
[3, 'three'],
|
|
1712
|
+
[7, 'seven'],
|
|
1713
|
+
[1, 'one'],
|
|
1714
|
+
[4, 'four'],
|
|
1715
|
+
[6, 'six'],
|
|
1716
|
+
[8, 'eight']
|
|
1717
|
+
],
|
|
1718
|
+
{ isMapMode: false }
|
|
1719
|
+
);
|
|
1720
|
+
|
|
1721
|
+
// Check if key exists
|
|
1722
|
+
expect(tree.has(5)).toBe(true);
|
|
1723
|
+
expect(tree.has(10)).toBe(false);
|
|
1724
|
+
|
|
1725
|
+
// Get value by key
|
|
1726
|
+
expect(tree.get(3)).toBe('three');
|
|
1727
|
+
expect(tree.get(7)).toBe('seven');
|
|
1728
|
+
expect(tree.get(100)).toBeUndefined();
|
|
1729
|
+
|
|
1730
|
+
// Get node structure
|
|
1731
|
+
const node = tree.getNode(5);
|
|
1732
|
+
expect(node?.key).toBe(5);
|
|
1733
|
+
expect(node?.value).toBe('five');
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
it('@example BinaryTree level-order traversal', () => {
|
|
1737
|
+
const tree = new BinaryTree([
|
|
1738
|
+
[1, 'one'],
|
|
1739
|
+
[2, 'two'],
|
|
1740
|
+
[3, 'three'],
|
|
1741
|
+
[4, 'four'],
|
|
1742
|
+
[5, 'five'],
|
|
1743
|
+
[6, 'six'],
|
|
1744
|
+
[7, 'seven']
|
|
1745
|
+
]);
|
|
1746
|
+
|
|
1747
|
+
// Binary tree maintains level-order insertion
|
|
1748
|
+
// Complete binary tree structure
|
|
1749
|
+
expect(tree.size).toBe(7);
|
|
1750
|
+
|
|
1751
|
+
// Verify all keys are present
|
|
1752
|
+
expect(tree.has(1)).toBe(true);
|
|
1753
|
+
expect(tree.has(4)).toBe(true);
|
|
1754
|
+
expect(tree.has(7)).toBe(true);
|
|
1755
|
+
|
|
1756
|
+
// Iterate through tree
|
|
1757
|
+
const keys: number[] = [];
|
|
1758
|
+
for (const [key] of tree) {
|
|
1759
|
+
keys.push(key);
|
|
1760
|
+
}
|
|
1761
|
+
expect(keys.length).toBe(7);
|
|
1762
|
+
});
|
|
1763
|
+
|
|
1683
1764
|
it('@example determine loan approval using a decision tree', () => {
|
|
1684
1765
|
// Decision tree structure
|
|
1685
1766
|
const loanDecisionTree = new BinaryTree<string>(
|
|
@@ -1743,4 +1824,89 @@ describe('Classic usage examples', () => {
|
|
|
1743
1824
|
|
|
1744
1825
|
expect(evaluate(expressionTree.root)).toBe(-27);
|
|
1745
1826
|
});
|
|
1827
|
+
|
|
1828
|
+
it('BinaryTree getHeight and structure queries', () => {
|
|
1829
|
+
const tree = new BinaryTree([
|
|
1830
|
+
[1, 'one'],
|
|
1831
|
+
[2, 'two'],
|
|
1832
|
+
[3, 'three'],
|
|
1833
|
+
[4, 'four'],
|
|
1834
|
+
[5, 'five'],
|
|
1835
|
+
[6, 'six'],
|
|
1836
|
+
[7, 'seven'],
|
|
1837
|
+
[8, 'eight'],
|
|
1838
|
+
[9, 'nine'],
|
|
1839
|
+
[10, 'ten'],
|
|
1840
|
+
[11, 'eleven'],
|
|
1841
|
+
[12, 'twelve'],
|
|
1842
|
+
[13, 'thirteen'],
|
|
1843
|
+
[14, 'fourteen'],
|
|
1844
|
+
[15, 'fifteen']
|
|
1845
|
+
]);
|
|
1846
|
+
|
|
1847
|
+
// Get overall tree height
|
|
1848
|
+
const height = tree.getHeight();
|
|
1849
|
+
expect(typeof height).toBe('number');
|
|
1850
|
+
expect(height).toBeGreaterThan(0);
|
|
1851
|
+
|
|
1852
|
+
// Complete binary tree with 15 nodes has height 3 (0-indexed from leaves)
|
|
1853
|
+
expect(height).toBe(3);
|
|
1854
|
+
|
|
1855
|
+
// Get height of specific node
|
|
1856
|
+
const heightOf2 = tree.getHeight(2);
|
|
1857
|
+
expect(typeof heightOf2).toBe('number');
|
|
1858
|
+
|
|
1859
|
+
// Root should have maximum height
|
|
1860
|
+
const heightOf1 = tree.getHeight(1);
|
|
1861
|
+
expect(heightOf1).toBe(height);
|
|
1862
|
+
});
|
|
1863
|
+
|
|
1864
|
+
it('BinaryTree for hierarchical data representation', () => {
|
|
1865
|
+
interface FileSystem {
|
|
1866
|
+
id: number;
|
|
1867
|
+
name: string;
|
|
1868
|
+
type: 'file' | 'folder';
|
|
1869
|
+
size: number;
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
// Binary trees are ideal for complete binary trees and heap-like structures
|
|
1873
|
+
// Here we use it for representing a file system hierarchy
|
|
1874
|
+
const fileSystem = new BinaryTree<number, FileSystem>([
|
|
1875
|
+
[1, { id: 1, name: 'root', type: 'folder', size: 1000 }],
|
|
1876
|
+
[2, { id: 2, name: 'documents', type: 'folder', size: 500 }],
|
|
1877
|
+
[3, { id: 3, name: 'images', type: 'folder', size: 400 }],
|
|
1878
|
+
[4, { id: 4, name: 'file1.txt', type: 'file', size: 10 }],
|
|
1879
|
+
[5, { id: 5, name: 'file2.txt', type: 'file', size: 20 }],
|
|
1880
|
+
[6, { id: 6, name: 'photo.jpg', type: 'file', size: 150 }],
|
|
1881
|
+
[7, { id: 7, name: 'video.mp4', type: 'file', size: 250 }]
|
|
1882
|
+
]);
|
|
1883
|
+
|
|
1884
|
+
expect(fileSystem.size).toBe(7);
|
|
1885
|
+
|
|
1886
|
+
// Quick lookup by ID
|
|
1887
|
+
const rootFolder = fileSystem.get(1);
|
|
1888
|
+
expect(rootFolder?.name).toBe('root');
|
|
1889
|
+
expect(rootFolder?.type).toBe('folder');
|
|
1890
|
+
|
|
1891
|
+
// Check existence
|
|
1892
|
+
expect(fileSystem.has(4)).toBe(true);
|
|
1893
|
+
expect(fileSystem.has(99)).toBe(false);
|
|
1894
|
+
|
|
1895
|
+
// Get node with children info
|
|
1896
|
+
const node = fileSystem.getNode(1);
|
|
1897
|
+
expect(node?.key).toBe(1);
|
|
1898
|
+
expect(node?.left).toBeDefined();
|
|
1899
|
+
expect(node?.right).toBeDefined();
|
|
1900
|
+
|
|
1901
|
+
// Calculate tree height
|
|
1902
|
+
const treeHeight = fileSystem.getHeight();
|
|
1903
|
+
expect(treeHeight).toBeGreaterThan(0);
|
|
1904
|
+
|
|
1905
|
+
// Add new file
|
|
1906
|
+
fileSystem.add(8, { id: 8, name: 'archive.zip', type: 'file', size: 300 });
|
|
1907
|
+
expect(fileSystem.size).toBe(8);
|
|
1908
|
+
|
|
1909
|
+
// Verify complete binary tree structure
|
|
1910
|
+
expect(fileSystem.has(8)).toBe(true);
|
|
1911
|
+
});
|
|
1746
1912
|
});
|