Skip to content

Commit

Permalink
perf: Unified all APIs, all parameters that accept node types can als…
Browse files Browse the repository at this point in the history
…o accept node keys as parameters.
  • Loading branch information
zrwusa committed Nov 9, 2023
1 parent 8971dbb commit 28207b6
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 239 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.42.5](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
## [v1.42.6](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)

### Changes

Expand Down
244 changes: 122 additions & 122 deletions README.md

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions src/data-structures/binary-tree/avl-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license MIT License
*/
import {BST, BSTNode} from './bst';
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BTNKey} from '../../types';
import type {AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey} from '../../types';
import {BTNCallback} from '../../types';
import {IBinaryTree} from '../../interfaces';

Expand Down Expand Up @@ -72,12 +72,12 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
* value. This value is compared with the `identifier` parameter to determine if the node should be
* included in the result. The `callback` parameter has a default value of
* `this.defaultOneParamCallback`
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
* @returns The method is returning an array of `BiTreeDeleteResult<N>` objects.
*/
override delete<C extends BTNCallback<N>>(
identifier: ReturnType<C>,
callback: C = this.defaultOneParamCallback as C
): BinaryTreeDeletedResult<N>[] {
): BiTreeDeleteResult<N>[] {
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
const deletedResults = super.delete(identifier, callback);
for (const {needBalanced} of deletedResults) {
Expand All @@ -96,23 +96,29 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
* from the source node (`srcNode`) will be swapped to.
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
*/
protected override _swap(srcNode: N, destNode: N): N {
const {key, value, height} = destNode;
const tempNode = this.createNode(key, value);
protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
if (this.isNodeKey(srcNode)) srcNode = this._getNodeByKey(srcNode);
if (this.isNodeKey(destNode)) destNode = this._getNodeByKey(destNode);

if (tempNode) {
tempNode.height = height;
if (srcNode && destNode) {
const {key, value, height} = destNode;
const tempNode = this.createNode(key, value);

destNode.key = srcNode.key;
destNode.value = srcNode.value;
destNode.height = srcNode.height;
if (tempNode) {
tempNode.height = height;

srcNode.key = tempNode.key;
srcNode.value = tempNode.value;
srcNode.height = tempNode.height;
}
destNode.key = srcNode.key;
destNode.value = srcNode.value;
destNode.height = srcNode.height;

srcNode.key = tempNode.key;
srcNode.value = tempNode.value;
srcNode.height = tempNode.height;
}

return destNode;
return destNode;
}
return undefined;
}

/**
Expand Down
Loading

0 comments on commit 28207b6

Please sign in to comment.