2024-01-15 04:12:01 +00:00
|
|
|
import { BST, BSTNode } from 'bst-typed';
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-08-30 16:18:13 +00:00
|
|
|
describe('Individual package BST operations test', () => {
|
2023-09-19 09:00:25 +00:00
|
|
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
2024-01-26 10:26:40 +00:00
|
|
|
const bst = new BST<number>();
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst).toBeInstanceOf(BST);
|
2023-11-27 05:59:26 +00:00
|
|
|
bst.add([11, 11]);
|
|
|
|
bst.add([3, 3]);
|
2023-10-30 12:27:08 +00:00
|
|
|
const idsOrValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
2023-12-22 11:59:38 +00:00
|
|
|
bst.addMany(idsOrValues, undefined);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.root).toBeInstanceOf(BSTNode);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-10-10 13:56:55 +00:00
|
|
|
if (bst.root) expect(bst.root.key).toBe(11);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.size).toBe(16);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.has(6)).toBe(true);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-09-19 09:00:25 +00:00
|
|
|
const node6 = bst.get(6);
|
|
|
|
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
|
|
expect(node6 && bst.getDepth(6)).toBe(3);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const nodeId10 = bst.getNode(10);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(nodeId10?.key).toBe(10);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const nodeVal9 = bst.getNode(9, node => node.value);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(nodeVal9?.key).toBe(9);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-09-19 09:00:25 +00:00
|
|
|
const leftMost = bst.getLeftMost();
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(leftMost?.key).toBe(1);
|
2023-08-30 06:27:18 +00:00
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const node15 = bst.getNode(15);
|
2023-09-19 09:00:25 +00:00
|
|
|
const minNodeBySpecificNode = node15 && bst.getLeftMost(node15);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
let subTreeSum = 0;
|
2024-01-14 11:49:50 +00:00
|
|
|
node15 && bst.dfs(node => (subTreeSum += node.key), 'IN', 15);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(subTreeSum).toBe(70);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
let lesserSum = 0;
|
2024-01-26 10:26:40 +00:00
|
|
|
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(lesserSum).toBe(45);
|
|
|
|
|
|
|
|
expect(node15).toBeInstanceOf(BSTNode);
|
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const node11 = bst.getNode(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(node11).toBeInstanceOf(BSTNode);
|
|
|
|
|
2024-01-14 11:49:50 +00:00
|
|
|
const dfsInorderNodes = bst.dfs(node => node, 'IN');
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(dfsInorderNodes[0].key).toBe(1);
|
|
|
|
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
bst.perfectlyBalance();
|
|
|
|
expect(bst.isPerfectlyBalanced()).toBe(true);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const bfsNodesAfterBalanced = bst.bfs(node => node);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
|
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed11 = bst.delete(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed11).toBeInstanceOf(Array);
|
|
|
|
expect(removed11[0]).toBeDefined();
|
|
|
|
expect(removed11[0].deleted).toBeDefined();
|
|
|
|
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
|
|
|
|
expect(bst.getHeight(15)).toBe(1);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed1 = bst.delete(1);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed1).toBeInstanceOf(Array);
|
|
|
|
expect(removed1[0]).toBeDefined();
|
|
|
|
expect(removed1[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
|
|
|
|
expect(bst.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed4 = bst.delete(4);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed4).toBeInstanceOf(Array);
|
|
|
|
expect(removed4[0]).toBeDefined();
|
|
|
|
expect(removed4[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
expect(bst.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed10 = bst.delete(10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed10).toBeInstanceOf(Array);
|
|
|
|
expect(removed10[0]).toBeDefined();
|
|
|
|
expect(removed10[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed15 = bst.delete(15);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed15).toBeInstanceOf(Array);
|
|
|
|
expect(removed15[0]).toBeDefined();
|
|
|
|
expect(removed15[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed5 = bst.delete(5);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed5).toBeInstanceOf(Array);
|
|
|
|
expect(removed5[0]).toBeDefined();
|
|
|
|
expect(removed5[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed13 = bst.delete(13);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed13).toBeInstanceOf(Array);
|
|
|
|
expect(removed13[0]).toBeDefined();
|
|
|
|
expect(removed13[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed3 = bst.delete(3);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed3).toBeInstanceOf(Array);
|
|
|
|
expect(removed3[0]).toBeDefined();
|
|
|
|
expect(removed3[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed8 = bst.delete(8);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed8).toBeInstanceOf(Array);
|
|
|
|
expect(removed8[0]).toBeDefined();
|
|
|
|
expect(removed8[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed6 = bst.delete(6);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed6).toBeInstanceOf(Array);
|
|
|
|
expect(removed6[0]).toBeDefined();
|
|
|
|
expect(removed6[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
2023-10-23 13:22:07 +00:00
|
|
|
expect(bst.delete(6).length).toBe(0);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed7 = bst.delete(7);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed7).toBeInstanceOf(Array);
|
|
|
|
expect(removed7[0]).toBeDefined();
|
|
|
|
expect(removed7[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed9 = bst.delete(9);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed9).toBeInstanceOf(Array);
|
|
|
|
expect(removed9[0]).toBeDefined();
|
|
|
|
expect(removed9[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed14 = bst.delete(14);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed14).toBeInstanceOf(Array);
|
|
|
|
expect(removed14[0]).toBeDefined();
|
|
|
|
expect(removed14[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
expect(bst.getHeight()).toBe(2);
|
|
|
|
|
|
|
|
expect(bst.isAVLBalanced()).toBe(false);
|
|
|
|
|
2023-10-11 15:36:10 +00:00
|
|
|
const bfsIDs = bst.bfs();
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bfsIDs[0]).toBe(2);
|
|
|
|
expect(bfsIDs[1]).toBe(12);
|
|
|
|
expect(bfsIDs[2]).toBe(16);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const bfsNodes = bst.bfs(node => node);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(bfsNodes[0].key).toBe(2);
|
|
|
|
expect(bfsNodes[1].key).toBe(12);
|
|
|
|
expect(bfsNodes[2].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
2023-12-07 01:16:25 +00:00
|
|
|
const objBST = new BST<number, { key: number; keyA: number }>();
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST).toBeInstanceOf(BST);
|
2023-11-27 05:59:26 +00:00
|
|
|
objBST.add([11, { key: 11, keyA: 11 }]);
|
|
|
|
objBST.add([3, { key: 3, keyA: 3 }]);
|
2023-11-27 10:01:59 +00:00
|
|
|
const values: [number, { key: number; keyA: number }][] = [
|
|
|
|
[15, { key: 15, keyA: 15 }],
|
2023-11-27 05:59:26 +00:00
|
|
|
[1, { key: 1, keyA: 1 }],
|
|
|
|
[8, { key: 8, keyA: 8 }],
|
2023-11-27 10:01:59 +00:00
|
|
|
[13, { key: 13, keyA: 13 }],
|
|
|
|
[16, { key: 16, keyA: 16 }],
|
2023-11-27 05:59:26 +00:00
|
|
|
[2, { key: 2, keyA: 2 }],
|
|
|
|
[6, { key: 6, keyA: 6 }],
|
|
|
|
[9, { key: 9, keyA: 9 }],
|
2023-11-27 10:01:59 +00:00
|
|
|
[12, { key: 12, keyA: 12 }],
|
|
|
|
[14, { key: 14, keyA: 14 }],
|
2023-11-27 05:59:26 +00:00
|
|
|
[4, { key: 4, keyA: 4 }],
|
|
|
|
[7, { key: 7, keyA: 7 }],
|
2023-11-27 10:01:59 +00:00
|
|
|
[10, { key: 10, keyA: 10 }],
|
2023-11-27 05:59:26 +00:00
|
|
|
[5, { key: 5, keyA: 5 }]
|
2023-09-26 11:43:45 +00:00
|
|
|
];
|
|
|
|
|
2023-12-22 11:59:38 +00:00
|
|
|
objBST.addMany(values, undefined);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
|
|
|
2023-10-10 13:56:55 +00:00
|
|
|
if (objBST.root) expect(objBST.root.key).toBe(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.has(6)).toBe(true);
|
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const node6 = objBST.getNode(6);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
|
|
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const nodeId10 = objBST.get(10);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(nodeId10?.key).toBe(10);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const nodeVal9 = objBST.get(9);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(nodeVal9?.key).toBe(9);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
const leftMost = objBST.getLeftMost();
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(leftMost?.key).toBe(1);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const node15 = objBST.getNode(15);
|
2023-11-16 02:14:14 +00:00
|
|
|
expect(node15?.value).toEqual({ key: 15, keyA: 15 });
|
2023-09-19 09:00:25 +00:00
|
|
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
let subTreeSum = 0;
|
2024-01-14 11:49:50 +00:00
|
|
|
node15 && objBST.dfs(node => (subTreeSum += node.key), 'IN', node15);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(subTreeSum).toBe(70);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
let lesserSum = 0;
|
2024-01-26 10:26:40 +00:00
|
|
|
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(lesserSum).toBe(45);
|
|
|
|
|
|
|
|
expect(node15).toBeInstanceOf(BSTNode);
|
|
|
|
|
2023-11-01 10:19:49 +00:00
|
|
|
const node11 = objBST.getNode(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(node11).toBeInstanceOf(BSTNode);
|
|
|
|
|
2024-01-14 11:49:50 +00:00
|
|
|
const dfsInorderNodes = objBST.dfs(node => node, 'IN');
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(dfsInorderNodes[0].key).toBe(1);
|
|
|
|
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
objBST.perfectlyBalance();
|
|
|
|
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const bfsNodesAfterBalanced = objBST.bfs(node => node);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
|
|
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed11 = objBST.delete(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed11).toBeInstanceOf(Array);
|
|
|
|
expect(removed11[0]).toBeDefined();
|
|
|
|
expect(removed11[0].deleted).toBeDefined();
|
|
|
|
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
|
|
|
|
expect(node15 && objBST.getHeight(node15)).toBe(2);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed1 = objBST.delete(1);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed1).toBeInstanceOf(Array);
|
|
|
|
expect(removed1[0]).toBeDefined();
|
|
|
|
expect(removed1[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
|
|
|
|
expect(objBST.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed4 = objBST.delete(4);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed4).toBeInstanceOf(Array);
|
|
|
|
expect(removed4[0]).toBeDefined();
|
|
|
|
expect(removed4[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
expect(objBST.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed10 = objBST.delete(10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed10).toBeInstanceOf(Array);
|
|
|
|
expect(removed10[0]).toBeDefined();
|
|
|
|
expect(removed10[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(4);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed15 = objBST.delete(15);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed15).toBeInstanceOf(Array);
|
|
|
|
expect(removed15[0]).toBeDefined();
|
|
|
|
expect(removed15[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed5 = objBST.delete(5);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed5).toBeInstanceOf(Array);
|
|
|
|
expect(removed5[0]).toBeDefined();
|
|
|
|
expect(removed5[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
2023-09-19 09:00:25 +00:00
|
|
|
|
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed13 = objBST.delete(13);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed13).toBeInstanceOf(Array);
|
|
|
|
expect(removed13[0]).toBeDefined();
|
|
|
|
expect(removed13[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed3 = objBST.delete(3);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed3).toBeInstanceOf(Array);
|
|
|
|
expect(removed3[0]).toBeDefined();
|
|
|
|
expect(removed3[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed8 = objBST.delete(8);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed8).toBeInstanceOf(Array);
|
|
|
|
expect(removed8[0]).toBeDefined();
|
|
|
|
expect(removed8[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(true);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed6 = objBST.delete(6);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed6).toBeInstanceOf(Array);
|
|
|
|
expect(removed6[0]).toBeDefined();
|
|
|
|
expect(removed6[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
2023-10-23 13:22:07 +00:00
|
|
|
expect(objBST.delete(6).length).toBe(0);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed7 = objBST.delete(7);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed7).toBeInstanceOf(Array);
|
|
|
|
expect(removed7[0]).toBeDefined();
|
|
|
|
expect(removed7[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed9 = objBST.delete(9);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed9).toBeInstanceOf(Array);
|
|
|
|
expect(removed9[0]).toBeDefined();
|
|
|
|
expect(removed9[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(3);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const removed14 = objBST.delete(14);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(removed14).toBeInstanceOf(Array);
|
|
|
|
expect(removed14[0]).toBeDefined();
|
|
|
|
expect(removed14[0].deleted).toBeDefined();
|
2023-10-10 13:56:55 +00:00
|
|
|
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
expect(objBST.getHeight()).toBe(2);
|
|
|
|
|
|
|
|
expect(objBST.isAVLBalanced()).toBe(false);
|
|
|
|
|
2023-10-11 15:36:10 +00:00
|
|
|
const bfsIDs = objBST.bfs();
|
2023-09-19 09:00:25 +00:00
|
|
|
expect(bfsIDs[0]).toBe(2);
|
|
|
|
expect(bfsIDs[1]).toBe(12);
|
|
|
|
expect(bfsIDs[2]).toBe(16);
|
|
|
|
|
2023-10-23 13:22:07 +00:00
|
|
|
const bfsNodes = objBST.bfs(node => node);
|
2023-10-10 13:56:55 +00:00
|
|
|
expect(bfsNodes[0].key).toBe(2);
|
|
|
|
expect(bfsNodes[1].key).toBe(12);
|
|
|
|
expect(bfsNodes[2].key).toBe(16);
|
2023-09-19 09:00:25 +00:00
|
|
|
});
|
2023-08-30 06:27:18 +00:00
|
|
|
});
|