data-structure-typed 1.51.8 → 1.52.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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +152 -152
- package/dist/cjs/data-structures/base/index.d.ts +2 -1
- package/dist/cjs/data-structures/base/index.js +2 -1
- package/dist/cjs/data-structures/base/index.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js +226 -0
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -0
- package/dist/cjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/cjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +13 -190
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/cjs/data-structures/binary-tree/bst.js +207 -249
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/cjs/data-structures/hash/hash-map.js +40 -55
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/cjs/data-structures/heap/heap.js +59 -127
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/cjs/data-structures/heap/max-heap.js +76 -10
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/cjs/data-structures/heap/min-heap.js +68 -11
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +21 -20
- package/dist/cjs/data-structures/queue/deque.js +29 -23
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/cjs/data-structures/queue/queue.js +15 -31
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/cjs/data-structures/stack/stack.js +25 -24
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +19 -14
- package/dist/cjs/data-structures/trie/trie.js +27 -16
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/cjs/types/common.d.ts +1 -2
- package/dist/cjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/cjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/cjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/cjs/utils/utils.js +3 -5
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/index.d.ts +2 -1
- package/dist/mjs/data-structures/base/index.js +2 -1
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/mjs/data-structures/base/iterable-element-base.js +222 -0
- package/dist/mjs/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/mjs/data-structures/base/{iterable-base.js → iterable-entry-base.js} +10 -186
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +476 -364
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/mjs/data-structures/binary-tree/bst.js +211 -255
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
- package/dist/mjs/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/mjs/data-structures/hash/hash-map.js +41 -55
- package/dist/mjs/data-structures/heap/heap.d.ts +43 -114
- package/dist/mjs/data-structures/heap/heap.js +60 -128
- package/dist/mjs/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/mjs/data-structures/heap/max-heap.js +79 -10
- package/dist/mjs/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/mjs/data-structures/heap/min-heap.js +68 -11
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +82 -10
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +21 -20
- package/dist/mjs/data-structures/queue/deque.js +29 -23
- package/dist/mjs/data-structures/queue/queue.d.ts +8 -28
- package/dist/mjs/data-structures/queue/queue.js +15 -31
- package/dist/mjs/data-structures/stack/stack.d.ts +17 -22
- package/dist/mjs/data-structures/stack/stack.js +25 -24
- package/dist/mjs/data-structures/trie/trie.d.ts +19 -14
- package/dist/mjs/data-structures/trie/trie.js +27 -16
- package/dist/mjs/interfaces/binary-tree.d.ts +7 -7
- package/dist/mjs/types/common.d.ts +1 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +5 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/mjs/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/mjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/mjs/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/mjs/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/mjs/utils/utils.js +3 -5
- package/dist/umd/data-structure-typed.js +1949 -1481
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
- package/test/integration/avl-tree.test.ts +2 -2
- package/test/integration/bst.test.ts +3 -3
- package/test/integration/index.html +25 -11
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +80 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +44 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +18 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +42 -0
- package/test/unit/data-structures/queue/deque.test.ts +30 -0
- package/test/unit/data-structures/queue/queue.test.ts +56 -0
- package/test/utils/big-o.ts +12 -6
- package/tsconfig-base.json +1 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +0 -1
|
@@ -59,23 +59,22 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
59
59
|
*/
|
|
60
60
|
export class BST extends BinaryTree {
|
|
61
61
|
/**
|
|
62
|
-
* This is the constructor function for a Binary Search Tree class in TypeScript
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
constructor(keysOrNodesOrEntries = [], options) {
|
|
62
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
63
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
64
|
+
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
65
|
+
* added to the binary search tree during the construction of the object.
|
|
66
|
+
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
67
|
+
* It can include a comparator function that defines the order of the elements in the tree.
|
|
68
|
+
*/
|
|
69
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
71
70
|
super([], options);
|
|
72
71
|
if (options) {
|
|
73
72
|
const { comparator } = options;
|
|
74
73
|
if (comparator)
|
|
75
74
|
this._comparator = comparator;
|
|
76
75
|
}
|
|
77
|
-
if (
|
|
78
|
-
this.addMany(
|
|
76
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
77
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
79
78
|
}
|
|
80
79
|
_root = undefined;
|
|
81
80
|
/**
|
|
@@ -85,20 +84,6 @@ export class BST extends BinaryTree {
|
|
|
85
84
|
get root() {
|
|
86
85
|
return this._root;
|
|
87
86
|
}
|
|
88
|
-
_comparator = (a, b) => {
|
|
89
|
-
if (a > b)
|
|
90
|
-
return 1;
|
|
91
|
-
if (a < b)
|
|
92
|
-
return -1;
|
|
93
|
-
return 0;
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* The function returns the value of the _comparator property.
|
|
97
|
-
* @returns The `_comparator` property is being returned.
|
|
98
|
-
*/
|
|
99
|
-
get comparator() {
|
|
100
|
-
return this._comparator;
|
|
101
|
-
}
|
|
102
87
|
/**
|
|
103
88
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
104
89
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -113,10 +98,9 @@ export class BST extends BinaryTree {
|
|
|
113
98
|
/**
|
|
114
99
|
* The function creates a new binary search tree with the specified options.
|
|
115
100
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
116
|
-
* behavior of the `createTree` method. It
|
|
117
|
-
*
|
|
118
|
-
* @returns a new instance of the BST class
|
|
119
|
-
* options. The returned value is casted as TREE.
|
|
101
|
+
* behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which has the
|
|
102
|
+
* following properties:
|
|
103
|
+
* @returns a new instance of the BST class with the provided options.
|
|
120
104
|
*/
|
|
121
105
|
createTree(options) {
|
|
122
106
|
return new BST([], {
|
|
@@ -126,95 +110,58 @@ export class BST extends BinaryTree {
|
|
|
126
110
|
});
|
|
127
111
|
}
|
|
128
112
|
/**
|
|
129
|
-
* The function
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* `
|
|
134
|
-
*
|
|
135
|
-
|
|
136
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
137
|
-
let node;
|
|
138
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
142
|
-
node = keyOrNodeOrEntry;
|
|
143
|
-
}
|
|
144
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
145
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
146
|
-
if (key === undefined || key === null) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
node = this.createNode(key, value);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
154
|
-
node = this.createNode(keyOrNodeOrEntry, value);
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
return node;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Time Complexity: O(log n)
|
|
163
|
-
* Space Complexity: O(log n)
|
|
113
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
114
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
|
|
115
|
+
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
116
|
+
* element.
|
|
117
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
118
|
+
* value associated with a key in a key-value pair.
|
|
119
|
+
* @returns either a NODE object or undefined.
|
|
164
120
|
*/
|
|
121
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
|
|
122
|
+
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
|
|
123
|
+
}
|
|
165
124
|
/**
|
|
166
125
|
* Time Complexity: O(log n)
|
|
167
126
|
* Space Complexity: O(log n)
|
|
168
127
|
*
|
|
169
|
-
* The function
|
|
170
|
-
*
|
|
171
|
-
* @param {
|
|
172
|
-
* `
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
184
|
-
const key = keyOrNodeOrEntry[0];
|
|
185
|
-
if (key === null || key === undefined)
|
|
186
|
-
return;
|
|
187
|
-
return this.getNodeByKey(key, iterationType);
|
|
188
|
-
}
|
|
189
|
-
const key = keyOrNodeOrEntry;
|
|
190
|
-
if (key === null || key === undefined)
|
|
191
|
-
return;
|
|
192
|
-
return this.getNodeByKey(key, iterationType);
|
|
128
|
+
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
129
|
+
* it doesn't exist.
|
|
130
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
131
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
|
|
132
|
+
* entry, or raw element that needs to be ensured in the tree.
|
|
133
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
134
|
+
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
135
|
+
* value of `'ITERATIVE'`.
|
|
136
|
+
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
137
|
+
* not be ensured.
|
|
138
|
+
*/
|
|
139
|
+
ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = 'ITERATIVE') {
|
|
140
|
+
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
|
|
193
141
|
}
|
|
194
142
|
/**
|
|
195
|
-
* The function checks if
|
|
196
|
-
* @param
|
|
197
|
-
*
|
|
143
|
+
* The function checks if the input is an instance of the BSTNode class.
|
|
144
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
145
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
146
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
147
|
+
* an instance of the `BSTNode` class.
|
|
198
148
|
*/
|
|
199
|
-
isNode(
|
|
200
|
-
return
|
|
149
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
150
|
+
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
201
151
|
}
|
|
202
|
-
/**
|
|
203
|
-
* Time Complexity: O(log n)
|
|
204
|
-
* Space Complexity: O(1)
|
|
205
|
-
*/
|
|
206
152
|
/**
|
|
207
153
|
* Time Complexity: O(log n)
|
|
208
154
|
* Space Complexity: O(1)
|
|
209
155
|
*
|
|
210
|
-
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* @param {V} [value] - The value
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
156
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
157
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
158
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
159
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
160
|
+
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
161
|
+
* @returns a boolean value.
|
|
162
|
+
*/
|
|
163
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
164
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
218
165
|
if (newNode === undefined)
|
|
219
166
|
return false;
|
|
220
167
|
if (this.root === undefined) {
|
|
@@ -225,15 +172,8 @@ export class BST extends BinaryTree {
|
|
|
225
172
|
let current = this.root;
|
|
226
173
|
while (current !== undefined) {
|
|
227
174
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
228
|
-
// if (current !== newNode) {
|
|
229
|
-
// The key value is the same but the reference is different, update the value of the existing node
|
|
230
175
|
this._replaceNode(current, newNode);
|
|
231
176
|
return true;
|
|
232
|
-
// } else {
|
|
233
|
-
// The key value is the same and the reference is the same, replace the entire node
|
|
234
|
-
// this._replaceNode(current, newNode);
|
|
235
|
-
// return;
|
|
236
|
-
// }
|
|
237
177
|
}
|
|
238
178
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
239
179
|
if (current.left === undefined) {
|
|
@@ -255,18 +195,17 @@ export class BST extends BinaryTree {
|
|
|
255
195
|
return false;
|
|
256
196
|
}
|
|
257
197
|
/**
|
|
258
|
-
* Time Complexity: O(
|
|
259
|
-
* Space Complexity: O(
|
|
198
|
+
* Time Complexity: O(log n)
|
|
199
|
+
* Space Complexity: O(log n)
|
|
260
200
|
*/
|
|
261
201
|
/**
|
|
262
202
|
* Time Complexity: O(k log n)
|
|
263
203
|
* Space Complexity: O(k + log n)
|
|
264
204
|
*
|
|
265
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* data structure.
|
|
205
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
206
|
+
* an array indicating whether each key or node was successfully inserted.
|
|
207
|
+
* @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
|
|
208
|
+
* elements to be added to the data structure.
|
|
270
209
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
271
210
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
272
211
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
@@ -275,20 +214,19 @@ export class BST extends BinaryTree {
|
|
|
275
214
|
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
276
215
|
* value is true.
|
|
277
216
|
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
278
|
-
* specifies the type of iteration to use when adding multiple keys or nodes to the binary
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* or entry was successfully inserted into the data structure.
|
|
217
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary search
|
|
218
|
+
* tree. It can have two possible values:
|
|
219
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
220
|
+
* successfully inserted into the data structure.
|
|
283
221
|
*/
|
|
284
|
-
addMany(
|
|
222
|
+
addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
285
223
|
const inserted = [];
|
|
286
224
|
let valuesIterator;
|
|
287
225
|
if (values) {
|
|
288
226
|
valuesIterator = values[Symbol.iterator]();
|
|
289
227
|
}
|
|
290
228
|
if (!isBalanceAdd) {
|
|
291
|
-
for (const kve of
|
|
229
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
292
230
|
const value = valuesIterator?.next().value;
|
|
293
231
|
const nn = this.add(kve, value);
|
|
294
232
|
inserted.push(nn);
|
|
@@ -301,25 +239,32 @@ export class BST extends BinaryTree {
|
|
|
301
239
|
return false;
|
|
302
240
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
303
241
|
};
|
|
304
|
-
for (const kve of
|
|
242
|
+
for (const kve of keysOrNodesOrEntriesOrRawElements) {
|
|
305
243
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
306
244
|
}
|
|
307
245
|
let sorted = [];
|
|
308
246
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
309
247
|
let keyA, keyB;
|
|
310
|
-
if (this.isEntry(a))
|
|
248
|
+
if (this.isEntry(a))
|
|
311
249
|
keyA = a[0];
|
|
312
|
-
}
|
|
313
250
|
else if (this.isRealNode(a))
|
|
314
251
|
keyA = a.key;
|
|
315
|
-
else
|
|
252
|
+
else if (this.toEntryFn) {
|
|
253
|
+
keyA = this.toEntryFn(a)[0];
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
316
256
|
keyA = a;
|
|
257
|
+
}
|
|
317
258
|
if (this.isEntry(b))
|
|
318
259
|
keyB = b[0];
|
|
319
260
|
else if (this.isRealNode(b))
|
|
320
261
|
keyB = b.key;
|
|
321
|
-
else
|
|
262
|
+
else if (this.toEntryFn) {
|
|
263
|
+
keyB = this.toEntryFn(b)[0];
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
322
266
|
keyB = b;
|
|
267
|
+
}
|
|
323
268
|
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
324
269
|
return this.comparator(keyA, keyB);
|
|
325
270
|
}
|
|
@@ -360,32 +305,26 @@ export class BST extends BinaryTree {
|
|
|
360
305
|
return inserted;
|
|
361
306
|
}
|
|
362
307
|
/**
|
|
363
|
-
* Time Complexity: O(log n)
|
|
364
|
-
* Space Complexity: O(k + log n)
|
|
365
|
-
* /
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
308
|
* Time Complexity: O(log n)
|
|
369
309
|
* Space Complexity: O(k + log n)
|
|
370
310
|
*
|
|
371
|
-
* The
|
|
372
|
-
*
|
|
311
|
+
* The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
|
|
312
|
+
* identifier and callback function.
|
|
373
313
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
374
|
-
* want to search for in the
|
|
375
|
-
*
|
|
376
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node
|
|
377
|
-
*
|
|
378
|
-
* function
|
|
379
|
-
* @param [onlyOne=false] - A boolean
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* @returns The method returns an array of nodes (`NODE[]`).
|
|
314
|
+
* want to search for in the binary tree. It can be of any type that is returned by the callback
|
|
315
|
+
* function.
|
|
316
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node as input and
|
|
317
|
+
* returns a value. This value is used to identify the nodes that match the given identifier. The
|
|
318
|
+
* `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
319
|
+
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
|
|
320
|
+
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
|
|
321
|
+
* false, all matching nodes will be returned. The default value is false.
|
|
322
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
323
|
+
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
|
|
324
|
+
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
|
|
325
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
326
|
+
* iteration to be performed. It can have two possible values:
|
|
327
|
+
* @returns The method `getNodes` returns an array of `NODE` objects.
|
|
389
328
|
*/
|
|
390
329
|
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
391
330
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -456,50 +395,49 @@ export class BST extends BinaryTree {
|
|
|
456
395
|
* Time Complexity: O(log n)
|
|
457
396
|
* Space Complexity: O(1)
|
|
458
397
|
*
|
|
459
|
-
* The `getNode`
|
|
460
|
-
*
|
|
461
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
465
|
-
* the
|
|
466
|
-
*
|
|
467
|
-
*
|
|
398
|
+
* The function `getNode` returns the first node that matches the given identifier and callback
|
|
399
|
+
* function in a binary search tree.
|
|
400
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
401
|
+
* want to search for in the binary search tree. It can be of any type that is compatible with the
|
|
402
|
+
* type returned by the callback function.
|
|
403
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
404
|
+
* node matches the desired criteria. It should be a function that takes a node as an argument and
|
|
405
|
+
* returns a boolean value indicating whether the node matches the criteria or not. If no callback is
|
|
406
|
+
* provided, the default callback will be
|
|
468
407
|
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
469
|
-
* search tree. It can be either a key or a node. If it is a key,
|
|
470
|
-
*
|
|
471
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
472
|
-
* be performed when searching for nodes in the binary search tree. It
|
|
473
|
-
*
|
|
474
|
-
* @returns The method is returning a
|
|
408
|
+
* search tree. It can be either a key or a node. If it is a key, the search will start from the node
|
|
409
|
+
* with that key. If it is a node, the search will start from that node.
|
|
410
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
411
|
+
* of iteration to be performed when searching for nodes in the binary search tree. It can have one
|
|
412
|
+
* of the following values:
|
|
413
|
+
* @returns The method is returning a NODE object or undefined.
|
|
475
414
|
*/
|
|
476
415
|
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
477
416
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
478
417
|
}
|
|
479
418
|
/**
|
|
480
|
-
* Time Complexity: O(log n)
|
|
481
|
-
* Space Complexity: O(
|
|
419
|
+
* Time Complexity: O(k log n)
|
|
420
|
+
* Space Complexity: O(k + log n)
|
|
482
421
|
*/
|
|
483
422
|
/**
|
|
484
423
|
* Time Complexity: O(log n)
|
|
485
424
|
* Space Complexity: O(1)
|
|
486
425
|
*
|
|
487
|
-
* The function `getNodeByKey`
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
* @param iterationType - The `iterationType` parameter is an optional
|
|
492
|
-
* type of iteration to
|
|
493
|
-
*
|
|
494
|
-
* @returns The
|
|
495
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
426
|
+
* The function `getNodeByKey` returns a node with a specific key from a tree data structure.
|
|
427
|
+
* @param {K} key - The key parameter is the value used to search for a specific node in the tree. It
|
|
428
|
+
* is typically a unique identifier or a value that can be used to determine the position of the node
|
|
429
|
+
* in the tree structure.
|
|
430
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
431
|
+
* parameter that specifies the type of iteration to be used when searching for a node in the tree.
|
|
432
|
+
* It has a default value of `'ITERATIVE'`.
|
|
433
|
+
* @returns The method is returning a NODE object or undefined.
|
|
496
434
|
*/
|
|
497
435
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
498
436
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
499
437
|
}
|
|
500
438
|
/**
|
|
501
|
-
* Time
|
|
502
|
-
* Space
|
|
439
|
+
* Time Complexity: O(log n)
|
|
440
|
+
* Space Complexity: O(k + log n)
|
|
503
441
|
*/
|
|
504
442
|
/**
|
|
505
443
|
* Time complexity: O(n)
|
|
@@ -508,15 +446,16 @@ export class BST extends BinaryTree {
|
|
|
508
446
|
* The function overrides the depth-first search method and returns an array of the return types of
|
|
509
447
|
* the callback function.
|
|
510
448
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
511
|
-
* during the depth-first search traversal. It is an optional parameter and
|
|
512
|
-
*
|
|
513
|
-
* @param {DFSOrderPattern} [pattern=
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
449
|
+
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
450
|
+
* `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
|
|
451
|
+
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
452
|
+
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
453
|
+
* take one of the following values:
|
|
454
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
455
|
+
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
456
|
+
* node entry. If not specified, the default value is the root of the tree.
|
|
457
|
+
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
458
|
+
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
|
|
520
459
|
* following values:
|
|
521
460
|
* @returns The method is returning an array of the return type of the callback function.
|
|
522
461
|
*/
|
|
@@ -524,8 +463,8 @@ export class BST extends BinaryTree {
|
|
|
524
463
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
525
464
|
}
|
|
526
465
|
/**
|
|
527
|
-
* Time
|
|
528
|
-
* Space
|
|
466
|
+
* Time Complexity: O(log n)
|
|
467
|
+
* Space Complexity: O(1)
|
|
529
468
|
*/
|
|
530
469
|
/**
|
|
531
470
|
* Time complexity: O(n)
|
|
@@ -534,38 +473,38 @@ export class BST extends BinaryTree {
|
|
|
534
473
|
* The function overrides the breadth-first search method and returns an array of the return types of
|
|
535
474
|
* the callback function.
|
|
536
475
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
537
|
-
* visited during the breadth-first search
|
|
538
|
-
*
|
|
539
|
-
* @param beginRoot - The `beginRoot` parameter is the starting
|
|
540
|
-
*
|
|
541
|
-
* the
|
|
542
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type
|
|
543
|
-
* be performed during the breadth-first search (BFS) traversal. It
|
|
544
|
-
*
|
|
545
|
-
* @returns
|
|
476
|
+
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
477
|
+
* node being visited, and it can return a value of any type.
|
|
478
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
479
|
+
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
480
|
+
* object. If no value is provided, the default value is the root of the tree.
|
|
481
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
482
|
+
* of iteration to be performed during the breadth-first search (BFS) traversal. It can have one of
|
|
483
|
+
* the following values:
|
|
484
|
+
* @returns an array of the return type of the callback function.
|
|
546
485
|
*/
|
|
547
486
|
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
548
487
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
549
488
|
}
|
|
550
489
|
/**
|
|
551
|
-
* Time
|
|
552
|
-
* Space
|
|
490
|
+
* Time Complexity: O(log n)
|
|
491
|
+
* Space Complexity: O(1)
|
|
553
492
|
*/
|
|
554
493
|
/**
|
|
555
494
|
* Time complexity: O(n)
|
|
556
495
|
* Space complexity: O(n)
|
|
557
496
|
*
|
|
558
|
-
* The function overrides the listLevels method and returns an array of arrays
|
|
559
|
-
*
|
|
497
|
+
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
498
|
+
* containing the results of the callback function applied to each level of the tree.
|
|
560
499
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
561
|
-
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
562
|
-
* during the
|
|
563
|
-
* @param beginRoot - The `beginRoot` parameter is
|
|
564
|
-
* levels of
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
* iteration.
|
|
500
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
501
|
+
* tree during the iteration process.
|
|
502
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
503
|
+
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
504
|
+
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
505
|
+
* value is provided, the root of
|
|
506
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
507
|
+
* of iteration to be performed on the tree. It can have one of the following values:
|
|
569
508
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
570
509
|
* function.
|
|
571
510
|
*/
|
|
@@ -573,27 +512,27 @@ export class BST extends BinaryTree {
|
|
|
573
512
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
574
513
|
}
|
|
575
514
|
/**
|
|
576
|
-
* Time
|
|
577
|
-
* Space
|
|
515
|
+
* Time complexity: O(n)
|
|
516
|
+
* Space complexity: O(n)
|
|
578
517
|
*/
|
|
579
518
|
/**
|
|
580
|
-
* Time
|
|
581
|
-
* Space
|
|
519
|
+
* Time complexity: O(n)
|
|
520
|
+
* Space complexity: O(n)
|
|
582
521
|
*
|
|
583
|
-
* The `lesserOrGreaterTraverse` function traverses a binary tree and
|
|
584
|
-
*
|
|
522
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
523
|
+
* each node that meets a certain condition based on a target node and a comparison value.
|
|
585
524
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
586
|
-
* that
|
|
587
|
-
*
|
|
525
|
+
* that meets the condition specified by the `lesserOrGreater` parameter. It takes a single argument,
|
|
526
|
+
* which is the current node being traversed, and returns a value of any type.
|
|
588
527
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
589
|
-
* traverse nodes that are lesser
|
|
590
|
-
*
|
|
591
|
-
* `
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
* @param iterationType - The `iterationType` parameter determines the type of
|
|
596
|
-
* performed on the binary tree. It can have two possible values:
|
|
528
|
+
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
529
|
+
* 0, or 1, where:
|
|
530
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
|
|
531
|
+
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
532
|
+
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
533
|
+
* `targetNode` is provided,
|
|
534
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
535
|
+
* traversal to be performed on the binary tree. It can have two possible values:
|
|
597
536
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
598
537
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
599
538
|
*/
|
|
@@ -636,18 +575,19 @@ export class BST extends BinaryTree {
|
|
|
636
575
|
}
|
|
637
576
|
}
|
|
638
577
|
/**
|
|
639
|
-
* Time
|
|
640
|
-
* Space
|
|
578
|
+
* Time complexity: O(n)
|
|
579
|
+
* Space complexity: O(n)
|
|
641
580
|
*/
|
|
642
581
|
/**
|
|
643
|
-
* Time
|
|
644
|
-
* Space
|
|
582
|
+
* Time complexity: O(n)
|
|
583
|
+
* Space complexity: O(n)
|
|
645
584
|
*
|
|
646
|
-
* The `perfectlyBalance` function
|
|
647
|
-
*
|
|
648
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
649
|
-
* type of iteration to use when building a balanced binary search tree. It
|
|
650
|
-
*
|
|
585
|
+
* The `perfectlyBalance` function takes an optional `iterationType` parameter and returns `true` if
|
|
586
|
+
* the binary search tree is perfectly balanced, otherwise it returns `false`.
|
|
587
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
588
|
+
* specifies the type of iteration to use when building a balanced binary search tree. It has a
|
|
589
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
590
|
+
* current instance of the class.
|
|
651
591
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
652
592
|
*/
|
|
653
593
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
@@ -687,25 +627,19 @@ export class BST extends BinaryTree {
|
|
|
687
627
|
}
|
|
688
628
|
}
|
|
689
629
|
/**
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
* 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.
|
|
693
|
-
*
|
|
694
|
-
* Use Cases and Efficiency:
|
|
695
|
-
* 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.
|
|
696
|
-
* 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).
|
|
697
|
-
*/
|
|
698
|
-
/**
|
|
699
|
-
* Time Complexity: O(n)
|
|
700
|
-
* Space Complexity: O(log n)
|
|
630
|
+
* Time complexity: O(n)
|
|
631
|
+
* Space complexity: O(n)
|
|
701
632
|
*/
|
|
702
633
|
/**
|
|
703
634
|
* Time Complexity: O(n)
|
|
704
635
|
* Space Complexity: O(log n)
|
|
705
636
|
*
|
|
706
|
-
* The function checks if a binary tree is AVL balanced using either recursive or
|
|
707
|
-
*
|
|
708
|
-
*
|
|
637
|
+
* The function `isAVLBalanced` checks if a binary tree is AVL balanced using either a recursive or
|
|
638
|
+
* iterative approach.
|
|
639
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
640
|
+
* specifies the type of iteration to use when checking if the AVL tree is balanced. It has a default
|
|
641
|
+
* value of `this.iterationType`, which means it will use the iteration type specified in the current
|
|
642
|
+
* instance of the AVL tree.
|
|
709
643
|
* @returns a boolean value.
|
|
710
644
|
*/
|
|
711
645
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -753,10 +687,32 @@ export class BST extends BinaryTree {
|
|
|
753
687
|
}
|
|
754
688
|
return balanced;
|
|
755
689
|
}
|
|
690
|
+
_DEFAULT_COMPARATOR = (a, b) => {
|
|
691
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
692
|
+
throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
|
|
693
|
+
}
|
|
694
|
+
if (a > b)
|
|
695
|
+
return 1;
|
|
696
|
+
if (a < b)
|
|
697
|
+
return -1;
|
|
698
|
+
return 0;
|
|
699
|
+
};
|
|
700
|
+
_comparator = this._DEFAULT_COMPARATOR;
|
|
701
|
+
/**
|
|
702
|
+
* Time Complexity: O(n)
|
|
703
|
+
* Space Complexity: O(log n)
|
|
704
|
+
*/
|
|
705
|
+
/**
|
|
706
|
+
* The function returns the value of the _comparator property.
|
|
707
|
+
* @returns The `_comparator` property is being returned.
|
|
708
|
+
*/
|
|
709
|
+
get comparator() {
|
|
710
|
+
return this._comparator;
|
|
711
|
+
}
|
|
756
712
|
/**
|
|
757
|
-
* The function sets the root
|
|
758
|
-
*
|
|
759
|
-
*
|
|
713
|
+
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
714
|
+
* root.
|
|
715
|
+
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
760
716
|
*/
|
|
761
717
|
_setRoot(v) {
|
|
762
718
|
if (v) {
|