data-structure-typed 1.47.5 → 1.47.7
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- 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 +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- 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 +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- 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 +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- 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 +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/cjs/types/common.d.ts +7 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/mjs/types/common.d.ts +7 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +629 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +156 -0
- package/tsconfig-cjs.json +1 -1
- package/test/integration/conversion.test.ts +0 -0
|
@@ -14,38 +14,17 @@ import { Queue } from '../queue';
|
|
|
14
14
|
* @template N - The type of the family relationship in the binary tree.
|
|
15
15
|
*/
|
|
16
16
|
export class BinaryTreeNode {
|
|
17
|
-
/**
|
|
18
|
-
* The key associated with the node.
|
|
19
|
-
*/
|
|
20
17
|
key;
|
|
21
|
-
/**
|
|
22
|
-
* The value stored in the node.
|
|
23
|
-
*/
|
|
24
18
|
value;
|
|
25
|
-
/**
|
|
26
|
-
* The parent node of the current node.
|
|
27
|
-
*/
|
|
28
19
|
parent;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new instance of BinaryTreeNode.
|
|
31
|
-
* @param {BTNKey} key - The key associated with the node.
|
|
32
|
-
* @param {V} value - The value stored in the node.
|
|
33
|
-
*/
|
|
34
20
|
constructor(key, value) {
|
|
35
21
|
this.key = key;
|
|
36
22
|
this.value = value;
|
|
37
23
|
}
|
|
38
24
|
_left;
|
|
39
|
-
/**
|
|
40
|
-
* Get the left child node.
|
|
41
|
-
*/
|
|
42
25
|
get left() {
|
|
43
26
|
return this._left;
|
|
44
27
|
}
|
|
45
|
-
/**
|
|
46
|
-
* Set the left child node.
|
|
47
|
-
* @param {N | null | undefined} v - The left child node.
|
|
48
|
-
*/
|
|
49
28
|
set left(v) {
|
|
50
29
|
if (v) {
|
|
51
30
|
v.parent = this;
|
|
@@ -53,16 +32,9 @@ export class BinaryTreeNode {
|
|
|
53
32
|
this._left = v;
|
|
54
33
|
}
|
|
55
34
|
_right;
|
|
56
|
-
/**
|
|
57
|
-
* Get the right child node.
|
|
58
|
-
*/
|
|
59
35
|
get right() {
|
|
60
36
|
return this._right;
|
|
61
37
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Set the right child node.
|
|
64
|
-
* @param {N | null | undefined} v - The right child node.
|
|
65
|
-
*/
|
|
66
38
|
set right(v) {
|
|
67
39
|
if (v) {
|
|
68
40
|
v.parent = this;
|
|
@@ -88,35 +60,43 @@ export class BinaryTreeNode {
|
|
|
88
60
|
}
|
|
89
61
|
}
|
|
90
62
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
63
|
+
* 1. Two Children Maximum: Each node has at most two children.
|
|
64
|
+
* 2. Left and Right Children: Nodes have distinct left and right children.
|
|
65
|
+
* 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
|
|
66
|
+
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
67
|
+
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
68
|
+
* 6. Internal Nodes: Nodes with at least one child are internal.
|
|
69
|
+
* 7. Balanced Trees: The heights of the left and right subtrees of any node differ by no more than one.
|
|
70
|
+
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
|
+
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
93
72
|
*/
|
|
94
73
|
export class BinaryTree {
|
|
95
|
-
|
|
74
|
+
iterationType = IterationType.ITERATIVE;
|
|
96
75
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param
|
|
76
|
+
* The constructor function initializes a binary tree object with optional elements and options.
|
|
77
|
+
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
78
|
+
* elements to be added to the binary tree.
|
|
79
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
80
|
+
* configuration options for the binary tree. In this case, it is of type
|
|
81
|
+
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
|
82
|
+
* required.
|
|
99
83
|
*/
|
|
100
|
-
constructor(options) {
|
|
84
|
+
constructor(elements, options) {
|
|
101
85
|
if (options) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
86
|
+
const { iterationType } = options;
|
|
87
|
+
if (iterationType) {
|
|
88
|
+
this.iterationType = iterationType;
|
|
89
|
+
}
|
|
106
90
|
}
|
|
107
91
|
this._size = 0;
|
|
92
|
+
if (elements)
|
|
93
|
+
this.addMany(elements);
|
|
108
94
|
}
|
|
109
95
|
_root;
|
|
110
|
-
/**
|
|
111
|
-
* Get the root node of the binary tree.
|
|
112
|
-
*/
|
|
113
96
|
get root() {
|
|
114
97
|
return this._root;
|
|
115
98
|
}
|
|
116
99
|
_size;
|
|
117
|
-
/**
|
|
118
|
-
* Get the number of nodes in the binary tree.
|
|
119
|
-
*/
|
|
120
100
|
get size() {
|
|
121
101
|
return this._size;
|
|
122
102
|
}
|
|
@@ -129,35 +109,46 @@ export class BinaryTree {
|
|
|
129
109
|
createNode(key, value) {
|
|
130
110
|
return new BinaryTreeNode(key, value);
|
|
131
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* The function creates a binary tree with the given options.
|
|
114
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
115
|
+
* behavior of the `BinaryTree` class. It is of type `Partial<BinaryTreeOptions>`, which means that
|
|
116
|
+
* you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
|
|
117
|
+
* @returns a new instance of a binary tree.
|
|
118
|
+
*/
|
|
132
119
|
createTree(options) {
|
|
133
|
-
return new BinaryTree({
|
|
120
|
+
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
134
121
|
}
|
|
135
122
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
123
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
124
|
+
* @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
|
|
125
|
+
* two type parameters V and N, representing the value and node type respectively.
|
|
126
|
+
* @returns a boolean value.
|
|
139
127
|
*/
|
|
128
|
+
isEntry(kne) {
|
|
129
|
+
return Array.isArray(kne) && kne.length === 2;
|
|
130
|
+
}
|
|
140
131
|
/**
|
|
141
|
-
* Time Complexity
|
|
142
|
-
* Space Complexity
|
|
132
|
+
* Time Complexity O(log n) - O(n)
|
|
133
|
+
* Space Complexity O(1)
|
|
134
|
+
*/
|
|
135
|
+
/**
|
|
136
|
+
* Time Complexity O(log n) - O(n)
|
|
137
|
+
* Space Complexity O(1)
|
|
143
138
|
*
|
|
144
|
-
* The `add` function adds a new node
|
|
145
|
-
*
|
|
146
|
-
* @
|
|
147
|
-
* following types:
|
|
148
|
-
* @param {V} [value] - The value to be associated with the key or node being added to the binary
|
|
149
|
-
* tree.
|
|
150
|
-
* @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
|
|
151
|
-
* tree, or `null` or `undefined` if the insertion was not successful.
|
|
139
|
+
* The `add` function adds a new node to a binary tree, either by key or by providing a node object.
|
|
140
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be one of the following:
|
|
141
|
+
* @returns The function `add` returns the inserted node (`N`), `null`, or `undefined`.
|
|
152
142
|
*/
|
|
153
|
-
add(
|
|
143
|
+
add(keyOrNodeOrEntry) {
|
|
144
|
+
let inserted, needInsert;
|
|
154
145
|
const _bfs = (root, newNode) => {
|
|
155
146
|
const queue = new Queue([root]);
|
|
156
147
|
while (queue.size > 0) {
|
|
157
148
|
const cur = queue.shift();
|
|
158
149
|
if (newNode && cur.key === newNode.key) {
|
|
159
|
-
cur
|
|
160
|
-
return;
|
|
150
|
+
this._replaceNode(cur, newNode);
|
|
151
|
+
return newNode;
|
|
161
152
|
}
|
|
162
153
|
const inserted = this._addTo(newNode, cur);
|
|
163
154
|
if (inserted !== undefined)
|
|
@@ -168,15 +159,26 @@ export class BinaryTree {
|
|
|
168
159
|
queue.push(cur.right);
|
|
169
160
|
}
|
|
170
161
|
};
|
|
171
|
-
|
|
172
|
-
if (keyOrNode === null) {
|
|
162
|
+
if (keyOrNodeOrEntry === null) {
|
|
173
163
|
needInsert = null;
|
|
174
164
|
}
|
|
175
|
-
else if (this.isNodeKey(
|
|
176
|
-
needInsert = this.createNode(
|
|
165
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
166
|
+
needInsert = this.createNode(keyOrNodeOrEntry);
|
|
177
167
|
}
|
|
178
|
-
else if (
|
|
179
|
-
needInsert =
|
|
168
|
+
else if (keyOrNodeOrEntry instanceof BinaryTreeNode) {
|
|
169
|
+
needInsert = keyOrNodeOrEntry;
|
|
170
|
+
}
|
|
171
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
172
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
173
|
+
if (key === undefined) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
else if (key === null) {
|
|
177
|
+
needInsert = null;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
needInsert = this.createNode(key, value);
|
|
181
|
+
}
|
|
180
182
|
}
|
|
181
183
|
else {
|
|
182
184
|
return;
|
|
@@ -197,35 +199,28 @@ export class BinaryTree {
|
|
|
197
199
|
return inserted;
|
|
198
200
|
}
|
|
199
201
|
/**
|
|
200
|
-
* Time Complexity: O(k
|
|
202
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
201
203
|
* Space Complexity: O(1)
|
|
204
|
+
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
202
205
|
*/
|
|
203
206
|
/**
|
|
204
|
-
* Time Complexity: O(k
|
|
207
|
+
* Time Complexity: O(k log n) - O(k * n)
|
|
205
208
|
* Space Complexity: O(1)
|
|
206
209
|
*
|
|
207
|
-
* The `addMany`
|
|
208
|
-
*
|
|
209
|
-
* @param
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
216
|
-
*/
|
|
217
|
-
addMany(keysOrNodes, values) {
|
|
210
|
+
* The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
|
|
211
|
+
* current instance, and returns an array of the inserted nodes.
|
|
212
|
+
* @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
|
|
213
|
+
* `BTNodeExemplar<V, N>` objects.
|
|
214
|
+
* @returns The function `addMany` returns an array of values, where each value is either of type
|
|
215
|
+
* `N`, `null`, or `undefined`.
|
|
216
|
+
*/
|
|
217
|
+
addMany(nodes) {
|
|
218
218
|
// TODO not sure addMany not be run multi times
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return this.add(null);
|
|
225
|
-
}
|
|
226
|
-
const value = values?.[i];
|
|
227
|
-
return this.add(keyOrNode, value);
|
|
228
|
-
});
|
|
219
|
+
const inserted = [];
|
|
220
|
+
for (const kne of nodes) {
|
|
221
|
+
inserted.push(this.add(kne));
|
|
222
|
+
}
|
|
223
|
+
return inserted;
|
|
229
224
|
}
|
|
230
225
|
/**
|
|
231
226
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -235,22 +230,14 @@ export class BinaryTree {
|
|
|
235
230
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
236
231
|
* Space Complexity: O(1)
|
|
237
232
|
*
|
|
238
|
-
* The `refill` function clears the
|
|
239
|
-
* @param
|
|
240
|
-
* `
|
|
241
|
-
* @param {N[] | Array<V>} [values] - The `data` parameter is an optional array of values that will be assigned to
|
|
242
|
-
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
|
|
243
|
-
* array. Each value in the `data` array will be assigned to the
|
|
244
|
-
* @returns The method is returning a boolean value.
|
|
233
|
+
* The `refill` function clears the current collection and adds new nodes, keys, or entries to it.
|
|
234
|
+
* @param nodesOrKeysOrEntries - The parameter `nodesOrKeysOrEntries` is an iterable object that can
|
|
235
|
+
* contain either `BTNodeExemplar` objects, keys, or entries.
|
|
245
236
|
*/
|
|
246
|
-
refill(
|
|
237
|
+
refill(nodesOrKeysOrEntries) {
|
|
247
238
|
this.clear();
|
|
248
|
-
|
|
239
|
+
this.addMany(nodesOrKeysOrEntries);
|
|
249
240
|
}
|
|
250
|
-
/**
|
|
251
|
-
* Time Complexity: O(n)
|
|
252
|
-
* Space Complexity: O(1)
|
|
253
|
-
*/
|
|
254
241
|
/**
|
|
255
242
|
* Time Complexity: O(n)
|
|
256
243
|
* Space Complexity: O(1)
|
|
@@ -299,7 +286,7 @@ export class BinaryTree {
|
|
|
299
286
|
const leftSubTreeRightMost = this.getRightMost(curr.left);
|
|
300
287
|
if (leftSubTreeRightMost) {
|
|
301
288
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
302
|
-
orgCurrent = this.
|
|
289
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
303
290
|
if (parentOfLeftSubTreeMax) {
|
|
304
291
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
305
292
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -332,8 +319,8 @@ export class BinaryTree {
|
|
|
332
319
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
333
320
|
*/
|
|
334
321
|
getDepth(distNode, beginRoot = this.root) {
|
|
335
|
-
distNode = this.
|
|
336
|
-
beginRoot = this.
|
|
322
|
+
distNode = this.ensureNode(distNode);
|
|
323
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
337
324
|
let depth = 0;
|
|
338
325
|
while (distNode?.parent) {
|
|
339
326
|
if (distNode === beginRoot) {
|
|
@@ -346,8 +333,7 @@ export class BinaryTree {
|
|
|
346
333
|
}
|
|
347
334
|
/**
|
|
348
335
|
* Time Complexity: O(n)
|
|
349
|
-
* Space Complexity: O(
|
|
350
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
336
|
+
* Space Complexity: O(1)
|
|
351
337
|
*/
|
|
352
338
|
/**
|
|
353
339
|
* Time Complexity: O(n)
|
|
@@ -363,8 +349,8 @@ export class BinaryTree {
|
|
|
363
349
|
* values:
|
|
364
350
|
* @returns the height of the binary tree.
|
|
365
351
|
*/
|
|
366
|
-
getHeight(beginRoot = this.root, iterationType = this.
|
|
367
|
-
beginRoot = this.
|
|
352
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
353
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
368
354
|
if (!beginRoot)
|
|
369
355
|
return -1;
|
|
370
356
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -409,8 +395,8 @@ export class BinaryTree {
|
|
|
409
395
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
410
396
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
411
397
|
*/
|
|
412
|
-
getMinHeight(beginRoot = this.root, iterationType = this.
|
|
413
|
-
beginRoot = this.
|
|
398
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
399
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
414
400
|
if (!beginRoot)
|
|
415
401
|
return -1;
|
|
416
402
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -456,6 +442,7 @@ export class BinaryTree {
|
|
|
456
442
|
/**
|
|
457
443
|
* Time Complexity: O(n)
|
|
458
444
|
* Space Complexity: O(log n)
|
|
445
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
459
446
|
*/
|
|
460
447
|
/**
|
|
461
448
|
* Time Complexity: O(n)
|
|
@@ -471,10 +458,6 @@ export class BinaryTree {
|
|
|
471
458
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
472
459
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
473
460
|
}
|
|
474
|
-
/**
|
|
475
|
-
* Time Complexity: O(n)
|
|
476
|
-
* Space Complexity: O(log n).
|
|
477
|
-
*/
|
|
478
461
|
/**
|
|
479
462
|
* Time Complexity: O(n)
|
|
480
463
|
* Space Complexity: O(log n).
|
|
@@ -500,10 +483,10 @@ export class BinaryTree {
|
|
|
500
483
|
* traverse the binary tree. It can have two possible values:
|
|
501
484
|
* @returns an array of nodes of type `N`.
|
|
502
485
|
*/
|
|
503
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
486
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
487
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
505
488
|
callback = (node => node);
|
|
506
|
-
beginRoot = this.
|
|
489
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
507
490
|
if (!beginRoot)
|
|
508
491
|
return [];
|
|
509
492
|
const ans = [];
|
|
@@ -538,10 +521,6 @@ export class BinaryTree {
|
|
|
538
521
|
}
|
|
539
522
|
return ans;
|
|
540
523
|
}
|
|
541
|
-
/**
|
|
542
|
-
* Time Complexity: O(n)
|
|
543
|
-
* Space Complexity: O(log n).
|
|
544
|
-
*/
|
|
545
524
|
/**
|
|
546
525
|
* Time Complexity: O(n)
|
|
547
526
|
*
|
|
@@ -562,15 +541,11 @@ export class BinaryTree {
|
|
|
562
541
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
563
542
|
* @returns a boolean value.
|
|
564
543
|
*/
|
|
565
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
544
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
566
545
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
567
546
|
callback = (node => node);
|
|
568
547
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
569
548
|
}
|
|
570
|
-
/**
|
|
571
|
-
* Time Complexity: O(n)
|
|
572
|
-
* Space Complexity: O(log n)
|
|
573
|
-
*/
|
|
574
549
|
/**
|
|
575
550
|
* Time Complexity: O(n)
|
|
576
551
|
* Space Complexity: O(log n)
|
|
@@ -592,7 +567,7 @@ export class BinaryTree {
|
|
|
592
567
|
* nodes are visited during the search.
|
|
593
568
|
* @returns a value of type `N | null | undefined`.
|
|
594
569
|
*/
|
|
595
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
570
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
596
571
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
597
572
|
callback = (node => node);
|
|
598
573
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -645,7 +620,11 @@ export class BinaryTree {
|
|
|
645
620
|
}
|
|
646
621
|
}
|
|
647
622
|
/**
|
|
648
|
-
*
|
|
623
|
+
* Time Complexity: O(n)
|
|
624
|
+
* Space Complexity: O(log n)
|
|
625
|
+
*/
|
|
626
|
+
/**
|
|
627
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a valid node
|
|
649
628
|
* key, otherwise it returns the key itself.
|
|
650
629
|
* @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
|
|
651
630
|
* `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
|
|
@@ -655,13 +634,9 @@ export class BinaryTree {
|
|
|
655
634
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
656
635
|
* itself if it is not a valid node key.
|
|
657
636
|
*/
|
|
658
|
-
|
|
637
|
+
ensureNode(key, iterationType = IterationType.ITERATIVE) {
|
|
659
638
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
660
639
|
}
|
|
661
|
-
/**
|
|
662
|
-
* Time Complexity: O(n)
|
|
663
|
-
* Space Complexity: O(log n)
|
|
664
|
-
*/
|
|
665
640
|
/**
|
|
666
641
|
* Time Complexity: O(n)
|
|
667
642
|
* Space Complexity: O(log n)
|
|
@@ -684,11 +659,15 @@ export class BinaryTree {
|
|
|
684
659
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
685
660
|
* found, `undefined` is returned.
|
|
686
661
|
*/
|
|
687
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
662
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
688
663
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
689
664
|
callback = (node => node);
|
|
690
665
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
691
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Time Complexity: O(n)
|
|
669
|
+
* Space Complexity: O(log n)
|
|
670
|
+
*/
|
|
692
671
|
/**
|
|
693
672
|
* Clear the binary tree, removing all nodes.
|
|
694
673
|
*/
|
|
@@ -703,10 +682,6 @@ export class BinaryTree {
|
|
|
703
682
|
isEmpty() {
|
|
704
683
|
return this.size === 0;
|
|
705
684
|
}
|
|
706
|
-
/**
|
|
707
|
-
* Time Complexity: O(log n)
|
|
708
|
-
* Space Complexity: O(log n)
|
|
709
|
-
*/
|
|
710
685
|
/**
|
|
711
686
|
* Time Complexity: O(log n)
|
|
712
687
|
* Space Complexity: O(log n)
|
|
@@ -724,7 +699,7 @@ export class BinaryTree {
|
|
|
724
699
|
getPathToRoot(beginRoot, isReverse = true) {
|
|
725
700
|
// TODO to support get path through passing key
|
|
726
701
|
const result = [];
|
|
727
|
-
beginRoot = this.
|
|
702
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
728
703
|
if (!beginRoot)
|
|
729
704
|
return result;
|
|
730
705
|
while (beginRoot.parent) {
|
|
@@ -738,7 +713,7 @@ export class BinaryTree {
|
|
|
738
713
|
}
|
|
739
714
|
/**
|
|
740
715
|
* Time Complexity: O(log n)
|
|
741
|
-
* Space Complexity: O(
|
|
716
|
+
* Space Complexity: O(log n)
|
|
742
717
|
*/
|
|
743
718
|
/**
|
|
744
719
|
* Time Complexity: O(log n)
|
|
@@ -754,8 +729,8 @@ export class BinaryTree {
|
|
|
754
729
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
755
730
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
756
731
|
*/
|
|
757
|
-
getLeftMost(beginRoot = this.root, iterationType = this.
|
|
758
|
-
beginRoot = this.
|
|
732
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
733
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
759
734
|
if (!beginRoot)
|
|
760
735
|
return beginRoot;
|
|
761
736
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -795,9 +770,9 @@ export class BinaryTree {
|
|
|
795
770
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
796
771
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
797
772
|
*/
|
|
798
|
-
getRightMost(beginRoot = this.root, iterationType = this.
|
|
773
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
799
774
|
// TODO support get right most by passing key in
|
|
800
|
-
beginRoot = this.
|
|
775
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
801
776
|
if (!beginRoot)
|
|
802
777
|
return beginRoot;
|
|
803
778
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -819,7 +794,7 @@ export class BinaryTree {
|
|
|
819
794
|
}
|
|
820
795
|
}
|
|
821
796
|
/**
|
|
822
|
-
* Time Complexity: O(n)
|
|
797
|
+
* Time Complexity: O(log n)
|
|
823
798
|
* Space Complexity: O(1)
|
|
824
799
|
*/
|
|
825
800
|
/**
|
|
@@ -834,9 +809,9 @@ export class BinaryTree {
|
|
|
834
809
|
* possible values:
|
|
835
810
|
* @returns a boolean value.
|
|
836
811
|
*/
|
|
837
|
-
isSubtreeBST(beginRoot, iterationType = this.
|
|
812
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
838
813
|
// TODO there is a bug
|
|
839
|
-
beginRoot = this.
|
|
814
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
840
815
|
if (!beginRoot)
|
|
841
816
|
return true;
|
|
842
817
|
if (iterationType === IterationType.RECURSIVE) {
|
|
@@ -881,15 +856,11 @@ export class BinaryTree {
|
|
|
881
856
|
* expected to be
|
|
882
857
|
* @returns a boolean value.
|
|
883
858
|
*/
|
|
884
|
-
isBST(iterationType = this.
|
|
859
|
+
isBST(iterationType = this.iterationType) {
|
|
885
860
|
if (this.root === null)
|
|
886
861
|
return true;
|
|
887
862
|
return this.isSubtreeBST(this.root, iterationType);
|
|
888
863
|
}
|
|
889
|
-
/**
|
|
890
|
-
* Time complexity: O(n)
|
|
891
|
-
* Space complexity: O(log n)
|
|
892
|
-
*/
|
|
893
864
|
/**
|
|
894
865
|
* Time complexity: O(n)
|
|
895
866
|
* Space complexity: O(log n)
|
|
@@ -912,8 +883,8 @@ export class BinaryTree {
|
|
|
912
883
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
913
884
|
* by the return type of the `callback` function.
|
|
914
885
|
*/
|
|
915
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
916
|
-
beginRoot = this.
|
|
886
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
887
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
917
888
|
const ans = [];
|
|
918
889
|
if (!beginRoot)
|
|
919
890
|
return ans;
|
|
@@ -952,6 +923,10 @@ export class BinaryTree {
|
|
|
952
923
|
}
|
|
953
924
|
return ans;
|
|
954
925
|
}
|
|
926
|
+
/**
|
|
927
|
+
* Time complexity: O(n)
|
|
928
|
+
* Space complexity: O(log n)
|
|
929
|
+
*/
|
|
955
930
|
/**
|
|
956
931
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
957
932
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -986,10 +961,6 @@ export class BinaryTree {
|
|
|
986
961
|
isNodeKey(potentialKey) {
|
|
987
962
|
return typeof potentialKey === 'number';
|
|
988
963
|
}
|
|
989
|
-
/**
|
|
990
|
-
* Time complexity: O(n)
|
|
991
|
-
* Space complexity: O(n)
|
|
992
|
-
*/
|
|
993
964
|
/**
|
|
994
965
|
* Time complexity: O(n)
|
|
995
966
|
* Space complexity: O(n)
|
|
@@ -1014,7 +985,7 @@ export class BinaryTree {
|
|
|
1014
985
|
* @returns an array of values that are the return values of the callback function.
|
|
1015
986
|
*/
|
|
1016
987
|
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE, includeNull = false) {
|
|
1017
|
-
beginRoot = this.
|
|
988
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1018
989
|
if (!beginRoot)
|
|
1019
990
|
return [];
|
|
1020
991
|
const ans = [];
|
|
@@ -1119,10 +1090,6 @@ export class BinaryTree {
|
|
|
1119
1090
|
}
|
|
1120
1091
|
return ans;
|
|
1121
1092
|
}
|
|
1122
|
-
/**
|
|
1123
|
-
* Time complexity: O(n)
|
|
1124
|
-
* Space complexity: O(n)
|
|
1125
|
-
*/
|
|
1126
1093
|
/**
|
|
1127
1094
|
* Time complexity: O(n)
|
|
1128
1095
|
* Space complexity: O(n)
|
|
@@ -1144,8 +1111,8 @@ export class BinaryTree {
|
|
|
1144
1111
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1145
1112
|
* the breadth-first traversal of a binary tree.
|
|
1146
1113
|
*/
|
|
1147
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
1148
|
-
beginRoot = this.
|
|
1114
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1115
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1149
1116
|
if (!beginRoot)
|
|
1150
1117
|
return [];
|
|
1151
1118
|
const ans = [];
|
|
@@ -1196,10 +1163,6 @@ export class BinaryTree {
|
|
|
1196
1163
|
}
|
|
1197
1164
|
return ans;
|
|
1198
1165
|
}
|
|
1199
|
-
/**
|
|
1200
|
-
* Time complexity: O(n)
|
|
1201
|
-
* Space complexity: O(n)
|
|
1202
|
-
*/
|
|
1203
1166
|
/**
|
|
1204
1167
|
* Time complexity: O(n)
|
|
1205
1168
|
* Space complexity: O(n)
|
|
@@ -1221,8 +1184,8 @@ export class BinaryTree {
|
|
|
1221
1184
|
* be excluded
|
|
1222
1185
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1223
1186
|
*/
|
|
1224
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
1225
|
-
beginRoot = this.
|
|
1187
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1188
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1226
1189
|
const levelsNodes = [];
|
|
1227
1190
|
if (!beginRoot)
|
|
1228
1191
|
return levelsNodes;
|
|
@@ -1277,7 +1240,7 @@ export class BinaryTree {
|
|
|
1277
1240
|
* @returns The function `getPredecessor` returns a value of type `N | undefined`.
|
|
1278
1241
|
*/
|
|
1279
1242
|
getPredecessor(node) {
|
|
1280
|
-
node = this.
|
|
1243
|
+
node = this.ensureNode(node);
|
|
1281
1244
|
if (!this.isRealNode(node))
|
|
1282
1245
|
return undefined;
|
|
1283
1246
|
if (node.left) {
|
|
@@ -1300,7 +1263,7 @@ export class BinaryTree {
|
|
|
1300
1263
|
* after the given node in the inorder traversal of the binary tree.
|
|
1301
1264
|
*/
|
|
1302
1265
|
getSuccessor(x) {
|
|
1303
|
-
x = this.
|
|
1266
|
+
x = this.ensureNode(x);
|
|
1304
1267
|
if (!x)
|
|
1305
1268
|
return undefined;
|
|
1306
1269
|
if (x.right) {
|
|
@@ -1313,10 +1276,6 @@ export class BinaryTree {
|
|
|
1313
1276
|
}
|
|
1314
1277
|
return y;
|
|
1315
1278
|
}
|
|
1316
|
-
/**
|
|
1317
|
-
* Time complexity: O(n)
|
|
1318
|
-
* Space complexity: O(1)
|
|
1319
|
-
*/
|
|
1320
1279
|
/**
|
|
1321
1280
|
* Time complexity: O(n)
|
|
1322
1281
|
* Space complexity: O(1)
|
|
@@ -1336,7 +1295,7 @@ export class BinaryTree {
|
|
|
1336
1295
|
* by the return type of the `callback` function.
|
|
1337
1296
|
*/
|
|
1338
1297
|
morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
|
|
1339
|
-
beginRoot = this.
|
|
1298
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1340
1299
|
if (beginRoot === null)
|
|
1341
1300
|
return [];
|
|
1342
1301
|
const ans = [];
|
|
@@ -1420,6 +1379,10 @@ export class BinaryTree {
|
|
|
1420
1379
|
}
|
|
1421
1380
|
return ans;
|
|
1422
1381
|
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Time complexity: O(n)
|
|
1384
|
+
* Space complexity: O(1)
|
|
1385
|
+
*/
|
|
1423
1386
|
/**
|
|
1424
1387
|
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
1425
1388
|
* entry and the tree as arguments.
|
|
@@ -1443,19 +1406,11 @@ export class BinaryTree {
|
|
|
1443
1406
|
const newTree = this.createTree();
|
|
1444
1407
|
for (const [key, value] of this) {
|
|
1445
1408
|
if (predicate([key, value], this)) {
|
|
1446
|
-
newTree.add(key, value);
|
|
1409
|
+
newTree.add([key, value]);
|
|
1447
1410
|
}
|
|
1448
1411
|
}
|
|
1449
1412
|
return newTree;
|
|
1450
1413
|
}
|
|
1451
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1452
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1453
|
-
// const newTree = this.createTree();
|
|
1454
|
-
// for (const [key, value] of this) {
|
|
1455
|
-
// newTree.add(key, callback([key, value], this));
|
|
1456
|
-
// }
|
|
1457
|
-
// return newTree;
|
|
1458
|
-
// }
|
|
1459
1414
|
/**
|
|
1460
1415
|
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1461
1416
|
* tree.
|
|
@@ -1465,10 +1420,18 @@ export class BinaryTree {
|
|
|
1465
1420
|
map(callback) {
|
|
1466
1421
|
const newTree = this.createTree();
|
|
1467
1422
|
for (const [key, value] of this) {
|
|
1468
|
-
newTree.add(key, callback([key, value], this));
|
|
1423
|
+
newTree.add([key, callback([key, value], this)]);
|
|
1469
1424
|
}
|
|
1470
1425
|
return newTree;
|
|
1471
1426
|
}
|
|
1427
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1428
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1429
|
+
// const newTree = this.createTree();
|
|
1430
|
+
// for (const [key, value] of this) {
|
|
1431
|
+
// newTree.add(key, callback([key, value], this));
|
|
1432
|
+
// }
|
|
1433
|
+
// return newTree;
|
|
1434
|
+
// }
|
|
1472
1435
|
/**
|
|
1473
1436
|
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1474
1437
|
* entry, accumulating a single value.
|
|
@@ -1500,7 +1463,7 @@ export class BinaryTree {
|
|
|
1500
1463
|
*[Symbol.iterator](node = this.root) {
|
|
1501
1464
|
if (!node)
|
|
1502
1465
|
return;
|
|
1503
|
-
if (this.
|
|
1466
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1504
1467
|
const stack = [];
|
|
1505
1468
|
let current = node;
|
|
1506
1469
|
while (current || stack.length > 0) {
|
|
@@ -1534,7 +1497,7 @@ export class BinaryTree {
|
|
|
1534
1497
|
*/
|
|
1535
1498
|
print(beginRoot = this.root, options) {
|
|
1536
1499
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1537
|
-
beginRoot = this.
|
|
1500
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
1538
1501
|
if (!beginRoot)
|
|
1539
1502
|
return;
|
|
1540
1503
|
if (opts.isShowUndefined)
|
|
@@ -1604,9 +1567,9 @@ export class BinaryTree {
|
|
|
1604
1567
|
* @param {N} destNode - The destination node to swap.
|
|
1605
1568
|
* @returns {N} - The destination node after the swap.
|
|
1606
1569
|
*/
|
|
1607
|
-
|
|
1608
|
-
srcNode = this.
|
|
1609
|
-
destNode = this.
|
|
1570
|
+
_swapProperties(srcNode, destNode) {
|
|
1571
|
+
srcNode = this.ensureNode(srcNode);
|
|
1572
|
+
destNode = this.ensureNode(destNode);
|
|
1610
1573
|
if (srcNode && destNode) {
|
|
1611
1574
|
const { key, value } = destNode;
|
|
1612
1575
|
const tempNode = this.createNode(key, value);
|
|
@@ -1620,6 +1583,31 @@ export class BinaryTree {
|
|
|
1620
1583
|
}
|
|
1621
1584
|
return undefined;
|
|
1622
1585
|
}
|
|
1586
|
+
/**
|
|
1587
|
+
* The function replaces an old node with a new node in a binary tree.
|
|
1588
|
+
* @param {N} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
1589
|
+
* tree.
|
|
1590
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
1591
|
+
* tree.
|
|
1592
|
+
* @returns The method is returning the newNode.
|
|
1593
|
+
*/
|
|
1594
|
+
_replaceNode(oldNode, newNode) {
|
|
1595
|
+
if (oldNode.parent) {
|
|
1596
|
+
if (oldNode.parent.left === oldNode) {
|
|
1597
|
+
oldNode.parent.left = newNode;
|
|
1598
|
+
}
|
|
1599
|
+
else if (oldNode.parent.right === oldNode) {
|
|
1600
|
+
oldNode.parent.right = newNode;
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
newNode.left = oldNode.left;
|
|
1604
|
+
newNode.right = oldNode.right;
|
|
1605
|
+
newNode.parent = oldNode.parent;
|
|
1606
|
+
if (this.root === oldNode) {
|
|
1607
|
+
this._root = newNode;
|
|
1608
|
+
}
|
|
1609
|
+
return newNode;
|
|
1610
|
+
}
|
|
1623
1611
|
/**
|
|
1624
1612
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1625
1613
|
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|