From 89dadfccc2a726b28fed68db395f279afb274cfe Mon Sep 17 00:00:00 2001 From: Revone Date: Thu, 25 Jan 2024 09:38:03 +0800 Subject: [PATCH] Refactor: Remove the unnecessary design of the extractor in the Binary Search Tree (BST). Utilize 1, -1, and 0 to replace the original comparison results GT, LT, and EQ. --- .../binary-tree/binary-tree.ts | 29 ++---- src/data-structures/binary-tree/bst.ts | 88 ++++++------------- src/data-structures/binary-tree/rb-tree.ts | 13 ++- src/types/common.ts | 2 +- .../binary-tree/binary-tree.ts | 3 +- test/integration/all-in-one.test.ts | 2 +- test/integration/avl-tree.test.ts | 2 +- test/integration/bst.test.ts | 4 +- .../binary-tree/avl-tree-multi-map.test.ts | 14 +-- .../binary-tree/avl-tree.test.ts | 4 +- .../data-structures/binary-tree/bst.test.ts | 10 +-- .../binary-tree/overall.test.ts | 34 ++++--- .../binary-tree/tree-multi-map.test.ts | 10 +-- 13 files changed, 80 insertions(+), 135 deletions(-) diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index caf9e9b..fb756c3 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -152,9 +152,8 @@ export class BinaryTree< constructor(keysOrNodesOrEntries: Iterable> = [], options?: BinaryTreeOptions) { super(); if (options) { - const { iterationType, extractor } = options; + const { iterationType } = options; if (iterationType) this.iterationType = iterationType; - if (extractor) this._extractor = extractor; } this._size = 0; @@ -162,16 +161,6 @@ export class BinaryTree< if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries); } - protected _extractor = (key: K | null | undefined) => (typeof key === 'number' ? key : Number(key)); - - /** - * The function returns the value of the `_extractor` property. - * @returns The `_extractor` property is being returned. - */ - get extractor() { - return this._extractor; - } - protected _root?: NODE | null; /** @@ -923,7 +912,7 @@ export class BinaryTree< if (iterationType === 'RECURSIVE') { const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => { if (!this.isRealNode(cur)) return true; - const numKey = this.extractor(cur.key); + const numKey = Number(cur.key); if (numKey <= min || numKey >= max) return false; return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max); }; @@ -943,7 +932,7 @@ export class BinaryTree< curr = curr.left; } curr = stack.pop()!; - const numKey = this.extractor(curr.key); + const numKey = Number(curr.key); if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false; prev = numKey; curr = curr.right; @@ -1875,24 +1864,24 @@ export class BinaryTree< let current: NODE | null | undefined = node; while (current || stack.length > 0) { - while (current && !isNaN(this.extractor(current.key))) { + while (this.isRealNode(current)) { stack.push(current); current = current.left; } current = stack.pop(); - if (current && !isNaN(this.extractor(current.key))) { + if (this.isRealNode(current)) { yield [current.key, current.value]; current = current.right; } } } else { - if (node.left && !isNaN(this.extractor(node.key))) { + if (node.left && this.isRealNode(node)) { yield* this[Symbol.iterator](node.left); } yield [node.key, node.value]; - if (node.right && !isNaN(this.extractor(node.key))) { + if (node.right && this.isRealNode(node)) { yield* this[Symbol.iterator](node.right); } } @@ -1921,13 +1910,13 @@ export class BinaryTree< return emptyDisplayLayout; } else if (node === undefined && !isShowUndefined) { return emptyDisplayLayout; - } else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) { + } else if (this.isNIL(node) && !isShowRedBlackNIL) { return emptyDisplayLayout; } else if (node !== null && node !== undefined) { // Display logic of normal nodes const key = node.key, - line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), + line = this.isNIL(node) ? 'S' : key.toString(), width = line.length; return _buildNodeDisplay( diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 0e4dcc5..e91c22e 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -273,7 +273,7 @@ export class BST< let current = this.root; while (current !== undefined) { - if (this._compare(current.key, newNode.key) === 'EQ') { + if (this._compare(current.key, newNode.key) === 0) { // if (current !== newNode) { // The key value is the same but the reference is different, update the value of the existing node this._replaceNode(current, newNode); @@ -285,7 +285,7 @@ export class BST< // return; // } - } else if (this._compare(current.key, newNode.key) === 'GT') { + } else if (this._compare(current.key, newNode.key) === 1) { if (current.left === undefined) { current.left = newNode; this._size++; @@ -367,17 +367,20 @@ export class BST< let sorted: BTNodePureExemplar[] = []; sorted = realBTNExemplars.sort((a, b) => { - let aR: number, bR: number; + let keyA: K | undefined | null, keyB: K | undefined | null; if (this.isEntry(a)) { - aR = this.extractor(a[0]); - } else if (this.isRealNode(a)) aR = this.extractor(a.key); - else aR = this.extractor(a); + keyA = a[0]; + } else if (this.isRealNode(a)) keyA = a.key; + else keyA = a; - if (this.isEntry(b)) bR = this.extractor(b[0]); - else if (this.isRealNode(b)) bR = this.extractor(b.key); - else bR = this.extractor(b); + if (this.isEntry(b)) keyB = b[0]; + else if (this.isRealNode(b)) keyB = b.key; + else keyB = b; - return aR - bR; + if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) { + return this._compare(keyA, keyB); + } + return 0; }); const _dfs = (arr: BTNodePureExemplar[]) => { @@ -468,8 +471,8 @@ export class BST< if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return; // TODO potential bug if (callback === this._DEFAULT_CALLBACK) { - if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') dfs(cur.left); - if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') dfs(cur.right); + if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 1) dfs(cur.left); + if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === -1) dfs(cur.right); } else { this.isRealNode(cur.left) && dfs(cur.left); this.isRealNode(cur.right) && dfs(cur.right); @@ -488,8 +491,8 @@ export class BST< } // TODO potential bug if (callback === this._DEFAULT_CALLBACK) { - if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right); - if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left); + if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === -1) stack.push(cur.right); + if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 1) stack.push(cur.left); // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right); // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left); @@ -724,7 +727,7 @@ export class BST< */ lesserOrGreaterTraverse>( callback: C = this._DEFAULT_CALLBACK as C, - lesserOrGreater: CP = 'LT', + lesserOrGreater: CP = -1, targetNode: KeyOrNodeOrEntry = this.root, iterationType: IterationType = this.iterationType ): ReturnType[] { @@ -900,57 +903,18 @@ export class BST< * is greater than, less than, or equal to the second value. * @param {K} a - The parameter "a" is of type K. * @param {K} b - The parameter "b" in the above code represents a K. - * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater - * than), 'LT' (less than), or 'EQ' (equal). + * @returns a value of type CP (ComparisonResult). The possible return values are '1' (greater + * than), -1 (less than), or 0 (equal). */ protected _compare(a: K, b: K): CP { if (this.variant === 'STANDARD') { - if (a > b) return 'GT'; - if (a < b) return 'LT'; - return 'EQ'; + if (a > b) return 1; + if (a < b) return -1; + return 0; } else { - if (a > b) return 'LT'; - if (a < b) return 'GT'; - return 'EQ'; + if (a > b) return -1; + if (a < b) return 1; + return 0; } - // const extractedA = this.extractor(a); - // const extractedB = this.extractor(b); - // const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA; - // - // if (compared > 0) return 'GT'; - // if (compared < 0) return 'LT'; - // return 'EQ'; - } - - /** - * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if - * `a` is less than `b` based on the specified variant. - * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the - * first value to be compared in the function. - * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one - * of the arguments for the comparison in the `_lt` function. - * @returns a boolean value. - */ - protected _lt(a: K, b: K): boolean { - return this.variant === 'STANDARD' ? a < b : a > b; - // const extractedA = this.extractor(a); - // const extractedB = this.extractor(b); - // return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB; - } - - /** - * The function compares two values using a custom extractor function and returns true if the first - * value is greater than the second value. - * @param {K} a - The parameter "a" is of type K, which means it can be any type. - * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one - * of the arguments for the comparison in the function. - * @returns a boolean value. - */ - protected _gt(a: K, b: K): boolean { - return this.variant === 'STANDARD' ? a > b : a < b; - - // const extractedA = this.extractor(a); - // const extractedB = this.extractor(b); - // return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB; } } diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index e0e353c..b3ff545 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -665,15 +665,12 @@ export class RedBlackTree< * is greater than, less than, or equal to the second value. * @param {K} a - The parameter "a" is of type K. * @param {K} b - The parameter "b" in the above code represents a K. - * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater - * than), 'LT' (less than), or 'EQ' (equal). + * @returns a value of type CP (ComparisonResult). The possible return values are '1' (greater + * than), -1 (less than), or 0 (equal). */ protected override _compare(a: K, b: K): CP { - const extractedA = this.extractor(a); - const extractedB = this.extractor(b); - const compared = extractedA - extractedB; - if (compared > 0) return 'GT'; - if (compared < 0) return 'LT'; - return 'EQ'; + if (a > b) return 1; + if (a < b) return -1; + return 0; } } diff --git a/src/types/common.ts b/src/types/common.ts index f479708..b6d32b9 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,5 +1,5 @@ export type BSTVariant = 'STANDARD' | 'INVERSE'; -export type CP = 'LT' | 'EQ' | 'GT'; +export type CP = 1 | -1 | 0; /** * Enum representing different loop types. diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index e3d2c0e..88c70f5 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -7,6 +7,5 @@ export type BinaryTreeNodeNested = BinaryTreeNode> = BinaryTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> export type BinaryTreeOptions = { - iterationType?: IterationType, - extractor?: (key: K | null | undefined) => number + iterationType?: IterationType } diff --git a/test/integration/all-in-one.test.ts b/test/integration/all-in-one.test.ts index 9806716..d5a4398 100644 --- a/test/integration/all-in-one.test.ts +++ b/test/integration/all-in-one.test.ts @@ -3,7 +3,7 @@ import { AVLTree } from 'data-structure-typed'; describe('AVL Tree Test from data-structure-typed', () => { it('should perform various operations on a AVL Tree from data-structure-typed', () => { const keys = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]; - const tree = new AVLTree(); + const tree = new AVLTree(); for (const i of keys) tree.add([i, i]); diff --git a/test/integration/avl-tree.test.ts b/test/integration/avl-tree.test.ts index 09af032..082824e 100644 --- a/test/integration/avl-tree.test.ts +++ b/test/integration/avl-tree.test.ts @@ -44,7 +44,7 @@ describe('AVL Tree Test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 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. diff --git a/test/integration/bst.test.ts b/test/integration/bst.test.ts index 61c915a..55f1ba3 100644 --- a/test/integration/bst.test.ts +++ b/test/integration/bst.test.ts @@ -38,7 +38,7 @@ describe('Individual package BST operations test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -235,7 +235,7 @@ describe('Individual package BST operations test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); diff --git a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts index de47a14..39d9be9 100644 --- a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts @@ -28,7 +28,7 @@ describe('AVLTreeMultiMap count', () => { [3, 3] ]); tm.add([2, 2], undefined, 10); - tm.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 1); + tm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 1); tm.delete(2); expect(tm.count).toBe(12); expect(tm.getComputedCount()).toBe(16); @@ -37,7 +37,7 @@ describe('AVLTreeMultiMap count', () => { describe('AVLTreeMultiMap operations test1', () => { it('should perform various operations on a Binary Search Tree with numeric values1', () => { - const treeMultimap = new AVLTreeMultiMap(); + const treeMultimap = new AVLTreeMultiMap(); expect(treeMultimap instanceof AVLTreeMultiMap); treeMultimap.add([11, 11]); @@ -94,7 +94,7 @@ describe('AVLTreeMultiMap operations test1', () => { node15 && treeMultimap.dfs(node => (subTreeSum += node.key), 'PRE', 15); expect(subTreeSum).toBe(31); let lesserSum = 0; - treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode) => (lesserSum += node.key), 'LT', 10); + treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode) => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15 instanceof AVLTreeMultiMapNode); @@ -105,7 +105,7 @@ describe('AVLTreeMultiMap operations test1', () => { const node11 = treeMultimap.getNode(11); expect(node11 instanceof AVLTreeMultiMapNode); if (node11 instanceof AVLTreeMultiMapNode) { - const allGreaterNodesAdded = treeMultimap.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 11); + const allGreaterNodesAdded = treeMultimap.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11); expect(allGreaterNodesAdded); } @@ -348,7 +348,7 @@ describe('AVLTreeMultiMap operations test recursively1', () => { node15 && treeMultimap.dfs(node => (subTreeSum += node.key), 'PRE', 15); expect(subTreeSum).toBe(31); let lesserSum = 0; - treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode) => (lesserSum += node.key), 'LT', 10); + treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode) => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15 instanceof AVLTreeMultiMapNode); @@ -359,7 +359,7 @@ describe('AVLTreeMultiMap operations test recursively1', () => { const node11 = treeMultimap.getNode(11); expect(node11 instanceof AVLTreeMultiMapNode); if (node11 instanceof AVLTreeMultiMapNode) { - const allGreaterNodesAdded = treeMultimap.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 11); + const allGreaterNodesAdded = treeMultimap.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11); expect(allGreaterNodesAdded); } @@ -564,7 +564,7 @@ describe('AVLTreeMultiMap Performance test', function () { } isDebug && console.log('---add', performance.now() - start); const startL = performance.now(); - treeMS.lesserOrGreaterTraverse(node => (node.count += 1), 'LT', inputSize / 2); + treeMS.lesserOrGreaterTraverse(node => (node.count += 1), -1, inputSize / 2); isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL); }); diff --git a/test/unit/data-structures/binary-tree/avl-tree.test.ts b/test/unit/data-structures/binary-tree/avl-tree.test.ts index e1b5677..71306e5 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -28,7 +28,7 @@ describe('AVL Tree Test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 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. @@ -136,7 +136,7 @@ describe('AVL Tree Test recursively', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 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. diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index b6ed641..725df2e 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -58,7 +58,7 @@ describe('BST operations test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -261,7 +261,7 @@ describe('BST operations test', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -448,7 +448,7 @@ describe('BST operations test recursively', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -649,7 +649,7 @@ describe('BST operations test recursively', () => { expect(subTreeSum).toBe(70); let lesserSum = 0; - objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15).toBeInstanceOf(BSTNode); @@ -902,7 +902,7 @@ describe('BST Performance test', function () { node => { node.key - 1; }, - 'LT', + -1, inputSize / 2 ); isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL); diff --git a/test/unit/data-structures/binary-tree/overall.test.ts b/test/unit/data-structures/binary-tree/overall.test.ts index 33db622..64fc6b5 100644 --- a/test/unit/data-structures/binary-tree/overall.test.ts +++ b/test/unit/data-structures/binary-tree/overall.test.ts @@ -64,16 +64,15 @@ describe('Overall BinaryTree Test', () => { it('Should clone a BST works fine', () => { const bst = new BST([3, 6, 7, 1, 9], { iterationType: 'RECURSIVE', - variant: 'INVERSE', - extractor: key => Number(key) + variant: 'INVERSE' }); expect(bst.size).toBe(5); expect(bst.root?.key).toBe(6); - expect(bst.root?.left?.key).toBe(7); - expect(bst.root?.left?.left?.key).toBe(9); - expect(bst.root?.right?.key).toBe(1); - expect(bst.root?.right?.left?.key).toBe(3); - expect(bst.getNodeByKey(7)?.left?.key).toBe(9); + expect(bst.root?.left?.key).toBe(9); + expect(bst.root?.left?.right?.key).toBe(7); + expect(bst.root?.right?.key).toBe(3); + expect(bst.root?.right?.right?.key).toBe(1); + expect(bst.getNodeByKey(9)?.right?.key).toBe(7); expect(bst.getHeight()).toBe(2); expect(bst.has(9)).toBe(true); expect(bst.has(7)).toBe(true); @@ -82,31 +81,30 @@ describe('Overall BinaryTree Test', () => { expect(bst.size).toBe(4); expect(bst.root?.key).toBe(6); expect(bst.root?.left?.key).toBe(9); - expect(bst.root?.right?.key).toBe(1); - expect(bst.root?.right?.left?.key).toBe(3); + expect(bst.root?.right?.key).toBe(3); + expect(bst.root?.right?.right?.key).toBe(1); expect(bst.getNodeByKey(6)?.left?.key).toBe(9); expect(bst.getHeight()).toBe(2); expect(bst.has(9)).toBe(true); expect(bst.has(7)).toBe(false); - expect(bst.bfs()).toEqual([6, 9, 1, 3]); + expect(bst.bfs()).toEqual([6, 9, 3, 1]); const clonedBST = bst.clone(); expect(clonedBST.size).toBe(4); expect(clonedBST.root?.key).toBe(6); expect(clonedBST.root?.left?.key).toBe(9); - expect(clonedBST.root?.right?.key).toBe(1); - expect(clonedBST.root?.right?.left?.key).toBe(3); + expect(clonedBST.root?.right?.key).toBe(3); + expect(clonedBST.root?.right?.right?.key).toBe(1); expect(clonedBST.getNodeByKey(6)?.left?.key).toBe(9); expect(clonedBST.getHeight()).toBe(2); expect(clonedBST.has(9)).toBe(true); expect(clonedBST.has(7)).toBe(false); - expect(clonedBST.bfs()).toEqual([6, 9, 1, 3]); + expect(clonedBST.bfs()).toEqual([6, 9, 3, 1]); }); it('Should clone a AVLTree works fine', () => { const avl = new AVLTree([3, 6, 7, 1, 9], { iterationType: 'RECURSIVE', - variant: 'INVERSE', - extractor: key => Number(key) + variant: 'INVERSE' }); expect(avl.size).toBe(5); avl.add(2); @@ -148,8 +146,7 @@ describe('Overall BinaryTree Test', () => { it('Should clone a TreeMultiMap works fine', () => { const tmm = new TreeMultiMap([3, 6, 7, 1, 9], { - iterationType: 'RECURSIVE', - extractor: key => Number(key) + iterationType: 'RECURSIVE' }); expect(tmm.size).toBe(5); tmm.add(2); @@ -197,8 +194,7 @@ describe('Overall BinaryTree Test', () => { it('Should clone a RedBlackTree works fine', () => { const rbTree = new RedBlackTree([3, 6, 7, 1, 9], { - iterationType: 'RECURSIVE', - extractor: key => Number(key) + iterationType: 'RECURSIVE' }); expect(rbTree.size).toBe(5); rbTree.add(2); diff --git a/test/unit/data-structures/binary-tree/tree-multi-map.test.ts b/test/unit/data-structures/binary-tree/tree-multi-map.test.ts index 3a0751b..f0a762e 100644 --- a/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +++ b/test/unit/data-structures/binary-tree/tree-multi-map.test.ts @@ -32,7 +32,7 @@ describe('TreeMultiMap count', () => { [2, 2], [3, 3] ]); - tmm.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 1); + tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 1); expect(tmm.getComputedCount()).toBe(7); expect(tmm.count).toBe(3); }); @@ -152,7 +152,7 @@ describe('TreeMultiMap operations test1', () => { node15 && tmm.dfs(node => (subTreeSum += node.key), 'PRE', 15); expect(subTreeSum).toBe(45); let lesserSum = 0; - tmm.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10); + tmm.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10); expect(lesserSum).toBe(45); expect(node15 instanceof TreeMultiMapNode); @@ -163,7 +163,7 @@ describe('TreeMultiMap operations test1', () => { const node11 = tmm.getNode(11); expect(node11 instanceof TreeMultiMapNode); if (node11 instanceof TreeMultiMapNode) { - const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 11); + const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11); expect(allGreaterNodesAdded); } @@ -414,7 +414,7 @@ describe('TreeMultiMap operations test recursively1', () => { lesserSum += node.key; return node.key; }, - 'LT', + -1, 10 ); expect(lesserSum).toBe(45); @@ -427,7 +427,7 @@ describe('TreeMultiMap operations test recursively1', () => { const node11 = tmm.getNode(11); expect(node11 instanceof TreeMultiMapNode); if (node11 instanceof TreeMultiMapNode) { - const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 'GT', 11); + const allGreaterNodesAdded = tmm.lesserOrGreaterTraverse(node => (node.count += 2), 1, 11); expect(allGreaterNodesAdded); }