data-structure-typed 1.18.8 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -184
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +182 -148
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +288 -316
- package/dist/data-structures/binary-tree/avl-tree.d.ts +22 -14
- package/dist/data-structures/binary-tree/avl-tree.js +23 -17
- package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
- package/dist/data-structures/binary-tree/binary-tree.js +11 -26
- package/dist/data-structures/binary-tree/bst.d.ts +62 -74
- package/dist/data-structures/binary-tree/bst.js +72 -96
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/rb-tree.js +5 -17
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +186 -17
- package/dist/data-structures/binary-tree/tree-multiset.js +712 -28
- package/dist/data-structures/graph/abstract-graph.d.ts +107 -49
- package/dist/data-structures/graph/abstract-graph.js +104 -55
- package/dist/data-structures/graph/directed-graph.d.ts +95 -94
- package/dist/data-structures/graph/directed-graph.js +95 -95
- package/dist/data-structures/graph/undirected-graph.d.ts +62 -61
- package/dist/data-structures/graph/undirected-graph.js +62 -61
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +10 -15
- package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +3 -4
- package/dist/data-structures/interfaces/rb-tree.d.ts +1 -2
- package/dist/data-structures/interfaces/tree-multiset.d.ts +3 -3
- package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +3 -3
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/utils.d.ts +0 -18
- package/dist/utils/types/validate-type.d.ts +19 -0
- package/dist/utils/types/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +3 -8
- package/dist/utils/utils.js +1 -83
- package/dist/utils/validate-type.d.ts +45 -0
- package/dist/utils/validate-type.js +58 -0
- package/package.json +4 -1
|
@@ -8,25 +8,33 @@
|
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../types';
|
|
10
10
|
import { IAVLTree, IAVLTreeNode } from '../interfaces';
|
|
11
|
-
export declare class AVLTreeNode<T = any,
|
|
12
|
-
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
11
|
+
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
13
12
|
}
|
|
14
13
|
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
14
|
+
/**
|
|
15
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
16
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
17
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
18
|
+
* options.
|
|
19
|
+
*/
|
|
15
20
|
constructor(options?: AVLTreeOptions);
|
|
16
|
-
createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N;
|
|
17
21
|
/**
|
|
18
|
-
* The function
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
*
|
|
22
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
23
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
24
|
+
* identify each node in the tree.
|
|
25
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
26
|
+
* that will be stored in the node.
|
|
27
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
28
|
+
*/
|
|
29
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
30
|
+
/**
|
|
31
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
32
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
33
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
34
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
35
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
28
36
|
*/
|
|
29
|
-
add(id: BinaryTreeNodeId, val?: N['val']
|
|
37
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
30
38
|
/**
|
|
31
39
|
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
32
40
|
* then balances the tree if necessary.
|
|
@@ -40,34 +40,40 @@ var AVLTreeNode = /** @class */ (function (_super) {
|
|
|
40
40
|
function AVLTreeNode() {
|
|
41
41
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
42
42
|
}
|
|
43
|
-
AVLTreeNode.prototype.createNode = function (id, val, count) {
|
|
44
|
-
return new AVLTreeNode(id, val, count);
|
|
45
|
-
};
|
|
46
43
|
return AVLTreeNode;
|
|
47
44
|
}(bst_1.BSTNode));
|
|
48
45
|
exports.AVLTreeNode = AVLTreeNode;
|
|
49
46
|
var AVLTree = /** @class */ (function (_super) {
|
|
50
47
|
__extends(AVLTree, _super);
|
|
48
|
+
/**
|
|
49
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
50
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
51
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
52
|
+
* options.
|
|
53
|
+
*/
|
|
51
54
|
function AVLTree(options) {
|
|
52
55
|
return _super.call(this, options) || this;
|
|
53
56
|
}
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
59
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
60
|
+
* identify each node in the tree.
|
|
61
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
62
|
+
* that will be stored in the node.
|
|
63
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
64
|
+
*/
|
|
65
|
+
AVLTree.prototype.createNode = function (id, val) {
|
|
66
|
+
return new AVLTreeNode(id, val);
|
|
56
67
|
};
|
|
57
68
|
/**
|
|
58
|
-
* The function overrides the add method of a
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
* `id`. It can be of type `N` (the generic type) or `null`.
|
|
64
|
-
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
65
|
-
* of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
|
|
66
|
-
* to `1`, indicating that the value should be inserted once.
|
|
67
|
-
* @returns The method is returning either an N object or null.
|
|
69
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
70
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
71
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
72
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
73
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
68
74
|
*/
|
|
69
|
-
AVLTree.prototype.add = function (id, val
|
|
70
|
-
var inserted = _super.prototype.add.call(this, id, val
|
|
75
|
+
AVLTree.prototype.add = function (id, val) {
|
|
76
|
+
var inserted = _super.prototype.add.call(this, id, val);
|
|
71
77
|
if (inserted)
|
|
72
78
|
this.balancePath(inserted);
|
|
73
79
|
return inserted;
|
|
@@ -5,40 +5,26 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId, BinaryTreeNodeNested } from '../types';
|
|
9
|
-
import { BinaryTreeOptions } from '../types';
|
|
8
|
+
import type { BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions } from '../types';
|
|
10
9
|
import { AbstractBinaryTree, AbstractBinaryTreeNode } from './abstract-binary-tree';
|
|
11
10
|
import { IBinaryTree, IBinaryTreeNode } from '../interfaces/binary-tree';
|
|
12
|
-
export declare class BinaryTreeNode<T = any,
|
|
13
|
-
/**
|
|
14
|
-
* The function creates a new binary tree node with an optional value and count, and returns it as a specified type.
|
|
15
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
16
|
-
* `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes.
|
|
17
|
-
* @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the
|
|
18
|
-
* value stored in the node.
|
|
19
|
-
* @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value
|
|
20
|
-
* appears in the binary tree node.
|
|
21
|
-
* @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type.
|
|
22
|
-
*/
|
|
23
|
-
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
11
|
+
export declare class BinaryTreeNode<T = any, NEIGHBOR extends BinaryTreeNode<T, NEIGHBOR> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, NEIGHBOR> implements IBinaryTreeNode<T, NEIGHBOR> {
|
|
24
12
|
}
|
|
25
13
|
export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
|
|
26
14
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
15
|
+
* This is a constructor function for a binary tree class that takes an optional options parameter.
|
|
16
|
+
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
17
|
+
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
18
|
+
* different configuration options.
|
|
30
19
|
*/
|
|
31
20
|
constructor(options?: BinaryTreeOptions);
|
|
32
21
|
/**
|
|
33
|
-
* The function creates a new binary tree node with
|
|
34
|
-
* it returns null.
|
|
22
|
+
* The function creates a new binary tree node with an optional value.
|
|
35
23
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
36
|
-
* `BinaryTreeNodeId
|
|
37
|
-
* @param
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`.
|
|
41
|
-
* @returns a BinaryTreeNode object if the value is not null, otherwise it returns null.
|
|
24
|
+
* `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
|
|
25
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
26
|
+
* stored in the node.
|
|
27
|
+
* @returns a new instance of a BinaryTreeNode with the specified id and value.
|
|
42
28
|
*/
|
|
43
|
-
createNode(id: BinaryTreeNodeId, val?: N['val']
|
|
29
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
44
30
|
}
|
|
@@ -29,45 +29,30 @@ var BinaryTreeNode = /** @class */ (function (_super) {
|
|
|
29
29
|
function BinaryTreeNode() {
|
|
30
30
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
31
31
|
}
|
|
32
|
-
/**
|
|
33
|
-
* The function creates a new binary tree node with an optional value and count, and returns it as a specified type.
|
|
34
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
35
|
-
* `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes.
|
|
36
|
-
* @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the
|
|
37
|
-
* value stored in the node.
|
|
38
|
-
* @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value
|
|
39
|
-
* appears in the binary tree node.
|
|
40
|
-
* @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type.
|
|
41
|
-
*/
|
|
42
|
-
BinaryTreeNode.prototype.createNode = function (id, val, count) {
|
|
43
|
-
return new BinaryTreeNode(id, val, count);
|
|
44
|
-
};
|
|
45
32
|
return BinaryTreeNode;
|
|
46
33
|
}(abstract_binary_tree_1.AbstractBinaryTreeNode));
|
|
47
34
|
exports.BinaryTreeNode = BinaryTreeNode;
|
|
48
35
|
var BinaryTree = /** @class */ (function (_super) {
|
|
49
36
|
__extends(BinaryTree, _super);
|
|
50
37
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
38
|
+
* This is a constructor function for a binary tree class that takes an optional options parameter.
|
|
39
|
+
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
40
|
+
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
41
|
+
* different configuration options.
|
|
54
42
|
*/
|
|
55
43
|
function BinaryTree(options) {
|
|
56
44
|
return _super.call(this, options) || this;
|
|
57
45
|
}
|
|
58
46
|
/**
|
|
59
|
-
* The function creates a new binary tree node with
|
|
60
|
-
* it returns null.
|
|
47
|
+
* The function creates a new binary tree node with an optional value.
|
|
61
48
|
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
|
|
62
|
-
* `BinaryTreeNodeId
|
|
63
|
-
* @param
|
|
64
|
-
*
|
|
65
|
-
* @
|
|
66
|
-
* of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`.
|
|
67
|
-
* @returns a BinaryTreeNode object if the value is not null, otherwise it returns null.
|
|
49
|
+
* `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
|
|
50
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
51
|
+
* stored in the node.
|
|
52
|
+
* @returns a new instance of a BinaryTreeNode with the specified id and value.
|
|
68
53
|
*/
|
|
69
|
-
BinaryTree.prototype.createNode = function (id, val
|
|
70
|
-
return new BinaryTreeNode(id, val
|
|
54
|
+
BinaryTree.prototype.createNode = function (id, val) {
|
|
55
|
+
return new BinaryTreeNode(id, val);
|
|
71
56
|
};
|
|
72
57
|
return BinaryTree;
|
|
73
58
|
}(abstract_binary_tree_1.AbstractBinaryTree));
|
|
@@ -5,119 +5,107 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested } from '../types';
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions } from '../types';
|
|
9
|
+
import { CP } from '../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBST, IBSTNode } from '../interfaces';
|
|
12
|
-
export declare class BSTNode<T = any,
|
|
13
|
-
/**
|
|
14
|
-
* The function creates a new binary search tree node with the specified id, value, and count.
|
|
15
|
-
* @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely
|
|
16
|
-
* identify each node in the tree.
|
|
17
|
-
* @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an
|
|
18
|
-
* optional parameter, meaning it can be omitted when calling the "createNode" function.
|
|
19
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary
|
|
20
|
-
* search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method.
|
|
21
|
-
* @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type.
|
|
22
|
-
*/
|
|
23
|
-
createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY;
|
|
12
|
+
export declare class BSTNode<T = any, NEIGHBOR extends BSTNode<T, NEIGHBOR> = BSTNodeNested<T>> extends BinaryTreeNode<T, NEIGHBOR> implements IBSTNode<T, NEIGHBOR> {
|
|
24
13
|
}
|
|
25
14
|
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
|
|
26
15
|
/**
|
|
27
|
-
* The constructor function
|
|
28
|
-
* @param [options] - An optional object that
|
|
16
|
+
* The constructor function initializes a binary search tree object with an optional comparator function.
|
|
17
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
|
|
29
18
|
*/
|
|
30
19
|
constructor(options?: BSTOptions);
|
|
31
20
|
/**
|
|
32
|
-
* The function creates a new binary search tree node with the given id
|
|
33
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is
|
|
34
|
-
*
|
|
35
|
-
* @param
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* of a particular value in the binary search tree node.
|
|
39
|
-
* @returns a new instance of the BSTNode class, casted as type N.
|
|
21
|
+
* The function creates a new binary search tree node with the given id and value.
|
|
22
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
23
|
+
* identify each node in the binary tree.
|
|
24
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
25
|
+
* that will be stored in the node.
|
|
26
|
+
* @returns a new instance of the BSTNode class with the specified id and value.
|
|
40
27
|
*/
|
|
41
|
-
createNode(id: BinaryTreeNodeId, val?: N['val']
|
|
28
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
42
29
|
/**
|
|
43
|
-
* The `add` function
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
51
|
-
* inserted once.
|
|
52
|
-
* @returns The method `add` returns a `N` object or `null`.
|
|
30
|
+
* The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
|
|
31
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
|
|
32
|
+
* is of type `BinaryTreeNodeId`.
|
|
33
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
|
|
34
|
+
* the value associated with the node.
|
|
35
|
+
* @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
|
|
36
|
+
* If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
|
|
53
37
|
*/
|
|
54
|
-
add(id: BinaryTreeNodeId, val?: N['val']
|
|
38
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
55
39
|
/**
|
|
56
|
-
* The
|
|
40
|
+
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
57
41
|
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
|
|
58
|
-
* generic type `N`. It represents the
|
|
42
|
+
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
59
43
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
60
|
-
* specifies the property name to use for searching the binary
|
|
61
|
-
*
|
|
62
|
-
* @returns The method is returning a N object or null.
|
|
44
|
+
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
|
|
45
|
+
* @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
|
|
63
46
|
*/
|
|
64
47
|
get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null;
|
|
65
48
|
/**
|
|
66
49
|
* The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
|
|
67
50
|
* leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
|
|
68
|
-
* @returns The
|
|
69
|
-
* the
|
|
70
|
-
*
|
|
71
|
-
* there are no nodes in
|
|
51
|
+
* @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
|
|
52
|
+
* the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
|
|
53
|
+
* equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
72
54
|
*/
|
|
73
55
|
lastKey(): BinaryTreeNodeId;
|
|
74
56
|
/**
|
|
75
|
-
* The function `getNodes` returns an array of binary
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* generic type `N`. It represents the property value that you want to search for in the binary search tree.
|
|
57
|
+
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
58
|
+
* @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
|
|
59
|
+
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
79
60
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
80
|
-
* specifies the property
|
|
81
|
-
* `
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @returns an array of N objects.
|
|
61
|
+
* specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
|
|
62
|
+
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
63
|
+
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
64
|
+
* is set to `true`, the function will return an array with only one node (if
|
|
65
|
+
* @returns an array of nodes (type N).
|
|
86
66
|
*/
|
|
87
67
|
getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
|
|
88
68
|
/**
|
|
89
|
-
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a
|
|
90
|
-
* than a given node.
|
|
69
|
+
* The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
|
|
70
|
+
* less than a given node.
|
|
91
71
|
* @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
|
|
92
72
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
93
73
|
* specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
|
|
94
|
-
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in
|
|
95
|
-
* tree that have a lesser value than the specified `beginNode` based on the
|
|
74
|
+
* @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
|
|
75
|
+
* binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
|
|
96
76
|
*/
|
|
97
77
|
lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number;
|
|
98
78
|
/**
|
|
99
|
-
* The
|
|
100
|
-
*
|
|
101
|
-
* @param node - The `node` parameter
|
|
102
|
-
*
|
|
79
|
+
* The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
80
|
+
* have a greater value than a given node.
|
|
81
|
+
* @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
|
|
82
|
+
* `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
|
|
103
83
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
104
|
-
* each node should be increased.
|
|
105
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that
|
|
106
|
-
* property of the
|
|
107
|
-
*
|
|
84
|
+
* each greater node should be increased.
|
|
85
|
+
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
86
|
+
* specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
|
|
87
|
+
* 'id'.
|
|
108
88
|
* @returns a boolean value.
|
|
109
89
|
*/
|
|
110
90
|
allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
111
91
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
92
|
+
* Balancing Adjustment:
|
|
93
|
+
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
94
|
+
* AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
|
|
95
|
+
*
|
|
96
|
+
* Use Cases and Efficiency:
|
|
97
|
+
* Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
|
|
98
|
+
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
115
99
|
*/
|
|
116
|
-
balance(): boolean;
|
|
117
100
|
/**
|
|
118
|
-
* The
|
|
119
|
-
*
|
|
120
|
-
*
|
|
101
|
+
* The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
|
|
102
|
+
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
103
|
+
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
104
|
+
*/
|
|
105
|
+
perfectlyBalance(): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
108
|
+
* @returns a boolean value.
|
|
121
109
|
*/
|
|
122
110
|
isAVLBalanced(): boolean;
|
|
123
111
|
protected _comparator: BSTComparator;
|