Skip to content

Commit a008a33

Browse files
committed
style: Utilize the built-in _dfs method across all binary tree data structures to optimize the search method.
feat: The search and rangeSearch methods in binary trees now default to in-order traversal for producing ordered results. docs: Add sample code for AVLTree. Explicitly document method parameter types for all binary tree data structures.
1 parent b759eec commit a008a33

16 files changed

+875
-654
lines changed

src/data-structures/binary-tree/avl-tree-counter.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import type {
99
AVLTreeCounterOptions,
1010
BinaryTreeDeleteResult,
1111
BSTNOptKeyOrNode,
12-
BTNRep,
1312
EntryCallback,
14-
IterationType,
15-
OptNodeOrNull
13+
IterationType
1614
} from '../../types';
1715
import { IBinaryTree } from '../../interfaces';
1816
import { AVLTree, AVLTreeNode } from './avl-tree';
1917

2018
export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
19+
override parent?: AVLTreeCounterNode<K, V> = undefined;
20+
2121
/**
2222
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
2323
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@@ -33,28 +33,26 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
3333
this.count = count;
3434
}
3535

36-
override parent?: AVLTreeCounterNode<K, V> = undefined;
37-
38-
override _left?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
36+
override _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
3937

40-
override get left(): OptNodeOrNull<AVLTreeCounterNode<K, V>> {
38+
override get left(): AVLTreeCounterNode<K, V> | null | undefined {
4139
return this._left;
4240
}
4341

44-
override set left(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) {
42+
override set left(v: AVLTreeCounterNode<K, V> | null | undefined) {
4543
if (v) {
4644
v.parent = this;
4745
}
4846
this._left = v;
4947
}
5048

51-
override _right?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined;
49+
override _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined;
5250

53-
override get right(): OptNodeOrNull<AVLTreeCounterNode<K, V>> {
51+
override get right(): AVLTreeCounterNode<K, V> | null | undefined {
5452
return this._right;
5553
}
5654

57-
override set right(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) {
55+
override set right(v: AVLTreeCounterNode<K, V> | null | undefined) {
5856
if (v) {
5957
v.parent = this;
6058
}
@@ -78,7 +76,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
7876
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
7977
*/
8078
constructor(
81-
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeCounterNode<K, V>> | R> = [],
79+
keysNodesEntriesOrRaws: Iterable<
80+
K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
81+
> = [],
8282
options?: AVLTreeCounterOptions<K, V, R>
8383
) {
8484
super([], options);
@@ -145,12 +145,14 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
145145

146146
/**
147147
* The function checks if the input is an instance of AVLTreeCounterNode.
148-
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter
149-
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
148+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
149+
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
150150
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
151151
* an instance of the `AVLTreeCounterNode` class.
152152
*/
153-
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
153+
override isNode(
154+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
155+
): keyNodeOrEntry is AVLTreeCounterNode<K, V> {
154156
return keyNodeOrEntry instanceof AVLTreeCounterNode;
155157
}
156158

@@ -160,9 +162,9 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
160162
*
161163
* The function overrides the add method of a TypeScript class to add a new node to a data structure
162164
* and update the count.
163-
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
165+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
164166
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
165-
* can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node,
167+
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
166168
* entry, or raw element
167169
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
168170
* data structure. It is an optional parameter, so it can be omitted if not needed.
@@ -171,7 +173,11 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
171173
* be added once. However, you can specify a different value for `count` if you want to add
172174
* @returns a boolean value.
173175
*/
174-
override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, value?: V, count = 1): boolean {
176+
override add(
177+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
178+
value?: V,
179+
count = 1
180+
): boolean {
175181
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
176182
if (newNode === undefined) return false;
177183

@@ -189,7 +195,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
189195
*
190196
* The function overrides the delete method in a binary tree data structure, handling deletion of
191197
* nodes and maintaining balance in the tree.
192-
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate`
198+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
193199
* parameter in the `delete` method is used to specify the condition for deleting a node from the
194200
* binary tree. It can be a key, node, or entry that determines which
195201
* node(s) should be deleted.
@@ -203,7 +209,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
203209
* deleted node and whether balancing is needed in the tree.
204210
*/
205211
override delete(
206-
keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>,
212+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
207213
ignoreCount = false
208214
): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] {
209215
const deletedResult: BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] = [];
@@ -276,6 +282,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
276282
/**
277283
* Time Complexity: O(n log n)
278284
* Space Complexity: O(log n)
285+
*
279286
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
280287
* tree using either a recursive or iterative approach.
281288
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
@@ -374,8 +381,8 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
374381
/**
375382
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
376383
* a node object.
377-
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
378-
* `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
384+
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
385+
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
379386
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
380387
* `override` function. It represents the value associated with the key in the data structure. If no
381388
* value is provided, it will default to `undefined`.
@@ -384,7 +391,7 @@ export class AVLTreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR
384391
* @returns either a AVLTreeCounterNode<K, V> object or undefined.
385392
*/
386393
protected override _keyValueNodeOrEntryToNodeAndValue(
387-
keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>,
394+
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
388395
value?: V,
389396
count = 1
390397
): [AVLTreeCounterNode<K, V> | undefined, V | undefined] {

src/data-structures/binary-tree/avl-tree-multi-map.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
* @copyright Copyright (c) 2022 Pablo Zeng <[email protected]>
66
* @license MIT License
77
*/
8-
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types';
8+
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types';
99
import { AVLTree, AVLTreeNode } from './avl-tree';
1010
import { IBinaryTree } from '../../interfaces';
1111

1212
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
13+
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
14+
1315
/**
1416
* This TypeScript constructor initializes an object with a key of type K and an array of values of
1517
* type V.
@@ -23,28 +25,26 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
2325
super(key, value);
2426
}
2527

26-
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
27-
28-
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
28+
override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
2929

30-
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
30+
override get left(): AVLTreeMultiMapNode<K, V> | null | undefined {
3131
return this._left;
3232
}
3333

34-
override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
34+
override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
3535
if (v) {
3636
v.parent = this;
3737
}
3838
this._left = v;
3939
}
4040

41-
override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
41+
override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined;
4242

43-
override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
43+
override get right(): AVLTreeMultiMapNode<K, V> | null | undefined {
4444
return this._right;
4545
}
4646

47-
override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
47+
override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) {
4848
if (v) {
4949
v.parent = this;
5050
}
@@ -71,7 +71,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
7171
* additional options for configuring the AVLTreeMultiMap instance.
7272
*/
7373
constructor(
74-
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [],
74+
keysNodesEntriesOrRaws: Iterable<
75+
K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
76+
> = [],
7577
options?: AVLTreeMultiMapOptions<K, V[], R>
7678
) {
7779
super([], { ...options, isMapMode: true });
@@ -117,7 +119,9 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
117119
return new AVLTreeMultiMapNode<K, V>(key, []);
118120
}
119121

120-
override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean;
122+
override add(
123+
node: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
124+
): boolean;
121125

122126
override add(key: K, value: V): boolean;
123127

@@ -127,7 +131,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
127131
*
128132
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
129133
* tree multi-map.
130-
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
134+
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
131135
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it
132136
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
133137
* `values`
@@ -137,7 +141,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
137141
* @returns The `override add` method is returning a boolean value, which indicates whether the
138142
* addition operation was successful or not.
139143
*/
140-
override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean {
144+
override add(
145+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
146+
value?: V
147+
): boolean {
141148
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
142149

143150
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
@@ -180,7 +187,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
180187
*
181188
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
182189
* structure and deletes the entire node if no values are left for that key.
183-
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
190+
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
184191
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
185192
* pair in the AVLTreeMultiMapNode, or just the key itself.
186193
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
@@ -191,7 +198,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, M
191198
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
192199
* the value was not found in the array, it returns `false`.
193200
*/
194-
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean {
201+
deleteValue(
202+
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K,
203+
value: V
204+
): boolean {
195205
const values = this.get(keyNodeOrEntry);
196206
if (Array.isArray(values)) {
197207
const index = values.indexOf(value);

0 commit comments

Comments
 (0)