data-structure-typed/test/integration/avl-tree.test.ts

109 lines
3.7 KiB
TypeScript
Raw Normal View History

import {AVLTree} from 'avl-tree-typed';
describe('AVL Tree Test', () => {
2023-09-19 09:00:25 +00:00
it('should perform various operations on a AVL Tree', () => {
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
const tree = new AVLTree();
2023-09-19 09:00:25 +00:00
for (const i of arr) tree.add(i, i);
2023-09-19 09:00:25 +00:00
const node6 = tree.get(6);
2023-09-19 09:00:25 +00:00
expect(node6 && tree.getHeight(node6)).toBe(3);
expect(node6 && tree.getDepth(node6)).toBe(1);
2023-09-19 09:00:25 +00:00
const getNodeById = tree.get(10, 'id');
expect(getNodeById?.id).toBe(10);
2023-09-19 09:00:25 +00:00
const getMinNodeByRoot = tree.getLeftMost();
expect(getMinNodeByRoot?.id).toBe(1);
2023-09-19 09:00:25 +00:00
const node15 = tree.get(15);
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
expect(getMinNodeBySpecificNode?.id).toBe(12);
2023-09-19 09:00:25 +00:00
const subTreeSum = node15 && tree.subTreeSum(node15);
expect(subTreeSum).toBe(70);
2023-09-19 09:00:25 +00:00
const lesserSum = tree.lesserSum(10);
expect(lesserSum).toBe(45);
2023-09-19 09:00:25 +00:00
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
expect(node15?.val).toBe(15);
2023-09-19 09:00:25 +00:00
const dfs = tree.DFS('in', 'node');
expect(dfs[0].id).toBe(1);
expect(dfs[dfs.length - 1].id).toBe(16);
2023-09-19 09:00:25 +00:00
tree.perfectlyBalance();
const bfs = tree.BFS('node');
expect(tree.isPerfectlyBalanced()).toBe(true);
expect(bfs[0].id).toBe(8);
expect(bfs[bfs.length - 1].id).toBe(16);
2023-09-27 02:10:11 +00:00
expect(tree.remove(11)[0].deleted?.id).toBe(11);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(node15 && tree.getHeight(node15)).toBe(2);
2023-09-27 02:10:11 +00:00
expect(tree.remove(1)[0].deleted?.id).toBe(1);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(4);
2023-09-27 02:10:11 +00:00
expect(tree.remove(4)[0].deleted?.id).toBe(4);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(4);
2023-09-27 02:10:11 +00:00
expect(tree.remove(10)[0].deleted?.id).toBe(10);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(15)[0].deleted?.id).toBe(15);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
2023-09-19 09:00:25 +00:00
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(5)[0].deleted?.id).toBe(5);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(13)[0].deleted?.id).toBe(13);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(3)[0].deleted?.id).toBe(3);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(8)[0].deleted?.id).toBe(8);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);
2023-09-27 02:10:11 +00:00
expect(tree.remove(6)[0].deleted?.id).toBe(6);
expect(tree.remove(6).length).toBe(0);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);
2023-09-27 02:10:11 +00:00
expect(tree.remove(7)[0].deleted?.id).toBe(7);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);
2023-09-27 02:10:11 +00:00
expect(tree.remove(9)[0].deleted?.id).toBe(9);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);
2023-09-27 02:10:11 +00:00
expect(tree.remove(14)[0].deleted?.id).toBe(14);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(1);
2023-09-19 09:00:25 +00:00
expect(tree.isAVLBalanced()).toBe(true);
const lastBFSIds = tree.BFS();
expect(lastBFSIds[0]).toBe(12);
expect(lastBFSIds[1]).toBe(2);
expect(lastBFSIds[2]).toBe(16);
2023-09-19 09:00:25 +00:00
const lastBFSNodes = tree.BFS('node');
expect(lastBFSNodes[0].id).toBe(12);
expect(lastBFSNodes[1].id).toBe(2);
expect(lastBFSNodes[2].id).toBe(16);
});
});