mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
Refactor: Review methods in the binary tree that require explicit 'override' annotation. Feat: Add the 'getMutableCount' method to AVLMultiMap.
This commit is contained in:
parent
e1631a8e3e
commit
644ce69190
|
@ -83,6 +83,10 @@ export class AVLTreeMultiMap<
|
|||
* @returns the sum of the count property of all nodes in the tree.
|
||||
*/
|
||||
get count(): number {
|
||||
return this._count;
|
||||
}
|
||||
|
||||
getMutableCount(): number {
|
||||
let sum = 0;
|
||||
this.dfs(node => (sum += node.count));
|
||||
return sum;
|
||||
|
@ -414,7 +418,7 @@ export class AVLTreeMultiMap<
|
|||
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
||||
* superclass, after updating the `count` property of the `newNode` object.
|
||||
*/
|
||||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
||||
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
||||
newNode.count = oldNode.count + newNode.count;
|
||||
return super._replaceNode(oldNode, newNode);
|
||||
}
|
||||
|
|
|
@ -522,7 +522,7 @@ export class AVLTree<
|
|||
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the
|
||||
* `oldNode` and `newNode` as arguments.
|
||||
*/
|
||||
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
||||
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
||||
newNode.height = oldNode.height;
|
||||
|
||||
return super._replaceNode(oldNode, newNode);
|
||||
|
|
|
@ -1837,7 +1837,7 @@ export class BinaryTree<
|
|||
* following types:
|
||||
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
||||
*/
|
||||
print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
|
||||
override print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
|
||||
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (!beginRoot) return;
|
||||
|
|
|
@ -856,7 +856,7 @@ export class BST<
|
|||
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
|
||||
* can either be an object of type `NODE` or it can be `undefined`.
|
||||
*/
|
||||
protected _setRoot(v: NODE | undefined) {
|
||||
protected override _setRoot(v: NODE | undefined) {
|
||||
if (v) {
|
||||
v.parent = undefined;
|
||||
}
|
||||
|
|
|
@ -89,26 +89,16 @@ export class RedBlackTree<
|
|||
return this._SENTINEL;
|
||||
}
|
||||
|
||||
protected _root: NODE | undefined;
|
||||
protected override _root: NODE | undefined;
|
||||
|
||||
/**
|
||||
* The function returns the root node of a tree or undefined if there is no root.
|
||||
* @returns The root node of the tree structure, or undefined if there is no root node.
|
||||
*/
|
||||
get root(): NODE | undefined {
|
||||
override get root(): NODE | undefined {
|
||||
return this._root;
|
||||
}
|
||||
|
||||
protected _size: number = 0;
|
||||
|
||||
/**
|
||||
* The function returns the size of an object.
|
||||
* @returns The size of the object, which is a number.
|
||||
*/
|
||||
get size(): number {
|
||||
return this._size;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
||||
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
||||
|
@ -262,8 +252,8 @@ export class RedBlackTree<
|
|||
* size counter to zero.
|
||||
*/
|
||||
override clear() {
|
||||
super.clear();
|
||||
this._root = this.SENTINEL;
|
||||
this._size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -69,4 +69,4 @@ export enum CRUD {
|
|||
READ = 'READ',
|
||||
UPDATED = 'UPDATED',
|
||||
DELETED = 'DELETED'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,8 +35,11 @@ describe('AVLTreeMultiMap count', () => {
|
|||
[2, 2],
|
||||
[3, 3]
|
||||
]);
|
||||
tm.add([2, 2], undefined, 10)
|
||||
tm.lesserOrGreaterTraverse(node => (node.count += 2), CP.gt, 1);
|
||||
expect(tm.count).toBe(7);
|
||||
tm.delete(2);
|
||||
expect(tm.count).toBe(12);
|
||||
expect(tm.getMutableCount()).toBe(16);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue