mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
379 lines
11 KiB
HTML
379 lines
11 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'>
|
|
<head>
|
|
<meta charset='UTF-8'>
|
|
<title>CDN Test</title>
|
|
<!-- <script src="../../dist/umd/data-structure-typed.min.js"></script>-->
|
|
<!-- <script src="../../dist/umd/data-structure-typed.js"></script>-->
|
|
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.42.2/dist/umd/data-structure-typed.min.js'></script>-->
|
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.43.3/dist/umd/data-structure-typed.min.js'></script>-->
|
|
<!-- <script src='https://cdn.jsdelivr.net/npm/data-structure-typed@1.44.0/dist/umd/data-structure-typed.min.js'></script>-->
|
|
|
|
<script src='https://unpkg.com/js-sdsl@4.4.2/dist/umd/js-sdsl.js'></script>
|
|
</head>
|
|
<body>
|
|
<div id='app'>
|
|
<ul class='modules'>
|
|
|
|
</ul>
|
|
</div>
|
|
<script defer>
|
|
|
|
const $modules = document.querySelector('.modules');
|
|
|
|
try {
|
|
const { Queue } = dataStructureTyped;
|
|
const queue = new Queue();
|
|
const n = 1000000;
|
|
const startEn = performance.now();
|
|
for (let i = 0; i < n; i++) {
|
|
queue.enqueue(i);
|
|
}
|
|
console.log((performance.now() - startEn).toFixed(2), `Queue ${n.toLocaleString()} enqueue `);
|
|
let last = 0;
|
|
const startTime = performance.now();
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
last = queue.dequeue();
|
|
}
|
|
|
|
console.log((performance.now() - startTime).toFixed(2), `Queue ${n.toLocaleString()} dequeue `);
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
try {
|
|
const { AVLTree } = dataStructureTyped;
|
|
|
|
const avlTree = new AVLTree();
|
|
const $avlTree = document.createElement('li');
|
|
const $avlTreeSpan = document.createElement('span');
|
|
$avlTreeSpan.innerText = 'AVLTree';
|
|
$avlTree.append($avlTreeSpan);
|
|
for (let i = 1; i < 31; i++) {
|
|
avlTree.add(i, i);
|
|
}
|
|
console.log(avlTree.bfs(), `avlTree.bfs()`);
|
|
avlTree.print();
|
|
$modules.append($avlTree);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
try {
|
|
const { BinaryTree } = dataStructureTyped;
|
|
const tree = new BinaryTree();
|
|
tree.add(3);
|
|
tree.add(12);
|
|
tree.addMany([1, 6, 9, 8, 5, 2, 3, 4, 7])
|
|
tree.add(10);
|
|
console.log(tree.isPerfectlyBalanced(), `tree.isPerfectlyBalanced()`);
|
|
tree.print(undefined, { isShowUndefined: true });
|
|
|
|
const node3 = tree.getNode(3);
|
|
if (node3) node3.right = tree.createNode(1);
|
|
console.log(tree.isPerfectlyBalanced(), `tree.isPerfectlyBalanced()`);
|
|
tree.print();
|
|
|
|
tree.clear();
|
|
tree.addMany([1, null, 2, null, 3, null, 4, null, 5, null, 6, null]);
|
|
console.log(tree.isPerfectlyBalanced(), `tree.isPerfectlyBalanced()`);
|
|
tree.print(undefined, { isShowNull: true });
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
|
|
try {
|
|
const { OrderedMap } = sdsl;
|
|
const { RedBlackTree } = dataStructureTyped;
|
|
const cRBTree = new OrderedMap();
|
|
const rbTree = new RedBlackTree();
|
|
const tS = performance.now();
|
|
const n = 100000;
|
|
for (let i = 0; i < n; i++) {
|
|
rbTree.add(i, i);
|
|
}
|
|
console.log((performance.now() - tS).toFixed(2), `RedBlackTree ${n.toLocaleString()} add`);
|
|
console.log(`rbTree.size`, rbTree.size);
|
|
for (let i = 0; i < n - 8; i++) {
|
|
rbTree.delete(i)
|
|
}
|
|
rbTree.print(rbTree.root, { isShowRedBlackNIL: true });
|
|
const cS = performance.now();
|
|
for (let i = 1; i < 100000; i++) {
|
|
cRBTree.setElement(i, i);
|
|
}
|
|
console.log((performance.now() - cS).toFixed(2), `CRedBlackTree ${n.toLocaleString()} add`);
|
|
console.log(cRBTree.size(), `cRBTree.size()`);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
try {
|
|
const { PriorityQueue: CPriorityQueue } = sdsl;
|
|
const { PriorityQueue } = dataStructureTyped;
|
|
const pq = new PriorityQueue({ comparator: (a, b) => b - a });
|
|
|
|
const tS = performance.now();
|
|
const n = 1000000;
|
|
for (let i = 0; i < n; i++) {
|
|
pq.add(i);
|
|
}
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
pq.pop();
|
|
}
|
|
console.log((performance.now() - tS).toFixed(2), `PriorityQueue ${n.toLocaleString()} add`);
|
|
console.log(pq.size, `pq.size`);
|
|
const cS = performance.now();
|
|
const cpq = new CPriorityQueue();
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
cpq.push(i);
|
|
}
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
cpq.pop();
|
|
}
|
|
console.log((performance.now() - cS).toFixed(), `CPriorityQueue ${n.toLocaleString()} add`);
|
|
console.log(cpq.size(), `cpq.size()`);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
try {
|
|
const { BST, RedBlackTree, AVLTree } = dataStructureTyped;
|
|
|
|
const bst = new BST();
|
|
bst.add(11);
|
|
bst.add(3);
|
|
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
bst.size === 16; // true
|
|
bst.has(6); // true
|
|
const node6 = bst.getNode(6); // BSTNode
|
|
bst.getHeight(6) === 2; // true
|
|
bst.getHeight() === 5; // true
|
|
bst.getDepth(6) === 3; // true
|
|
|
|
bst.getLeftMost()?.key === 1; // true
|
|
|
|
bst.delete(6);
|
|
bst.get(6); // undefined
|
|
bst.isAVLBalanced(); // true
|
|
bst.bfs()[0] === 11; // true
|
|
bst.print();
|
|
|
|
const objBST = new BST();
|
|
|
|
objBST.add(11, { "name": "Pablo", "age": 15 });
|
|
objBST.add(3, { "name": "Kirk", "age": 1 });
|
|
|
|
objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
|
|
{ "name": "Alice", "age": 15 },
|
|
{ "name": "Bob", "age": 1 },
|
|
{ "name": "Charlie", "age": 8 },
|
|
{ "name": "David", "age": 13 },
|
|
{ "name": "Emma", "age": 16 },
|
|
{ "name": "Frank", "age": 2 },
|
|
{ "name": "Grace", "age": 6 },
|
|
{ "name": "Hannah", "age": 9 },
|
|
{ "name": "Isaac", "age": 12 },
|
|
{ "name": "Jack", "age": 14 },
|
|
{ "name": "Katie", "age": 4 },
|
|
{ "name": "Liam", "age": 7 },
|
|
{ "name": "Mia", "age": 10 },
|
|
{ "name": "Noah", "age": 5 }
|
|
]
|
|
);
|
|
objBST.print()
|
|
|
|
objBST.delete(11);
|
|
objBST.print()
|
|
|
|
|
|
const rbTree = new RedBlackTree();
|
|
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
rbTree.isAVLBalanced(); // true
|
|
rbTree.delete(10);
|
|
rbTree.isAVLBalanced(); // true
|
|
console.log(`rbTree.delete(10)`);
|
|
rbTree.print();
|
|
|
|
rbTree.delete(14);
|
|
rbTree.print()
|
|
|
|
const avlTree = new AVLTree();
|
|
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
avlTree.isAVLBalanced(); // true
|
|
avlTree.delete(10);
|
|
avlTree.isAVLBalanced(); // true
|
|
avlTree.print()
|
|
avlTree.delete(14);
|
|
avlTree.print()
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
try {
|
|
const {
|
|
AVLTree,
|
|
BinaryTree,
|
|
BST,
|
|
Deque,
|
|
DoublyLinkedList,
|
|
HashMap,
|
|
Heap,
|
|
MaxPriorityQueue,
|
|
MinHeap,
|
|
MinPriorityQueue,
|
|
Queue,
|
|
RedBlackTree,
|
|
SinglyLinkedList,
|
|
Stack,
|
|
TreeMultimap,
|
|
Trie
|
|
} = dataStructureTyped;
|
|
const orgArr = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
|
const orgStrArr = ["trie", "trial", "trick", "trip", "tree", "trend", "triangle", "track", "trace", "transmit"];
|
|
const entries = [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]];
|
|
|
|
const queue = new Queue(orgArr);
|
|
queue.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
|
|
const deque = new Deque(orgArr);
|
|
deque.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
|
|
const sList = new SinglyLinkedList(orgArr);
|
|
sList.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
|
|
const dList = new DoublyLinkedList(orgArr);
|
|
dList.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
|
|
const stack = new Stack(orgArr);
|
|
stack.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
|
|
const minHeap = new MinHeap(orgArr);
|
|
minHeap.print(); // [1, 5, 2, 7, 6, 3, 4, 9, 8]
|
|
|
|
const maxPQ = new MaxPriorityQueue(orgArr);
|
|
maxPQ.print(); // [9, 8, 4, 7, 5, 2, 3, 1, 6]
|
|
|
|
const biTree = new BinaryTree(entries);
|
|
biTree.print();
|
|
// ___6___
|
|
// / \
|
|
// ___1_ _2_
|
|
// / \ / \
|
|
// _7_ 5 3 4
|
|
// / \
|
|
// 9 8
|
|
|
|
const bst = new BST(entries);
|
|
bst.print();
|
|
// _____5___
|
|
// / \
|
|
// _2_ _7_
|
|
// / \ / \
|
|
// 1 3_ 6 8_
|
|
// \ \
|
|
// 4 9
|
|
|
|
|
|
const rbTree = new RedBlackTree(entries);
|
|
rbTree.print();
|
|
// ___4___
|
|
// / \
|
|
// _2_ _6___
|
|
// / \ / \
|
|
// 1 3 5 _8_
|
|
// / \
|
|
// 7 9
|
|
|
|
|
|
const avl = new AVLTree(entries);
|
|
avl.print();
|
|
// ___4___
|
|
// / \
|
|
// _2_ _6___
|
|
// / \ / \
|
|
// 1 3 5 _8_
|
|
// / \
|
|
// 7 9
|
|
|
|
const treeMulti = new TreeMultimap(entries);
|
|
treeMulti.print();
|
|
// ___4___
|
|
// / \
|
|
// _2_ _6___
|
|
// / \ / \
|
|
// 1 3 5 _8_
|
|
// / \
|
|
// 7 9
|
|
|
|
const hm = new HashMap(entries);
|
|
hm.print() // [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]]
|
|
|
|
const rbTreeH = new RedBlackTree(hm);
|
|
rbTreeH.print();
|
|
// ___4___
|
|
// / \
|
|
// _2_ _6___
|
|
// / \ / \
|
|
// 1 3 5 _8_
|
|
// / \
|
|
// 7 9
|
|
|
|
const pq = new MinPriorityQueue(orgArr);
|
|
pq.print(); // [1, 5, 2, 7, 6, 3, 4, 9, 8]
|
|
|
|
const bst1 = new BST(pq);
|
|
bst1.print();
|
|
// _____5___
|
|
// / \
|
|
// _2_ _7_
|
|
// / \ / \
|
|
// 1 3_ 6 8_
|
|
// \ \
|
|
// 4 9
|
|
|
|
const dq1 = new Deque(orgArr);
|
|
dq1.print(); // [6, 1, 2, 7, 5, 3, 4, 9, 8]
|
|
const rbTree1 = new RedBlackTree(dq1);
|
|
rbTree1.print();
|
|
// _____5___
|
|
// / \
|
|
// _2___ _7___
|
|
// / \ / \
|
|
// 1 _4 6 _9
|
|
// / /
|
|
// 3 8
|
|
|
|
|
|
const trie2 = new Trie(orgStrArr);
|
|
trie2.print(); // ['trie', 'trial', 'triangle', 'trick', 'trip', 'tree', 'trend', 'track', 'trace', 'transmit']
|
|
const heap2 = new Heap(trie2, { comparator: (a, b) => Number(a) - Number(b) });
|
|
heap2.print(); // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
|
|
const dq2 = new Deque(heap2);
|
|
dq2.print(); // ['transmit', 'trace', 'tree', 'trend', 'track', 'trial', 'trip', 'trie', 'trick', 'triangle']
|
|
const entries2 = dq2.map((el, i) => [i, el]);
|
|
const avl2 = new AVLTree(entries2);
|
|
avl2.print();
|
|
// ___3_______
|
|
// / \
|
|
// _1_ ___7_
|
|
// / \ / \
|
|
// 0 2 _5_ 8_
|
|
// / \ \
|
|
// 4 6 9
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|
|
|