From 5adf75f99c63b750651b4dff9039de4ee861ced4 Mon Sep 17 00:00:00 2001 From: Revone Date: Sun, 3 Nov 2024 16:38:07 +1300 Subject: [PATCH] =?UTF-8?q?fix:=20Fix=20the=20bug=20where=20the=20binary?= =?UTF-8?q?=20tree=20repeatedly=20adds=20elements=20with=20the=20same=20ke?= =?UTF-8?q?y=20in=20map=20mode=20and=20the=20bug=20where=20the=20node?= =?UTF-8?q?=E2=80=99s=20value=20is=20instantiated=20simultaneously.=20Enab?= =?UTF-8?q?le=20map=20mode=20by=20default.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- package-lock.json | 4 +- .../binary-tree/avl-tree-multi-map.ts | 2 +- src/data-structures/binary-tree/avl-tree.ts | 2 +- .../binary-tree/binary-tree.ts | 4 +- src/data-structures/binary-tree/bst.ts | 7 ++- src/data-structures/binary-tree/rb-tree.ts | 9 ++- .../binary-tree/tree-multi-map.ts | 2 +- .../binary-tree/avl-tree-multi-map.ts | 4 +- .../data-structures/binary-tree/avl-tree.ts | 4 +- .../binary-tree/binary-tree.ts | 12 ++-- src/types/data-structures/binary-tree/bst.ts | 8 +-- .../data-structures/binary-tree/rb-tree.ts | 4 +- .../binary-tree/tree-multi-map.ts | 4 +- .../binary-tree/avl-tree-multi-map.test.ts | 16 ++--- .../binary-tree/avl-tree.test.ts | 37 +++++++++--- .../binary-tree/binary-tree.test.ts | 58 ++++++++++++------- .../data-structures/binary-tree/bst.test.ts | 47 ++++++++++----- .../binary-tree/rb-tree.test.ts | 19 ++++++ .../binary-tree/tree-multi-map.test.ts | 22 +++---- 20 files changed, 172 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b8d76c..e566c8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.52.9](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming) +## [v1.53.0](https://github.com/zrwusa/data-structure-typed/compare/v1.51.5...main) (upcoming) ### Changes diff --git a/package-lock.json b/package-lock.json index 6141a25..848a4d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.52.9", + "version": "1.53.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.52.9", + "version": "1.53.0", "license": "MIT", "devDependencies": { "@eslint/compat": "^1.2.2", diff --git a/src/data-structures/binary-tree/avl-tree-multi-map.ts b/src/data-structures/binary-tree/avl-tree-multi-map.ts index 990e70f..3d8afcf 100644 --- a/src/data-structures/binary-tree/avl-tree-multi-map.ts +++ b/src/data-structures/binary-tree/avl-tree-multi-map.ts @@ -129,7 +129,7 @@ export class AVLTreeMultiMap< * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE. */ override createNode(key: K, value?: V, count?: number): NODE { - return new AVLTreeMultiMapNode(key, value, count) as NODE; + return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count) as NODE; } /** diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index e889e38..c7369f1 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -99,7 +99,7 @@ export class AVLTree< * type NODE. */ override createNode(key: K, value?: V): NODE { - return new AVLTreeNode(key, value) as NODE; + return new AVLTreeNode(key, this._isMapMode ? undefined : value) as NODE; } /** diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 95dae8d..0b94a5f 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -135,7 +135,7 @@ export class BinaryTree< if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws); } - protected _isMapMode = false; + protected _isMapMode = true; get isMapMode() { return this._isMapMode; @@ -181,7 +181,7 @@ export class BinaryTree< * as NODE. */ createNode(key: K, value?: V): NODE { - return new BinaryTreeNode(key, value) as NODE; + return new BinaryTreeNode(key, this._isMapMode ? undefined : value) as NODE; } /** diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index de70378..f5827fe 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -141,7 +141,7 @@ export class BST< * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type. */ override createNode(key: K, value?: V): NODE { - return new BSTNode(key, value) as NODE; + return new BSTNode(key, this._isMapMode ? undefined : value) as NODE; } /** @@ -174,9 +174,9 @@ export class BST< keyNodeEntryOrRaw: BTNRep | R, value?: V ): [OptNode, V | undefined] { - const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); + const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value); if (node === null) return [undefined, undefined]; - return [node, tValue ?? value]; + return [node, value ?? entryValue]; } /** @@ -250,6 +250,7 @@ export class BST< while (current !== undefined) { if (this.comparator(current.key, newNode.key) === 0) { this._replaceNode(current, newNode); + if (this._isMapMode) this._setValue(current.key, newValue); return true; } else if (this.comparator(current.key, newNode.key) > 0) { if (current.left === undefined) { diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 9f52c17..93644f5 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -106,7 +106,7 @@ export class RedBlackTree< * returned. */ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE { - return new RedBlackTreeNode(key, value, color) as NODE; + return new RedBlackTreeNode(key, this._isMapMode ? undefined : value, color) as NODE; } /** @@ -218,7 +218,12 @@ export class RedBlackTree< if (this._isMapMode) this._setValue(newNode.key, newValue); this._size++; return true; - } else return insertStatus === 'UPDATED'; + } + if (insertStatus === 'UPDATED') { + if (this._isMapMode) this._setValue(newNode.key, newValue); + return true; + } + return false; } /** diff --git a/src/data-structures/binary-tree/tree-multi-map.ts b/src/data-structures/binary-tree/tree-multi-map.ts index 59e8488..bdbd1d1 100644 --- a/src/data-structures/binary-tree/tree-multi-map.ts +++ b/src/data-structures/binary-tree/tree-multi-map.ts @@ -124,7 +124,7 @@ export class TreeMultiMap< * @returns A new instance of the TreeMultiMapNode class, casted as NODE. */ override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE { - return new TreeMultiMapNode(key, value, count, color) as NODE; + return new TreeMultiMapNode(key, this._isMapMode ? undefined : value, count, color) as NODE; } /** diff --git a/src/types/data-structures/binary-tree/avl-tree-multi-map.ts b/src/types/data-structures/binary-tree/avl-tree-multi-map.ts index 0f418b0..8ebad8a 100644 --- a/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +++ b/src/types/data-structures/binary-tree/avl-tree-multi-map.ts @@ -1,5 +1,5 @@ -import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures'; -import type { AVLTreeOptions } from './avl-tree'; +import {AVLTreeMultiMap, AVLTreeMultiMapNode} from '../../../data-structures'; +import type {AVLTreeOptions} from './avl-tree'; export type AVLTreeMultiMapNodeNested = AVLTreeMultiMapNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/avl-tree.ts b/src/types/data-structures/binary-tree/avl-tree.ts index b84b3b6..1576651 100644 --- a/src/types/data-structures/binary-tree/avl-tree.ts +++ b/src/types/data-structures/binary-tree/avl-tree.ts @@ -1,5 +1,5 @@ -import { AVLTree, AVLTreeNode } from '../../../data-structures'; -import { BSTOptions } from './bst'; +import {AVLTree, AVLTreeNode} from '../../../data-structures'; +import {BSTOptions} from './bst'; export type AVLTreeNodeNested = AVLTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index 0139825..cca130d 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -1,6 +1,6 @@ -import { BinaryTree, BinaryTreeNode } from '../../../data-structures'; -import { IterationType, OptValue } from '../../common'; -import { DFSOperation } from '../../../constants'; +import {BinaryTree, BinaryTreeNode} from '../../../data-structures'; +import {IterationType, OptValue} from '../../common'; +import {DFSOperation} from '../../../constants'; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @@ -9,9 +9,9 @@ export type BinaryTreeNested> = export type ToEntryFn = (rawElement: R) => BTNEntry; export type BinaryTreeOptions = { - iterationType?: IterationType; - toEntryFn?: ToEntryFn; - isMapMode?: boolean; + iterationType?: IterationType; + toEntryFn?: ToEntryFn; + isMapMode?: boolean; } export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean }; diff --git a/src/types/data-structures/binary-tree/bst.ts b/src/types/data-structures/binary-tree/bst.ts index a541273..b9d974e 100644 --- a/src/types/data-structures/binary-tree/bst.ts +++ b/src/types/data-structures/binary-tree/bst.ts @@ -1,13 +1,13 @@ -import { BST, BSTNode } from '../../../data-structures'; -import type { BinaryTreeOptions } from './binary-tree'; -import { Comparator } from '../../common'; +import {BST, BSTNode} from '../../../data-structures'; +import type {BinaryTreeOptions} from './binary-tree'; +import {Comparator} from '../../common'; export type BSTNodeNested = BSTNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> export type BSTNested> = BST>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> export type BSTOptions = BinaryTreeOptions & { - comparator?: Comparator + comparator?: Comparator } export type BSTNOptKey = K | undefined; diff --git a/src/types/data-structures/binary-tree/rb-tree.ts b/src/types/data-structures/binary-tree/rb-tree.ts index 3822199..b08002c 100644 --- a/src/types/data-structures/binary-tree/rb-tree.ts +++ b/src/types/data-structures/binary-tree/rb-tree.ts @@ -1,5 +1,5 @@ -import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures'; -import type { BSTOptions } from "./bst"; +import {RedBlackTree, RedBlackTreeNode} from '../../../data-structures'; +import type {BSTOptions} from "./bst"; export type RBTNColor = 'RED' | 'BLACK'; diff --git a/src/types/data-structures/binary-tree/tree-multi-map.ts b/src/types/data-structures/binary-tree/tree-multi-map.ts index 43ccc38..7498ff3 100644 --- a/src/types/data-structures/binary-tree/tree-multi-map.ts +++ b/src/types/data-structures/binary-tree/tree-multi-map.ts @@ -1,5 +1,5 @@ -import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures'; -import type { RBTreeOptions } from './rb-tree'; +import {TreeMultiMap, TreeMultiMapNode} from '../../../data-structures'; +import type {RBTreeOptions} from './rb-tree'; export type TreeMultiMapNodeNested = TreeMultiMapNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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 d774f6b..d6cdbbf 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 @@ -75,7 +75,7 @@ describe('AVLTreeMultiMap operations test1', () => { const nodeId10 = treeMultimap.getNode(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = treeMultimap.getNode(node => node.value === 9); + const nodeVal9 = treeMultimap.getNode(node => node.key === 9); expect(nodeVal9?.key).toBe(9); const nodesByCount1 = treeMultimap.getNodes(node => node.count === 1); @@ -331,7 +331,7 @@ describe('AVLTreeMultiMap operations test recursively1', () => { const nodeId10 = treeMultimap.getNode(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = treeMultimap.getNode(node => node.value === 9); + const nodeVal9 = treeMultimap.getNode(node => node.key === 9); expect(nodeVal9?.key).toBe(9); const nodesByCount1 = treeMultimap.getNodes(node => node.count === 1); @@ -762,10 +762,10 @@ describe('AVLTree toEntryFn', () => { }); }); -describe('AVLTreeMultiMap map mode count', () => { +describe('AVLTreeMultiMap not map mode count', () => { let tm: AVLTreeMultiMap; beforeEach(() => { - tm = new AVLTreeMultiMap([], { isMapMode: true }); + tm = new AVLTreeMultiMap([], { isMapMode: false }); }); it('Should added isolated node count ', () => { tm.addMany([ @@ -781,9 +781,9 @@ describe('AVLTreeMultiMap map mode count', () => { }); }); -describe('AVLTreeMultiMap map mode operations test1', () => { +describe('AVLTreeMultiMap not map mode operations test1', () => { it('should perform various operations on a Binary Search Tree with numeric values1', () => { - const treeMultimap = new AVLTreeMultiMap([], { isMapMode: true }); + const treeMultimap = new AVLTreeMultiMap([], { isMapMode: false }); expect(treeMultimap instanceof AVLTreeMultiMap); treeMultimap.add([11, 11]); @@ -826,11 +826,11 @@ describe('AVLTreeMultiMap map mode operations test1', () => { }); }); -describe('AVLTreeMultiMap map mode operations test recursively1', () => { +describe('AVLTreeMultiMap not map mode operations test recursively1', () => { it('should perform various operations on a Binary Search Tree with numeric values1', () => { const treeMultimap = new AVLTreeMultiMap([], { iterationType: 'RECURSIVE', - isMapMode: true + isMapMode: false }); expect(treeMultimap instanceof AVLTreeMultiMap); 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 8e78038..a3499e5 100644 --- a/test/unit/data-structures/binary-tree/avl-tree.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree.test.ts @@ -32,7 +32,7 @@ describe('AVL Tree Test', () => { 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?.value).toBe(15); + expect(node15?.value).toBe(undefined); const dfs = tree.dfs(node => node, 'IN'); expect(dfs[0].key).toBe(1); expect(dfs[dfs.length - 1].key).toBe(16); @@ -106,6 +106,25 @@ describe('AVL Tree Test', () => { expect(lastBFSNodes[1].key).toBe(2); expect(lastBFSNodes[2].key).toBe(16); }); + + it('should replace value', () => { + const tree = new AVLTree([4, 5, [1, '1'], 2, 3], { isMapMode: false }); + expect(tree.get(1)).toBe('1'); + expect(tree.getNode(1)?.value).toBe('1'); + tree.add(1, 'a'); + expect(tree.get(1)).toBe('a'); + tree.add([1, 'b']); + expect(tree.getNode(1)?.value).toBe('b'); + expect(tree.get(1)).toBe('b'); + const treeMap = new AVLTree([4, 5, [1, '1'], 2, 3]); + expect(treeMap.get(1)).toBe('1'); + expect(treeMap.getNode(1)?.value).toBe(undefined); + treeMap.add(1, 'a'); + expect(treeMap.get(1)).toBe('a'); + treeMap.add([1, 'b']); + expect(treeMap.getNode(1)?.value).toBe(undefined); + expect(treeMap.get(1)).toBe('b'); + }); }); describe('AVL Tree Test recursively', () => { @@ -139,7 +158,7 @@ describe('AVL Tree Test recursively', () => { 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?.value).toBe(15); + expect(node15?.value).toBe(undefined); const dfs = tree.dfs(node => node, 'IN'); expect(dfs[0].key).toBe(1); @@ -418,7 +437,7 @@ describe('AVLTree iterative methods test', () => { it('should clone work well', () => { const cloned = avl.clone(); expect(cloned.root?.left?.key).toBe(1); - expect(cloned.root?.right?.value).toBe('c'); + expect(cloned.root?.right?.value).toBe(undefined); }); it('should keys', () => { @@ -437,10 +456,10 @@ describe('AVLTree iterative methods test', () => { }); }); -describe('AVL Tree map mode', () => { +describe('AVL Tree not map mode', () => { 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([], { isMapMode: true }); + const tree = new AVLTree([], { isMapMode: false }); for (const i of arr) tree.add([i, i]); @@ -473,10 +492,10 @@ describe('AVL Tree map mode', () => { }); }); -describe('AVL Tree map mode test recursively', () => { +describe('AVL Tree not map mode test recursively', () => { 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([], { iterationType: 'RECURSIVE', isMapMode: true }); + const tree = new AVLTree([], { iterationType: 'RECURSIVE', isMapMode: false }); for (const i of arr) tree.add([i, i]); @@ -508,10 +527,10 @@ describe('AVL Tree map mode test recursively', () => { }); }); -describe('AVLTree iterative methods map mode', () => { +describe('AVLTree iterative methods not map mode', () => { let avl: AVLTree; beforeEach(() => { - avl = new AVLTree([], { isMapMode: true }); + avl = new AVLTree([], { isMapMode: false }); avl.add([1, 'a']); avl.add([2, 'b']); avl.add([3, 'c']); diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts index cd9c611..dfabe5b 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -102,10 +102,10 @@ describe('BinaryTree addMany', () => { ], [undefined, 22, 44, 33] ); - expect(tree.getNodeByKey(2)?.value).toBe(22); - expect(tree.getNodeByKey(3)?.value).toBe(33); - expect(tree.getNodeByKey(4)?.value).toBe(44); - expect(tree.getNodeByKey(1)?.value).toBe(1); + expect(tree.get(2)).toBe(22); + expect(tree.get(tree.getNodeByKey(3))).toBe(33); + expect(tree.get(tree.getNodeByKey(4))).toBe(44); + expect(tree.get(tree.getNodeByKey(1))).toBe(1); }); it('should addMany undefined and null', () => { @@ -193,7 +193,7 @@ describe('BinaryTree', () => { const node4 = tree.getNode(4); expect(tree.has(node4)).toBe(false); expect(tree.has(node => node === node4)).toBe(false); - expect(tree.has(node => node.value?.toString() === '3')).toBe(true); + expect(tree.has(node => node.key?.toString() === '3')).toBe(true); }); it('should the clone method work fine', () => { @@ -768,6 +768,25 @@ describe('BinaryTree', () => { }); expect(bTree.keyValueNodeEntryRawToNodeAndValue({ obj: { id: 1 } })).toEqual([undefined, undefined]); }); + + it('should replace value', () => { + const tree = new BinaryTree([4, 5, [1, '1'], 2, 3], { isMapMode: false }); + expect(tree.get(1)).toBe('1'); + expect(tree.getNode(1)?.value).toBe('1'); + tree.add(1, 'a'); + expect(tree.get(1)).toBe('a'); + tree.add([1, 'b']); + expect(tree.getNode(1)?.value).toBe('b'); + expect(tree.get(1)).toBe('b'); + const treeMap = new BinaryTree([4, 5, [1, '1'], 2, 3]); + expect(treeMap.get(1)).toBe('1'); + expect(treeMap.getNode(1)?.value).toBe(undefined); + treeMap.add(1, 'a'); + expect(treeMap.get(1)).toBe('a'); + treeMap.add([1, 'b']); + expect(treeMap.getNode(1)?.value).toBe(undefined); + expect(treeMap.get(1)).toBe('b'); + }); }); describe('BinaryTree ensureNode', () => { @@ -1049,9 +1068,9 @@ describe('BinaryTree', () => { const nodeB = tree.getNode(3); expect(nodeA?.key).toBe(5); - expect(nodeA?.value).toBe('A'); + expect(nodeA?.value).toBe(undefined); expect(nodeB?.key).toBe(3); - expect(nodeB?.value).toBe('B'); + expect(tree.get(nodeB)).toBe('B'); }); it('should return null when getting a non-existent node', () => { @@ -1160,21 +1179,17 @@ describe('BinaryTree', () => { tree.add([2, 'B']); tree.add([null, 'null']); - const nodes = tree.isMapMode ? tree.getNodes(node => node.key === 2) : tree.getNodes(node => node.value === 'B'); + const nodes = tree.getNodes(node => node.key === 2); expect(nodes.length).toBe(1); expect(nodes[0].key).toBe(2); - const nodesRec = tree.isMapMode - ? tree.getNodes(node => node.key === 2, false, tree.root, 'RECURSIVE') - : tree.getNodes(node => node.value === 'B', false, tree.root, 'RECURSIVE'); + const nodesRec = tree.getNodes(node => node.key === 2, false, tree.root, 'RECURSIVE'); expect(nodesRec.length).toBe(1); expect(nodesRec[0].key).toBe(2); - const nodesItr = tree.isMapMode - ? tree.getNodes(node => node.key === 2, false, tree.root, 'ITERATIVE') - : tree.getNodes(node => node.value === 'B', false, tree.root, 'ITERATIVE'); + const nodesItr = tree.getNodes(node => node.key === 2, false, tree.root, 'ITERATIVE'); expect(nodesItr.length).toBe(1); expect(nodesItr[0].key).toBe(2); @@ -1220,13 +1235,13 @@ describe('BinaryTree', () => { }); }); -describe('BinaryTree map mode', () => { +describe('BinaryTree not map mode', () => { let tree: BinaryTree; beforeEach(() => { tree = new BinaryTree([], { iterationType: 'RECURSIVE', - isMapMode: true + isMapMode: false }); }); @@ -1247,8 +1262,7 @@ describe('BinaryTree map mode', () => { const node4 = tree.getNode(4); expect(tree.has(node4)).toBe(false); expect(tree.has(node => node === node4)).toBe(false); - if (tree.isMapMode) expect(tree.has(node => node.key?.toString() === '3')).toBe(true); - else expect(tree.has(node => node.value?.toString() === '3')).toBe(true); + expect(tree.has(node => node.value?.toString() === '3')).toBe(true); }); it('should isSubtreeBST', () => { @@ -1379,8 +1393,8 @@ describe('BinaryTree iterative methods test', () => { it('should clone work well', () => { const cloned = binaryTree.clone(); expect(cloned.root?.left?.key).toBe(2); - if (cloned.isMapMode) expect(cloned.get(cloned.root?.right)).toBe('c'); - else expect(cloned.root?.right?.value).toBe('c'); + expect(cloned.root?.right?.value).toBe(undefined); + expect(cloned.get(cloned.root?.right)).toBe('c'); }); it('should keys', () => { @@ -1423,10 +1437,10 @@ describe('BinaryTree iterative methods test', () => { }); }); -describe('BinaryTree map mode iterative methods test', () => { +describe('BinaryTree not map mode iterative methods test', () => { let binaryTree: BinaryTree; beforeEach(() => { - binaryTree = new BinaryTree([], { isMapMode: true }); + binaryTree = new BinaryTree([], { isMapMode: false }); binaryTree.add([1, 'a']); binaryTree.add(2, 'b'); binaryTree.add([3, 'c']); diff --git a/test/unit/data-structures/binary-tree/bst.test.ts b/test/unit/data-structures/binary-tree/bst.test.ts index 2306212..9b01de8 100644 --- a/test/unit/data-structures/binary-tree/bst.test.ts +++ b/test/unit/data-structures/binary-tree/bst.test.ts @@ -74,7 +74,7 @@ describe('BST operations test', () => { const nodeId10 = bst.getNode(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = bst.getNode(node => node.value === 9); + const nodeVal9 = bst.getNode(node => node.key === 9); expect(nodeVal9?.key).toBe(9); const leftMost = bst.getLeftMost(); @@ -86,7 +86,7 @@ describe('BST operations test', () => { const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15); expect(minNodeBySpecificNode?.key).toBe(12); - const nodes = bst.getNodes(node => node.value === 15); + const nodes = bst.getNodes(node => node.key === 15); expect(nodes.map(node => node.key)).toEqual([15]); let subTreeSum = 0; @@ -291,7 +291,7 @@ describe('BST operations test', () => { expect(leftMost).toBe(1); const node15 = objBST.getNode(15); - expect(node15?.value).toEqual({ + expect(objBST.get(node15)).toEqual({ name: 'Alice', age: 15 }); @@ -470,6 +470,25 @@ describe('BST operations test', () => { const nodeNull = bst.keyValueNodeEntryRawToNodeAndValue(null); expect(nodeNull).toEqual([undefined, undefined]); }); + + it('should replace value', () => { + const tree = new BST([4, 5, [1, '1'], 2, 3], { isMapMode: false }); + expect(tree.get(1)).toBe('1'); + expect(tree.getNode(1)?.value).toBe('1'); + tree.add(1, 'a'); + expect(tree.get(1)).toBe('a'); + tree.add([1, 'b']); + expect(tree.getNode(1)?.value).toBe('b'); + expect(tree.get(1)).toBe('b'); + const treeMap = new BST([4, 5, [1, '1'], 2, 3]); + expect(treeMap.get(1)).toBe('1'); + expect(treeMap.getNode(1)?.value).toBe(undefined); + treeMap.add(1, 'a'); + expect(treeMap.get(1)).toBe('a'); + treeMap.add([1, 'b']); + expect(treeMap.getNode(1)?.value).toBe(undefined); + expect(treeMap.get(1)).toBe('b'); + }); }); describe('BST operations test recursively', () => { @@ -708,7 +727,7 @@ describe('BST operations test recursively', () => { expect(leftMost).toBe(1); const node15 = objBST.getNode(15); - expect(node15?.value).toEqual({ + expect(objBST.get(node15)).toEqual({ key: 15, keyA: 15 }); @@ -1135,7 +1154,7 @@ describe('BST iterative methods test', () => { it('should clone work well', () => { const cloned = bst.clone(); expect(cloned.root?.left).toBe(undefined); - expect(cloned.root?.right?.value).toBe('b'); + expect(cloned.root?.right?.value).toBe(undefined); }); it('should keys', () => { @@ -1210,13 +1229,13 @@ describe('BST iterative methods test', () => { ); expect(balanced.leaves()).toEqual([1, 6, 4, 9]); - expect(balanced.leaves(node => node?.value)).toEqual(['a', 'f', 'd', 'i']); + expect(balanced.leaves(node => balanced.get(node))).toEqual(['a', 'f', 'd', 'i']); }); }); -describe('BST operations map mode test', () => { +describe('BST operations not map mode test', () => { it('should perform various operations on a Binary Search Tree with numeric values', () => { - const bst = new BST([], { isMapMode: true }); + const bst = new BST([], { isMapMode: false }); expect(bst).toBeInstanceOf(BST); bst.add([11, 11]); bst.add([3, 3]); @@ -1269,7 +1288,7 @@ describe('BST operations map mode test', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST([], { isMapMode: true }); + const objBST = new BST([], { isMapMode: false }); expect(objBST).toBeInstanceOf(BST); objBST.add([11, { name: '11', age: 11 }]); objBST.add([3, { name: '3', age: 3 }]); @@ -1322,7 +1341,7 @@ describe('BST operations map mode test', () => { }); it('should keyValueNodeEntryRawToNodeAndValue', () => { - const bst = new BST([], { isMapMode: true }); + const bst = new BST([], { isMapMode: false }); const node0 = bst.keyValueNodeEntryRawToNodeAndValue(0); expect(node0).toEqual([ { @@ -1343,11 +1362,11 @@ describe('BST operations map mode test', () => { }); }); -describe('BST operations map mode test recursively', () => { +describe('BST operations not map mode test recursively', () => { it('should perform various operations on a Binary Search Tree with numeric values', () => { const bst = new BST([], { iterationType: 'RECURSIVE', - isMapMode: true + isMapMode: false }); expect(bst).toBeInstanceOf(BST); bst.add([11, 11]); @@ -1375,7 +1394,7 @@ describe('BST operations map mode test recursively', () => { }); it('should perform various operations on a Binary Search Tree with object values', () => { - const objBST = new BST([], { isMapMode: true }); + const objBST = new BST([], { isMapMode: false }); expect(objBST).toBeInstanceOf(BST); objBST.add([11, { key: 11, keyA: 11 }]); objBST.add([3, { key: 3, keyA: 3 }]); @@ -1429,7 +1448,7 @@ describe('BST operations map mode test recursively', () => { }); }); -describe('BST iterative methods map mode test', () => { +describe('BST iterative methods not map mode test', () => { let bst: BST; beforeEach(() => { bst = new BST(); diff --git a/test/unit/data-structures/binary-tree/rb-tree.test.ts b/test/unit/data-structures/binary-tree/rb-tree.test.ts index 1d89841..f07d61d 100644 --- a/test/unit/data-structures/binary-tree/rb-tree.test.ts +++ b/test/unit/data-structures/binary-tree/rb-tree.test.ts @@ -182,6 +182,25 @@ describe('RedBlackTree 1', () => { expect(rbTree.size).toBe(4); expect(cloned.size).toBe(3); }); + + it('should replace value', () => { + const tree = new RedBlackTree([4, 5, [1, '1'], 2, 3], { isMapMode: false }); + expect(tree.get(1)).toBe('1'); + expect(tree.getNode(1)?.value).toBe('1'); + tree.add(1, 'a'); + expect(tree.get(1)).toBe('a'); + tree.add([1, 'b']); + expect(tree.getNode(1)?.value).toBe('b'); + expect(tree.get(1)).toBe('b'); + const treeMap = new RedBlackTree([4, 5, [1, '1'], 2, 3]); + expect(treeMap.get(1)).toBe('1'); + expect(treeMap.getNode(1)?.value).toBe(undefined); + treeMap.add(1, 'a'); + expect(treeMap.get(1)).toBe('a'); + treeMap.add([1, 'b']); + expect(treeMap.getNode(1)?.value).toBe(undefined); + expect(treeMap.get(1)).toBe('b'); + }); }); describe('RedBlackTree 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 96bb9a0..bb99b2d 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 @@ -133,7 +133,7 @@ describe('TreeMultiMap operations test1', () => { const nodeId10 = tmm.getNode(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = tmm.getNode(node => node.value === 9); + const nodeVal9 = tmm.getNode(node => node.key === 9); expect(nodeVal9?.key).toBe(9); const nodesByCount1 = tmm.getNodes(node => node.count === 1); @@ -391,7 +391,7 @@ describe('TreeMultiMap operations test recursively1', () => { const nodeId10 = tmm.getNode(10); expect(nodeId10?.key).toBe(10); - const nodeVal9 = tmm.getNode(node => node.value === 9); + const nodeVal9 = tmm.getNode(node => node.key === 9); expect(nodeVal9?.key).toBe(9); const nodesByCount1 = tmm.getNodes(node => node.count === 1); @@ -837,10 +837,10 @@ describe('TreeMultiMap iterative methods test', () => { }); }); -describe('TreeMultiMap count map mode', () => { +describe('TreeMultiMap count not map mode', () => { let tmm: TreeMultiMap; beforeEach(() => { - tmm = new TreeMultiMap([], { isMapMode: true }); + tmm = new TreeMultiMap([], { isMapMode: false }); }); it('Should added node count ', () => { @@ -860,9 +860,9 @@ describe('TreeMultiMap count map mode', () => { }); }); -describe('TreeMultiMap operations test1 map mode', () => { +describe('TreeMultiMap operations test1 not map mode', () => { it('should perform various operations on a Binary Search Tree with numeric values1', () => { - const tmm = new TreeMultiMap([], { isMapMode: true }); + const tmm = new TreeMultiMap([], { isMapMode: false }); expect(tmm instanceof TreeMultiMap); @@ -907,11 +907,11 @@ describe('TreeMultiMap operations test1 map mode', () => { }); }); -describe('TreeMultiMap operations test recursively1 map mode', () => { +describe('TreeMultiMap operations test recursively1 not map mode', () => { it('should perform various operations on a Binary Search Tree with numeric values1', () => { const tmm = new TreeMultiMap([], { iterationType: 'RECURSIVE', - isMapMode: true + isMapMode: false }); expect(tmm instanceof TreeMultiMap); @@ -956,10 +956,10 @@ describe('TreeMultiMap operations test recursively1 map mode', () => { }); }); -describe('TreeMultiMap iterative methods testm ap mode', () => { +describe('TreeMultiMap iterative methods test not map mode', () => { let treeMM: TreeMultiMap; beforeEach(() => { - treeMM = new TreeMultiMap([], { isMapMode: true }); + treeMM = new TreeMultiMap([], { isMapMode: false }); treeMM.add(1, 'a', 10); treeMM.add([2, 'b'], undefined, 10); treeMM.add([3, 'c'], undefined, 1); @@ -970,6 +970,6 @@ describe('TreeMultiMap iterative methods testm ap mode', () => { expect(treeMM.getComputedCount()).toBe(21); const cloned = treeMM.clone(); expect(cloned.root?.left?.key).toBe(1); - expect(cloned.get(cloned.root?.right)).toBe('c'); + expect(cloned.get(cloned.root?.right)).toBe(undefined); }); });