Skip to content

Commit

Permalink
[binary-tree] Update the testing API to the new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrwusa committed Oct 23, 2023
1 parent c177a4f commit 48e94f2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 102 deletions.
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-structure-typed",
"version": "1.37.1",
"version": "1.37.2",
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
"main": "dist/index.js",
"module": "lib/index.js",
Expand Down Expand Up @@ -28,7 +28,7 @@
"fix:test": "npm run lint:test && npm run format:test",
"fix": "npm run fix:src && npm run fix:test",
"update:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
"install:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
"install:all-individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
"test": "jest",
"check:deps": "dependency-cruiser src",
"changelog": "auto-changelog",
Expand Down Expand Up @@ -58,17 +58,17 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"auto-changelog": "^2.4.0",
"avl-tree-typed": "^1.36.9",
"avl-tree-typed": "^1.37.2",
"benchmark": "^2.1.4",
"binary-tree-typed": "^1.36.9",
"bst-typed": "^1.36.9",
"binary-tree-typed": "^1.37.2",
"bst-typed": "^1.37.2",
"dependency-cruiser": "^14.1.0",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.28.1",
"heap-typed": "^1.36.9",
"heap-typed": "^1.37.2",
"istanbul-badges-readme": "^1.8.5",
"jest": "^29.7.0",
"prettier": "^3.0.3",
Expand Down
7 changes: 1 addition & 6 deletions src/data-structures/binary-tree/binary-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import type {
MapCallback,
MapCallbackReturn
} from '../../types';
import {
BinaryTreeDeletedResult,
DFSOrderPattern,
FamilyPosition,
LoopType
} from '../../types';
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
import {IBinaryTree} from '../../interfaces';
import {trampoline} from '../../utils';
import {Queue} from '../queue';
Expand Down
44 changes: 23 additions & 21 deletions test/integration/avl-tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AVLTree} from 'avl-tree-typed';
import {AVLTree, CP} from 'avl-tree-typed';

describe('AVL Tree Test', () => {
it('should perform various operations on a AVL Tree', () => {
Expand All @@ -12,7 +12,7 @@ describe('AVL Tree Test', () => {
expect(node6 && tree.getHeight(node6)).toBe(3);
expect(node6 && tree.getDepth(node6)).toBe(1);

const getNodeById = tree.get(10, 'key');
const getNodeById = tree.get(10);
expect(getNodeById?.key).toBe(10);

const getMinNodeByRoot = tree.getLeftMost();
Expand All @@ -22,75 +22,77 @@ describe('AVL Tree Test', () => {
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
expect(getMinNodeBySpecificNode?.key).toBe(12);

const subTreeSum = node15 && tree.subTreeSum(node15);
let subTreeSum = 0;
node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), 15);
expect(subTreeSum).toBe(70);

const lesserSum = tree.lesserSum(10);
let lesserSum = 0;
tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
expect(lesserSum).toBe(45);

// 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);

const dfs = tree.dfs('in', 'node');
const dfs = tree.dfs(node => node, 'in');
expect(dfs[0].key).toBe(1);
expect(dfs[dfs.length - 1].key).toBe(16);

tree.perfectlyBalance();
const bfs = tree.bfs('node');
const bfs = tree.bfs(node => node);
expect(tree.isPerfectlyBalanced()).toBe(true);
expect(bfs[0].key).toBe(8);
expect(bfs[bfs.length - 1].key).toBe(16);

expect(tree.remove(11)[0].deleted?.key).toBe(11);
expect(tree.delete(11)[0].deleted?.key).toBe(11);
expect(tree.isAVLBalanced()).toBe(true);
expect(node15 && tree.getHeight(node15)).toBe(2);

expect(tree.remove(1)[0].deleted?.key).toBe(1);
expect(tree.delete(1)[0].deleted?.key).toBe(1);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(4);

expect(tree.remove(4)[0].deleted?.key).toBe(4);
expect(tree.delete(4)[0].deleted?.key).toBe(4);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(4);

expect(tree.remove(10)[0].deleted?.key).toBe(10);
expect(tree.delete(10)[0].deleted?.key).toBe(10);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);

expect(tree.remove(15)[0].deleted?.key).toBe(15);
expect(tree.delete(15)[0].deleted?.key).toBe(15);
expect(tree.isAVLBalanced()).toBe(true);

expect(tree.getHeight()).toBe(3);

expect(tree.remove(5)[0].deleted?.key).toBe(5);
expect(tree.delete(5)[0].deleted?.key).toBe(5);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);

expect(tree.remove(13)[0].deleted?.key).toBe(13);
expect(tree.delete(13)[0].deleted?.key).toBe(13);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);

expect(tree.remove(3)[0].deleted?.key).toBe(3);
expect(tree.delete(3)[0].deleted?.key).toBe(3);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);

expect(tree.remove(8)[0].deleted?.key).toBe(8);
expect(tree.delete(8)[0].deleted?.key).toBe(8);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(3);

expect(tree.remove(6)[0].deleted?.key).toBe(6);
expect(tree.remove(6).length).toBe(0);
expect(tree.delete(6)[0].deleted?.key).toBe(6);
expect(tree.delete(6).length).toBe(0);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);

expect(tree.remove(7)[0].deleted?.key).toBe(7);
expect(tree.delete(7)[0].deleted?.key).toBe(7);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);

expect(tree.remove(9)[0].deleted?.key).toBe(9);
expect(tree.delete(9)[0].deleted?.key).toBe(9);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(2);
expect(tree.remove(14)[0].deleted?.key).toBe(14);
expect(tree.delete(14)[0].deleted?.key).toBe(14);
expect(tree.isAVLBalanced()).toBe(true);
expect(tree.getHeight()).toBe(1);

Expand All @@ -100,7 +102,7 @@ describe('AVL Tree Test', () => {
expect(lastBFSIds[1]).toBe(2);
expect(lastBFSIds[2]).toBe(16);

const lastBFSNodes = tree.bfs('node');
const lastBFSNodes = tree.bfs(node => node);
expect(lastBFSNodes[0].key).toBe(12);
expect(lastBFSNodes[1].key).toBe(2);
expect(lastBFSNodes[2].key).toBe(16);
Expand Down
Loading

0 comments on commit 48e94f2

Please sign in to comment.