mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
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.
This commit is contained in:
parent
1303aba070
commit
89dadfccc2
|
@ -152,9 +152,8 @@ export class BinaryTree<
|
|||
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions<K>) {
|
||||
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(
|
||||
|
|
|
@ -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<K, V, NODE>[] = [];
|
||||
|
||||
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<K, V, NODE>[]) => {
|
||||
|
@ -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<C extends BTNCallback<NODE>>(
|
||||
callback: C = this._DEFAULT_CALLBACK as C,
|
||||
lesserOrGreater: CP = 'LT',
|
||||
lesserOrGreater: CP = -1,
|
||||
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
||||
iterationType: IterationType = this.iterationType
|
||||
): ReturnType<C>[] {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -7,6 +7,5 @@ export type BinaryTreeNodeNested<K extends Comparable, V> = BinaryTreeNode<K, V,
|
|||
export type BinaryTreeNested<K extends Comparable, V, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, BinaryTree<K, V, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
export type BinaryTreeOptions<K> = {
|
||||
iterationType?: IterationType,
|
||||
extractor?: (key: K | null | undefined) => number
|
||||
iterationType?: IterationType
|
||||
}
|
||||
|
|
|
@ -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<number>();
|
||||
|
||||
for (const i of keys) tree.add([i, i]);
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<number>();
|
||||
|
||||
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<number>) => (lesserSum += node.key), 'LT', 10);
|
||||
treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode<number>) => (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<number>) => (lesserSum += node.key), 'LT', 10);
|
||||
treeMultimap.lesserOrGreaterTraverse((node: AVLTreeMultiMapNode<number>) => (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);
|
||||
});
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -64,16 +64,15 @@ describe('Overall BinaryTree Test', () => {
|
|||
it('Should clone a BST works fine', () => {
|
||||
const bst = new BST<number>([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<number>([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<number>([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<number>([3, 6, 7, 1, 9], {
|
||||
iterationType: 'RECURSIVE',
|
||||
extractor: key => Number(key)
|
||||
iterationType: 'RECURSIVE'
|
||||
});
|
||||
expect(rbTree.size).toBe(5);
|
||||
rbTree.add(2);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue