Skip to content

Commit

Permalink
fix: Fix the bug where the binary tree repeatedly adds elements with …
Browse files Browse the repository at this point in the history
…the same key in map mode and the bug where the node’s value is instantiated simultaneously. Enable map mode by default.
  • Loading branch information
zrwusa committed Nov 3, 2024
1 parent 37ff920 commit 5adf75f
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 95 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion src/data-structures/binary-tree/avl-tree-multi-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/binary-tree/avl-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class AVLTree<
* type NODE.
*/
override createNode(key: K, value?: V): NODE {
return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
return new AVLTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/data-structures/binary-tree/binary-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class BinaryTree<
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
}

protected _isMapMode = false;
protected _isMapMode = true;

get isMapMode() {
return this._isMapMode;
Expand Down Expand Up @@ -181,7 +181,7 @@ export class BinaryTree<
* as NODE.
*/
createNode(key: K, value?: V): NODE {
return new BinaryTreeNode<K, V, NODE>(key, value) as NODE;
return new BinaryTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/data-structures/binary-tree/bst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V, NODE>(key, value) as NODE;
return new BSTNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
}

/**
Expand Down Expand Up @@ -174,9 +174,9 @@ export class BST<
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
value?: V
): [OptNode<NODE>, 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];
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions src/data-structures/binary-tree/rb-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class RedBlackTree<
* returned.
*/
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
return new RedBlackTreeNode<K, V, NODE>(key, this._isMapMode ? undefined : value, color) as NODE;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/data-structures/binary-tree/tree-multi-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/types/data-structures/binary-tree/avl-tree-multi-map.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Expand Down
4 changes: 2 additions & 2 deletions src/types/data-structures/binary-tree/avl-tree.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Expand Down
12 changes: 6 additions & 6 deletions src/types/data-structures/binary-tree/binary-tree.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Expand All @@ -9,9 +9,9 @@ export type BinaryTreeNested<K, V, R, NODE extends BinaryTreeNode<K, V, NODE>> =
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;

export type BinaryTreeOptions<K, V, R> = {
iterationType?: IterationType;
toEntryFn?: ToEntryFn<K, V, R>;
isMapMode?: boolean;
iterationType?: IterationType;
toEntryFn?: ToEntryFn<K, V, R>;
isMapMode?: boolean;
}

export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
Expand Down
8 changes: 4 additions & 4 deletions src/types/data-structures/binary-tree/bst.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
comparator?: Comparator<K>
comparator?: Comparator<K>
}

export type BSTNOptKey<K> = K | undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/types/data-structures/binary-tree/rb-tree.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/types/data-structures/binary-tree/tree-multi-map.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Expand Down
16 changes: 8 additions & 8 deletions test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -762,10 +762,10 @@ describe('AVLTree toEntryFn', () => {
});
});

describe('AVLTreeMultiMap map mode count', () => {
describe('AVLTreeMultiMap not map mode count', () => {
let tm: AVLTreeMultiMap<number>;
beforeEach(() => {
tm = new AVLTreeMultiMap<number>([], { isMapMode: true });
tm = new AVLTreeMultiMap<number>([], { isMapMode: false });
});
it('Should added isolated node count ', () => {
tm.addMany([
Expand All @@ -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<number>([], { isMapMode: true });
const treeMultimap = new AVLTreeMultiMap<number>([], { isMapMode: false });

expect(treeMultimap instanceof AVLTreeMultiMap);
treeMultimap.add([11, 11]);
Expand Down Expand Up @@ -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<number>([], {
iterationType: 'RECURSIVE',
isMapMode: true
isMapMode: false
});

expect(treeMultimap instanceof AVLTreeMultiMap);
Expand Down
37 changes: 28 additions & 9 deletions test/unit/data-structures/binary-tree/avl-tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<number, string>([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<number>([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', () => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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<number>([], { isMapMode: true });
const tree = new AVLTree<number>([], { isMapMode: false });

for (const i of arr) tree.add([i, i]);

Expand Down Expand Up @@ -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<number>([], { iterationType: 'RECURSIVE', isMapMode: true });
const tree = new AVLTree<number>([], { iterationType: 'RECURSIVE', isMapMode: false });

for (const i of arr) tree.add([i, i]);

Expand Down Expand Up @@ -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<number, string>;
beforeEach(() => {
avl = new AVLTree<number, string>([], { isMapMode: true });
avl = new AVLTree<number, string>([], { isMapMode: false });
avl.add([1, 'a']);
avl.add([2, 'b']);
avl.add([3, 'c']);
Expand Down
Loading

0 comments on commit 5adf75f

Please sign in to comment.