From 0a8b9265cc9328a2de17a7cb685921a7d828ea35 Mon Sep 17 00:00:00 2001 From: Revone Date: Thu, 31 Oct 2024 13:26:45 +1300 Subject: [PATCH] fix: #110 --- .../binary-tree/avl-tree-multi-map.ts | 8 ++++---- src/data-structures/binary-tree/avl-tree.ts | 6 +++--- src/data-structures/binary-tree/binary-tree.ts | 10 ++++------ src/data-structures/binary-tree/rb-tree.ts | 13 ++++++++----- .../binary-tree/tree-multi-map.ts | 16 +++++++++------- 5 files changed, 28 insertions(+), 25 deletions(-) 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 bdfc1ad..b715882 100644 --- a/src/data-structures/binary-tree/avl-tree-multi-map.ts +++ b/src/data-structures/binary-tree/avl-tree-multi-map.ts @@ -232,9 +232,9 @@ export class AVLTreeMultiMap< * * The function overrides the delete method in a binary tree data structure, handling deletion of * nodes and maintaining balance in the tree. - * @param {BTNKeyOrNodeOrEntry | R | BTNPredicate} predicate - The `predicate` + * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRaw - The `predicate` * parameter in the `delete` method is used to specify the condition for deleting a node from the - * binary tree. It can be a key, node, entry, or a custom predicate function that determines which + * binary tree. It can be a key, node, or entry that determines which * node(s) should be deleted. * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a * boolean flag that determines whether to ignore the count of the node being deleted. If @@ -246,13 +246,13 @@ export class AVLTreeMultiMap< * deleted node and whether balancing is needed in the tree. */ override delete( - predicate: BTNKeyOrNodeOrEntry | R | BTNPredicate, + keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry | R, ignoreCount = false ): BinaryTreeDeleteResult[] { const deletedResult: BinaryTreeDeleteResult[] = []; if (!this.root) return deletedResult; - const curr: NODE | undefined = this.getNode(predicate) ?? undefined; + const curr: NODE | undefined = this.getNode(keyOrNodeOrEntryOrRaw) ?? undefined; if (!curr) return deletedResult; const parent: NODE | undefined = curr?.parent ? curr.parent : undefined; diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index f40030a..2e05bdb 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -160,15 +160,15 @@ export class AVLTree< * * The function overrides the delete method in a TypeScript class, performs deletion, and then * balances the tree if necessary. - * @param {BTNKeyOrNodeOrEntry | R | BTNPredicate} predicate - The `predicate` + * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw` * parameter in the `override delete` method can be one of the following types: * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` * method from the superclass (presumably a parent class) with the provided `predicate`, which could * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects. */ - override delete(predicate: BTNKeyOrNodeOrEntry | R | BTNPredicate): BinaryTreeDeleteResult[] { - const deletedResults = super.delete(predicate); + override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry | R): BinaryTreeDeleteResult[] { + const deletedResults = super.delete(keyOrNodeOrEntryOrRaw); for (const { needBalanced } of deletedResults) { if (needBalanced) { this._balancePath(needBalanced); diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 2543a8a..6dafe4f 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -515,22 +515,20 @@ export class BinaryTree< * * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns * the deleted node along with information for tree balancing. - * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRawOr + * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRaw * - The `delete` method you provided is used to delete a node from a binary tree based on the key, - * node, entry, raw data, or a custom predicate. The method returns an array of + * node, entry or raw data. The method returns an array of * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether * balancing is needed. * @returns The `delete` method returns an array of `BinaryTreeDeleteResult` objects. Each object in * the array contains information about the node that was deleted (`deleted`) and the node that may * need to be balanced (`needBalanced`). */ - delete( - keyOrNodeOrEntryOrRawOr: BTNKeyOrNodeOrEntry | R - ): BinaryTreeDeleteResult[] { + delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry | R): BinaryTreeDeleteResult[] { const deletedResult: BinaryTreeDeleteResult[] = []; if (!this._root) return deletedResult; - const curr = this.getNode(keyOrNodeOrEntryOrRawOr); + const curr = this.getNode(keyOrNodeOrEntryOrRaw); if (!curr) return deletedResult; const parent: NODE | undefined = curr?.parent; diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 18da22a..a196770 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -230,7 +230,7 @@ export class RedBlackTree< * * The function overrides the delete method in a binary tree data structure to remove a node based on * a given predicate and maintain the binary search tree properties. - * @param {BTNKeyOrNodeOrEntry | R | BTNPredicate} predicate - The `predicate` + * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw` * parameter in the `override delete` method is used to specify the condition or key based on which a * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate * function that determines which node(s) should be deleted. @@ -238,13 +238,16 @@ export class RedBlackTree< * objects. Each object in the array contains information about the deleted node and whether * balancing is needed. */ - override delete(predicate: BTNKeyOrNodeOrEntry | R | BTNPredicate): BinaryTreeDeleteResult[] { - if (predicate === null) return []; + override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry | R): BinaryTreeDeleteResult[] { + if (keyOrNodeOrEntryOrRaw === null) return []; const results: BinaryTreeDeleteResult[] = []; let nodeToDelete: OptBSTN; - if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate); - else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate); + if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw); + else + nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw) + ? keyOrNodeOrEntryOrRaw + : this.getNode(keyOrNodeOrEntryOrRaw); if (!nodeToDelete) { return results; diff --git a/src/data-structures/binary-tree/tree-multi-map.ts b/src/data-structures/binary-tree/tree-multi-map.ts index c381ba5..827e86a 100644 --- a/src/data-structures/binary-tree/tree-multi-map.ts +++ b/src/data-structures/binary-tree/tree-multi-map.ts @@ -232,10 +232,9 @@ export class TreeMultiMap< * * The function `delete` in TypeScript overrides the deletion operation in a binary tree data * structure, handling cases where nodes have children and maintaining balance in the tree. - * @param {BTNKeyOrNodeOrEntry | R | BTNPredicate} predicate - The `predicate` + * @param {BTNKeyOrNodeOrEntry | R} keyOrNodeOrEntryOrRaw - The `predicate` * parameter in the `delete` method is used to specify the condition or key based on which a node - * should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate - * function. + * should be deleted from the binary tree. It can be a key, a node, or an entry. * @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a * boolean flag that determines whether to ignore the count of nodes when performing deletion. If * `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If @@ -243,16 +242,19 @@ export class TreeMultiMap< * @returns The `override delete` method returns an array of `BinaryTreeDeleteResult` objects. */ override delete( - predicate: BTNKeyOrNodeOrEntry | R | BTNPredicate, + keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry | R, ignoreCount = false ): BinaryTreeDeleteResult[] { - if (predicate === null) return []; + if (keyOrNodeOrEntryOrRaw === null) return []; const results: BinaryTreeDeleteResult[] = []; let nodeToDelete: OptBSTN; - if (this._isPredicated(predicate)) nodeToDelete = this.getNode(predicate); - else nodeToDelete = this.isRealNode(predicate) ? predicate : this.getNode(predicate); + if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw); + else + nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw) + ? keyOrNodeOrEntryOrRaw + : this.getNode(keyOrNodeOrEntryOrRaw); if (!nodeToDelete) { return results;