data-structure-typed 1.18.6 → 1.18.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -366
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +72 -51
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +226 -158
- package/dist/data-structures/binary-tree/avl-tree.d.ts +7 -6
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +16 -6
- package/dist/data-structures/binary-tree/binary-tree.js +16 -7
- package/dist/data-structures/binary-tree/bst.d.ts +37 -28
- package/dist/data-structures/binary-tree/bst.js +44 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +30 -1
- package/dist/data-structures/binary-tree/rb-tree.js +27 -14
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +18 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -15
- package/dist/data-structures/graph/abstract-graph.d.ts +28 -27
- package/dist/data-structures/graph/abstract-graph.js +58 -36
- package/dist/data-structures/graph/directed-graph.d.ts +27 -24
- package/dist/data-structures/graph/directed-graph.js +65 -60
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -18
- package/dist/data-structures/graph/undirected-graph.js +52 -65
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +100 -0
- package/dist/data-structures/interfaces/abstract-binary-tree.js +2 -0
- package/dist/data-structures/interfaces/abstract-graph.d.ts +3 -8
- package/dist/data-structures/interfaces/avl-tree.d.ts +16 -1
- package/dist/data-structures/interfaces/binary-tree.d.ts +4 -24
- package/dist/data-structures/interfaces/bst.d.ts +18 -1
- package/dist/data-structures/interfaces/directed-graph.d.ts +4 -1
- package/dist/data-structures/interfaces/index.d.ts +1 -1
- package/dist/data-structures/interfaces/index.js +1 -1
- package/dist/data-structures/interfaces/rb-tree.d.ts +9 -0
- package/dist/data-structures/interfaces/rb-tree.js +2 -0
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
- package/dist/data-structures/interfaces/undirected-graph.d.ts +4 -1
- package/dist/data-structures/tree/tree.d.ts +1 -4
- package/dist/data-structures/tree/tree.js +1 -12
- package/dist/data-structures/types/abstract-binary-tree.d.ts +11 -7
- package/dist/data-structures/types/abstract-binary-tree.js +9 -5
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/bst.d.ts +1 -1
- package/dist/data-structures/types/bst.js +1 -1
- package/dist/data-structures/types/directed-graph.js +1 -1
- package/dist/data-structures/types/helpers.d.ts +1 -8
- package/dist/data-structures/types/rb-tree.d.ts +2 -0
- package/dist/data-structures/types/rb-tree.js +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +4 -4
- package/dist/utils/types/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts +6 -1
- package/dist/utils/utils.js +83 -1
- package/package.json +2 -2
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
- package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
- package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
- package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
- package/backup/recursive-type/src/data-structures/index.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
- package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
- package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
- package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
- package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
- package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
- package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
- package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/index.ts +0 -1
- package/backup/recursive-type/src/utils/index.ts +0 -2
- package/backup/recursive-type/src/utils/types/index.ts +0 -1
- package/backup/recursive-type/src/utils/types/utils.ts +0 -6
- package/backup/recursive-type/src/utils/utils.ts +0 -78
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2339
- package/docs/classes/AVLTreeNode.html +0 -450
- package/docs/classes/AaTree.html +0 -166
- package/docs/classes/AbstractBinaryTree.html +0 -2023
- package/docs/classes/AbstractBinaryTreeNode.html +0 -491
- package/docs/classes/AbstractEdge.html +0 -289
- package/docs/classes/AbstractGraph.html +0 -1046
- package/docs/classes/AbstractVertex.html +0 -252
- package/docs/classes/ArrayDeque.html +0 -433
- package/docs/classes/BST.html +0 -2196
- package/docs/classes/BSTNode.html +0 -453
- package/docs/classes/BTree.html +0 -166
- package/docs/classes/BinaryIndexedTree.html +0 -335
- package/docs/classes/BinaryTree.html +0 -2037
- package/docs/classes/BinaryTreeNode.html +0 -452
- package/docs/classes/Character.html +0 -214
- package/docs/classes/CoordinateMap.html +0 -477
- package/docs/classes/CoordinateSet.html +0 -438
- package/docs/classes/Deque.html +0 -969
- package/docs/classes/DirectedEdge.html +0 -360
- package/docs/classes/DirectedGraph.html +0 -1408
- package/docs/classes/DirectedVertex.html +0 -248
- package/docs/classes/DoublyLinkedList.html +0 -962
- package/docs/classes/DoublyLinkedListNode.html +0 -291
- package/docs/classes/HashTable.html +0 -166
- package/docs/classes/Heap.html +0 -417
- package/docs/classes/HeapItem.html +0 -249
- package/docs/classes/Matrix2D.html +0 -496
- package/docs/classes/MatrixNTI2D.html +0 -234
- package/docs/classes/MaxHeap.html +0 -430
- package/docs/classes/MaxPriorityQueue.html +0 -830
- package/docs/classes/MinHeap.html +0 -431
- package/docs/classes/MinPriorityQueue.html +0 -832
- package/docs/classes/Navigator.html +0 -307
- package/docs/classes/ObjectDeque.html +0 -449
- package/docs/classes/Pair.html +0 -166
- package/docs/classes/PriorityQueue.html +0 -754
- package/docs/classes/Queue.html +0 -386
- package/docs/classes/SegmentTree.html +0 -428
- package/docs/classes/SegmentTreeNode.html +0 -351
- package/docs/classes/SinglyLinkedList.html +0 -782
- package/docs/classes/SinglyLinkedListNode.html +0 -264
- package/docs/classes/SkipLinkedList.html +0 -166
- package/docs/classes/SplayTree.html +0 -166
- package/docs/classes/Stack.html +0 -362
- package/docs/classes/TreeMap.html +0 -166
- package/docs/classes/TreeMultiSet.html +0 -2196
- package/docs/classes/TreeMultiSetNode.html +0 -450
- package/docs/classes/TreeNode.html +0 -337
- package/docs/classes/TreeSet.html +0 -166
- package/docs/classes/Trie.html +0 -366
- package/docs/classes/TrieNode.html +0 -274
- package/docs/classes/TwoThreeTree.html +0 -166
- package/docs/classes/UndirectedEdge.html +0 -331
- package/docs/classes/UndirectedGraph.html +0 -1196
- package/docs/classes/UndirectedVertex.html +0 -248
- package/docs/classes/Vector2D.html +0 -799
- package/docs/enums/CP.html +0 -175
- package/docs/enums/FamilyPosition.html +0 -175
- package/docs/enums/LoopType.html +0 -176
- package/docs/enums/RBColor.html +0 -168
- package/docs/enums/TopologicalProperty.html +0 -175
- package/docs/index.html +0 -639
- package/docs/interfaces/IBinaryTree.html +0 -189
- package/docs/interfaces/IBinaryTreeNode.html +0 -396
- package/docs/interfaces/IDirectedGraph.html +0 -259
- package/docs/interfaces/IGraph.html +0 -471
- package/docs/interfaces/IUNDirectedGraph.html +0 -151
- package/docs/modules.html +0 -250
- package/docs/types/AVLTreeOptions.html +0 -144
- package/docs/types/AbstractBinaryTreeOptions.html +0 -150
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -146
- package/docs/types/AbstractResultByProperty.html +0 -146
- package/docs/types/AbstractResultsByProperty.html +0 -146
- package/docs/types/BSTComparator.html +0 -156
- package/docs/types/BSTOptions.html +0 -146
- package/docs/types/BinaryTreeDeletedResult.html +0 -153
- package/docs/types/BinaryTreeNodeId.html +0 -141
- package/docs/types/BinaryTreeNodePropertyName.html +0 -141
- package/docs/types/BinaryTreeOptions.html +0 -144
- package/docs/types/DFSOrderPattern.html +0 -141
- package/docs/types/DijkstraResult.html +0 -161
- package/docs/types/Direction.html +0 -141
- package/docs/types/EdgeId.html +0 -141
- package/docs/types/HeapOptions.html +0 -162
- package/docs/types/IdObject.html +0 -151
- package/docs/types/KeyValObject.html +0 -146
- package/docs/types/NavigatorParams.html +0 -175
- package/docs/types/NodeOrPropertyName.html +0 -141
- package/docs/types/PriorityQueueComparator.html +0 -161
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -141
- package/docs/types/PriorityQueueOptions.html +0 -155
- package/docs/types/RBTreeOptions.html +0 -144
- package/docs/types/RecursiveAVLTreeNode.html +0 -146
- package/docs/types/RecursiveBSTNode.html +0 -146
- package/docs/types/RecursiveBinaryTreeNode.html +0 -146
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -146
- package/docs/types/SegmentTreeNodeVal.html +0 -141
- package/docs/types/TopologicalStatus.html +0 -141
- package/docs/types/TreeMultiSetOptions.html +0 -146
- package/docs/types/Turning.html +0 -141
- package/docs/types/VertexId.html +0 -141
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
|
@@ -38,8 +38,16 @@ exports.AbstractBinaryTree = exports.AbstractBinaryTreeNode = void 0;
|
|
|
38
38
|
var utils_1 = require("../../utils");
|
|
39
39
|
var types_1 = require("../types");
|
|
40
40
|
var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
41
|
+
/**
|
|
42
|
+
* The constructor function initializes a BinaryTreeNode object with an id, value, and count.
|
|
43
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
|
|
44
|
+
* for the binary tree node.
|
|
45
|
+
* @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary
|
|
46
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
47
|
+
* @param {number} [count] - The `count` parameter is an optional parameter that represents the number of times the
|
|
48
|
+
* value `val` appears in the binary tree node. If the `count` parameter is not provided, it defaults to 1.
|
|
49
|
+
*/
|
|
41
50
|
function AbstractBinaryTreeNode(id, val, count) {
|
|
42
|
-
this._familyPosition = types_1.FamilyPosition.ROOT;
|
|
43
51
|
this._count = 1;
|
|
44
52
|
this._height = 0;
|
|
45
53
|
this._id = id;
|
|
@@ -60,8 +68,8 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
60
68
|
get: function () {
|
|
61
69
|
return this._val;
|
|
62
70
|
},
|
|
63
|
-
set: function (
|
|
64
|
-
this._val =
|
|
71
|
+
set: function (value) {
|
|
72
|
+
this._val = value;
|
|
65
73
|
},
|
|
66
74
|
enumerable: false,
|
|
67
75
|
configurable: true
|
|
@@ -73,7 +81,6 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
73
81
|
set: function (v) {
|
|
74
82
|
if (v) {
|
|
75
83
|
v.parent = this;
|
|
76
|
-
v.familyPosition = types_1.FamilyPosition.LEFT;
|
|
77
84
|
}
|
|
78
85
|
this._left = v;
|
|
79
86
|
},
|
|
@@ -87,7 +94,6 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
87
94
|
set: function (v) {
|
|
88
95
|
if (v) {
|
|
89
96
|
v.parent = this;
|
|
90
|
-
v.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
91
97
|
}
|
|
92
98
|
this._right = v;
|
|
93
99
|
},
|
|
@@ -104,16 +110,6 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
104
110
|
enumerable: false,
|
|
105
111
|
configurable: true
|
|
106
112
|
});
|
|
107
|
-
Object.defineProperty(AbstractBinaryTreeNode.prototype, "familyPosition", {
|
|
108
|
-
get: function () {
|
|
109
|
-
return this._familyPosition;
|
|
110
|
-
},
|
|
111
|
-
set: function (v) {
|
|
112
|
-
this._familyPosition = v;
|
|
113
|
-
},
|
|
114
|
-
enumerable: false,
|
|
115
|
-
configurable: true
|
|
116
|
-
});
|
|
117
113
|
Object.defineProperty(AbstractBinaryTreeNode.prototype, "count", {
|
|
118
114
|
get: function () {
|
|
119
115
|
return this._count;
|
|
@@ -134,26 +130,71 @@ var AbstractBinaryTreeNode = /** @class */ (function () {
|
|
|
134
130
|
enumerable: false,
|
|
135
131
|
configurable: true
|
|
136
132
|
});
|
|
137
|
-
AbstractBinaryTreeNode.prototype
|
|
138
|
-
|
|
139
|
-
|
|
133
|
+
Object.defineProperty(AbstractBinaryTreeNode.prototype, "familyPosition", {
|
|
134
|
+
get: function () {
|
|
135
|
+
var that = this;
|
|
136
|
+
if (that.parent) {
|
|
137
|
+
if (that.parent.left === that) {
|
|
138
|
+
if (that.left || that.right) {
|
|
139
|
+
return types_1.FamilyPosition.ROOT_LEFT;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return types_1.FamilyPosition.LEFT;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (that.parent.right === that) {
|
|
146
|
+
if (that.left || that.right) {
|
|
147
|
+
return types_1.FamilyPosition.ROOT_RIGHT;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
return types_1.FamilyPosition.RIGHT;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return types_1.FamilyPosition.MAL_NODE;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
if (that.left || that.right) {
|
|
159
|
+
return types_1.FamilyPosition.ROOT;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return types_1.FamilyPosition.ISOLATED;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
enumerable: false,
|
|
167
|
+
configurable: true
|
|
168
|
+
});
|
|
169
|
+
/**
|
|
170
|
+
* The function swaps the location of two nodes in a binary tree.
|
|
171
|
+
* @param {FAMILY} destNode - The `swapNode` parameter is of type `FAMILY`, which represents a node in a family tree.
|
|
172
|
+
* @returns the `swapNode` object after swapping its properties with the properties of `this` object.
|
|
173
|
+
*/
|
|
174
|
+
AbstractBinaryTreeNode.prototype.swapLocation = function (destNode) {
|
|
175
|
+
var val = destNode.val, count = destNode.count, height = destNode.height, id = destNode.id;
|
|
176
|
+
var tempNode = this.createNode(id, val, count);
|
|
177
|
+
tempNode.height = height;
|
|
140
178
|
if (tempNode instanceof AbstractBinaryTreeNode) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
swapNode.count = this.count;
|
|
147
|
-
swapNode.height = this.height;
|
|
179
|
+
// TODO should we consider the left, right children?
|
|
180
|
+
destNode.id = this.id;
|
|
181
|
+
destNode.val = this.val;
|
|
182
|
+
destNode.count = this.count;
|
|
183
|
+
destNode.height = this.height;
|
|
148
184
|
this.id = tempNode.id;
|
|
149
185
|
this.val = tempNode.val;
|
|
150
186
|
this.count = tempNode.count;
|
|
151
187
|
this.height = tempNode.height;
|
|
152
188
|
}
|
|
153
|
-
return
|
|
189
|
+
return destNode;
|
|
154
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* The `clone` function returns a new instance of the `FAMILY` class with the same `id`, `val`, and `count` properties.
|
|
193
|
+
* @returns The `clone()` method is returning a new instance of the `FAMILY` class with the same `id`, `val`, and
|
|
194
|
+
* `count` values as the current instance.
|
|
195
|
+
*/
|
|
155
196
|
AbstractBinaryTreeNode.prototype.clone = function () {
|
|
156
|
-
return this.
|
|
197
|
+
return this.createNode(this.id, this.val, this.count);
|
|
157
198
|
};
|
|
158
199
|
return AbstractBinaryTreeNode;
|
|
159
200
|
}());
|
|
@@ -173,13 +214,13 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
173
214
|
this._visitedLeftSum = [];
|
|
174
215
|
this._autoIncrementId = false;
|
|
175
216
|
this._maxId = -1;
|
|
176
|
-
this.
|
|
217
|
+
this._isMergeDuplicatedVal = true;
|
|
177
218
|
this._root = null;
|
|
178
219
|
this._size = 0;
|
|
179
220
|
this._count = 0;
|
|
180
221
|
if (options !== undefined) {
|
|
181
|
-
var _a = options.loopType, loopType = _a === void 0 ? types_1.LoopType.ITERATIVE : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.
|
|
182
|
-
this.
|
|
222
|
+
var _a = options.loopType, loopType = _a === void 0 ? types_1.LoopType.ITERATIVE : _a, _b = options.autoIncrementId, autoIncrementId = _b === void 0 ? false : _b, _c = options.isMergeDuplicatedVal, isMergeDuplicatedVal = _c === void 0 ? true : _c;
|
|
223
|
+
this._isMergeDuplicatedVal = isMergeDuplicatedVal;
|
|
183
224
|
this._autoIncrementId = autoIncrementId;
|
|
184
225
|
this._loopType = loopType;
|
|
185
226
|
}
|
|
@@ -240,9 +281,9 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
240
281
|
enumerable: false,
|
|
241
282
|
configurable: true
|
|
242
283
|
});
|
|
243
|
-
Object.defineProperty(AbstractBinaryTree.prototype, "
|
|
284
|
+
Object.defineProperty(AbstractBinaryTree.prototype, "isMergeDuplicatedVal", {
|
|
244
285
|
get: function () {
|
|
245
|
-
return this.
|
|
286
|
+
return this._isMergeDuplicatedVal;
|
|
246
287
|
},
|
|
247
288
|
enumerable: false,
|
|
248
289
|
configurable: true
|
|
@@ -317,7 +358,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
317
358
|
return;
|
|
318
359
|
};
|
|
319
360
|
var inserted;
|
|
320
|
-
var needInsert = val !== null ? this.
|
|
361
|
+
var needInsert = val !== null ? this.createNode(id, val, count) : null;
|
|
321
362
|
var existNode = val !== null ? this.get(id, 'id') : null;
|
|
322
363
|
if (this.root) {
|
|
323
364
|
if (existNode) {
|
|
@@ -333,7 +374,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
333
374
|
}
|
|
334
375
|
}
|
|
335
376
|
else {
|
|
336
|
-
this._setRoot(val !== null ? this.
|
|
377
|
+
this._setRoot(val !== null ? this.createNode(id, val, count) : null);
|
|
337
378
|
if (needInsert !== null) {
|
|
338
379
|
this._setSize(1);
|
|
339
380
|
this._setCount(count);
|
|
@@ -343,12 +384,13 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
343
384
|
return inserted;
|
|
344
385
|
};
|
|
345
386
|
/**
|
|
346
|
-
* The function
|
|
347
|
-
* @param {N | null} newNode - The `newNode` parameter
|
|
348
|
-
*
|
|
349
|
-
* @param parent - The `parent` parameter
|
|
350
|
-
*
|
|
351
|
-
* @returns
|
|
387
|
+
* The function adds a new node to a binary tree as the left or right child of a given parent node.
|
|
388
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
|
|
389
|
+
* either a node object (`N`) or `null`.
|
|
390
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
|
|
391
|
+
* child.
|
|
392
|
+
* @returns either the left or right child node that was added to the parent node. It can also return `null` or
|
|
393
|
+
* `undefined` in certain cases.
|
|
352
394
|
*/
|
|
353
395
|
AbstractBinaryTree.prototype.addTo = function (newNode, parent) {
|
|
354
396
|
var _a, _b;
|
|
@@ -356,7 +398,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
356
398
|
if (parent.left === undefined) {
|
|
357
399
|
if (newNode) {
|
|
358
400
|
newNode.parent = parent;
|
|
359
|
-
newNode.familyPosition = types_1.FamilyPosition.LEFT;
|
|
360
401
|
}
|
|
361
402
|
parent.left = newNode;
|
|
362
403
|
if (newNode !== null) {
|
|
@@ -368,7 +409,6 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
368
409
|
else if (parent.right === undefined) {
|
|
369
410
|
if (newNode) {
|
|
370
411
|
newNode.parent = parent;
|
|
371
|
-
newNode.familyPosition = types_1.FamilyPosition.RIGHT;
|
|
372
412
|
}
|
|
373
413
|
parent.right = newNode;
|
|
374
414
|
if (newNode !== null) {
|
|
@@ -395,13 +435,14 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
395
435
|
AbstractBinaryTree.prototype.addMany = function (data) {
|
|
396
436
|
var e_1, _a, e_2, _b;
|
|
397
437
|
var _c;
|
|
438
|
+
// TODO not sure addMany not be run multi times
|
|
398
439
|
var inserted = [];
|
|
399
440
|
var map = new Map();
|
|
400
|
-
if (
|
|
441
|
+
if (this.isMergeDuplicatedVal) {
|
|
401
442
|
try {
|
|
402
443
|
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
|
|
403
|
-
var
|
|
404
|
-
map.set(
|
|
444
|
+
var nodeOrId = data_1_1.value;
|
|
445
|
+
map.set(nodeOrId, ((_c = map.get(nodeOrId)) !== null && _c !== void 0 ? _c : 0) + 1);
|
|
405
446
|
}
|
|
406
447
|
}
|
|
407
448
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -414,53 +455,49 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
414
455
|
}
|
|
415
456
|
try {
|
|
416
457
|
for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
|
|
417
|
-
var
|
|
458
|
+
var nodeOrId = data_2_1.value;
|
|
459
|
+
if (nodeOrId instanceof AbstractBinaryTreeNode) {
|
|
460
|
+
inserted.push(this.add(nodeOrId.id, nodeOrId.val, nodeOrId.count));
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (nodeOrId === null) {
|
|
464
|
+
inserted.push(this.add(NaN, null, 0));
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
418
467
|
// TODO will this cause an issue?
|
|
419
|
-
var count = this.
|
|
420
|
-
|
|
421
|
-
|
|
468
|
+
var count = this.isMergeDuplicatedVal ? map.get(nodeOrId) : 1;
|
|
469
|
+
var newId = void 0;
|
|
470
|
+
if (typeof nodeOrId === 'number') {
|
|
471
|
+
newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
|
|
422
472
|
}
|
|
423
|
-
else if (
|
|
424
|
-
if (
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
473
|
+
else if (nodeOrId instanceof Object) {
|
|
474
|
+
if (this.autoIncrementId) {
|
|
475
|
+
newId = this.maxId + 1;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
if (Object.keys(nodeOrId).includes('id')) {
|
|
479
|
+
newId = nodeOrId.id;
|
|
430
480
|
}
|
|
431
481
|
else {
|
|
432
|
-
|
|
482
|
+
console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
|
|
483
|
+
continue;
|
|
433
484
|
}
|
|
434
485
|
}
|
|
435
486
|
}
|
|
436
|
-
else
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
else {
|
|
445
|
-
console.warn('Object value must has an id property when the autoIncrementId is false');
|
|
446
|
-
break;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
else {
|
|
450
|
-
newId = this.maxId + 1;
|
|
451
|
-
this._setMaxId(newId);
|
|
452
|
-
}
|
|
453
|
-
inserted.push(this.add(newId, item, count));
|
|
454
|
-
map.delete(item);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
inserted.push(this.add(++this._maxId, item, 1));
|
|
487
|
+
else {
|
|
488
|
+
console.warn(nodeOrId, " is not added");
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (this.isMergeDuplicatedVal) {
|
|
492
|
+
if (map.has(nodeOrId)) {
|
|
493
|
+
inserted.push(this.add(newId, nodeOrId, count));
|
|
494
|
+
map.delete(nodeOrId);
|
|
459
495
|
}
|
|
460
496
|
}
|
|
461
|
-
else
|
|
462
|
-
inserted.push(this.add(
|
|
497
|
+
else {
|
|
498
|
+
inserted.push(this.add(newId, nodeOrId, 1));
|
|
463
499
|
}
|
|
500
|
+
this._setMaxId(newId);
|
|
464
501
|
}
|
|
465
502
|
}
|
|
466
503
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
@@ -484,62 +521,76 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
484
521
|
return data.length === this.addMany(data).length;
|
|
485
522
|
};
|
|
486
523
|
/**
|
|
487
|
-
* The function removes a node from a binary tree and returns
|
|
488
|
-
*
|
|
489
|
-
*
|
|
524
|
+
* The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
525
|
+
* node that needs to be balanced.
|
|
526
|
+
* @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
|
|
490
527
|
* @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
|
|
491
528
|
* whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
|
|
492
|
-
* not be
|
|
493
|
-
* @returns
|
|
494
|
-
* "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
|
|
495
|
-
* "needBalanced" property is always null.
|
|
529
|
+
* not be taken into account when removing it. If `ignoreCount` is set to `false
|
|
530
|
+
* @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
496
531
|
*/
|
|
497
|
-
AbstractBinaryTree.prototype.remove = function (
|
|
498
|
-
var
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
532
|
+
AbstractBinaryTree.prototype.remove = function (nodeOrId, ignoreCount) {
|
|
533
|
+
var bstDeletedResult = [];
|
|
534
|
+
if (!this.root)
|
|
535
|
+
return bstDeletedResult;
|
|
536
|
+
var curr = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
|
|
537
|
+
if (!curr)
|
|
538
|
+
return bstDeletedResult;
|
|
539
|
+
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
540
|
+
var needBalanced = null, orgCurrent = curr;
|
|
541
|
+
if (curr.count > 1 && !ignoreCount) {
|
|
542
|
+
curr.count--;
|
|
504
543
|
this._setCount(this.count - 1);
|
|
505
544
|
}
|
|
506
|
-
else
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
this._setSize(this.size - subSize);
|
|
517
|
-
this._setCount(this.count - subCount);
|
|
518
|
-
node.parent.left = null;
|
|
545
|
+
else {
|
|
546
|
+
if (!curr.left) {
|
|
547
|
+
if (!parent) {
|
|
548
|
+
if (curr.right !== undefined)
|
|
549
|
+
this._setRoot(curr.right);
|
|
550
|
+
}
|
|
551
|
+
else {
|
|
552
|
+
var fp = curr.familyPosition;
|
|
553
|
+
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
|
|
554
|
+
parent.left = curr.right;
|
|
519
555
|
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
if (node.parent) {
|
|
523
|
-
this._setSize(this.size - subSize);
|
|
524
|
-
this._setCount(this.count - subCount);
|
|
525
|
-
node.parent.right = null;
|
|
556
|
+
else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) {
|
|
557
|
+
parent.right = curr.right;
|
|
526
558
|
}
|
|
527
|
-
|
|
559
|
+
needBalanced = parent;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
else {
|
|
563
|
+
var leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
564
|
+
if (leftSubTreeRightMost) {
|
|
565
|
+
var parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
566
|
+
orgCurrent = curr.swapLocation(leftSubTreeRightMost);
|
|
567
|
+
if (parentOfLeftSubTreeMax) {
|
|
568
|
+
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
569
|
+
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
570
|
+
else
|
|
571
|
+
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
572
|
+
needBalanced = parentOfLeftSubTreeMax;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
528
575
|
}
|
|
576
|
+
this._setSize(this.size - 1);
|
|
577
|
+
this._setCount(this.count - orgCurrent.count);
|
|
529
578
|
}
|
|
530
|
-
|
|
579
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
580
|
+
return bstDeletedResult;
|
|
531
581
|
};
|
|
532
582
|
/**
|
|
533
|
-
* The function calculates the depth of a
|
|
534
|
-
* @param
|
|
535
|
-
*
|
|
536
|
-
* @returns The depth of the given binary tree node.
|
|
583
|
+
* The function calculates the depth of a node in a binary tree.
|
|
584
|
+
* @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
|
|
585
|
+
* @returns the depth of the given node or binary tree.
|
|
537
586
|
*/
|
|
538
|
-
AbstractBinaryTree.prototype.getDepth = function (
|
|
587
|
+
AbstractBinaryTree.prototype.getDepth = function (beginRoot) {
|
|
588
|
+
if (typeof beginRoot === 'number')
|
|
589
|
+
beginRoot = this.get(beginRoot, 'id');
|
|
539
590
|
var depth = 0;
|
|
540
|
-
while (
|
|
591
|
+
while (beginRoot === null || beginRoot === void 0 ? void 0 : beginRoot.parent) {
|
|
541
592
|
depth++;
|
|
542
|
-
|
|
593
|
+
beginRoot = beginRoot.parent;
|
|
543
594
|
}
|
|
544
595
|
return depth;
|
|
545
596
|
};
|
|
@@ -554,6 +605,8 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
554
605
|
AbstractBinaryTree.prototype.getHeight = function (beginRoot) {
|
|
555
606
|
var _a, _b, _c;
|
|
556
607
|
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
608
|
+
if (typeof beginRoot === 'number')
|
|
609
|
+
beginRoot = this.get(beginRoot, 'id');
|
|
557
610
|
if (!beginRoot)
|
|
558
611
|
return -1;
|
|
559
612
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
@@ -742,24 +795,28 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
742
795
|
return result;
|
|
743
796
|
};
|
|
744
797
|
/**
|
|
745
|
-
* The `getLeftMost` function returns the leftmost node in a binary tree,
|
|
746
|
-
*
|
|
747
|
-
* @param {N | null} [
|
|
748
|
-
*
|
|
749
|
-
*
|
|
750
|
-
* @returns The `getLeftMost`
|
|
798
|
+
* The `getLeftMost` function returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
799
|
+
* no node is specified.
|
|
800
|
+
* @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
801
|
+
* generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
|
|
802
|
+
* node), or `null`.
|
|
803
|
+
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
804
|
+
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the
|
|
805
|
+
* traversal from the root of the binary tree. If there are no nodes in the binary tree, it returns `null`.
|
|
751
806
|
*/
|
|
752
|
-
AbstractBinaryTree.prototype.getLeftMost = function (
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
807
|
+
AbstractBinaryTree.prototype.getLeftMost = function (beginRoot) {
|
|
808
|
+
if (typeof beginRoot === 'number')
|
|
809
|
+
beginRoot = this.get(beginRoot, 'id');
|
|
810
|
+
beginRoot = beginRoot !== null && beginRoot !== void 0 ? beginRoot : this.root;
|
|
811
|
+
if (!beginRoot)
|
|
812
|
+
return beginRoot;
|
|
756
813
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
757
814
|
var _traverse_2 = function (cur) {
|
|
758
815
|
if (!cur.left)
|
|
759
816
|
return cur;
|
|
760
817
|
return _traverse_2(cur.left);
|
|
761
818
|
};
|
|
762
|
-
return _traverse_2(
|
|
819
|
+
return _traverse_2(beginRoot);
|
|
763
820
|
}
|
|
764
821
|
else {
|
|
765
822
|
// Indirect implementation of iteration using tail recursion optimization
|
|
@@ -768,7 +825,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
768
825
|
return cur;
|
|
769
826
|
return _traverse_3.cont(cur.left);
|
|
770
827
|
});
|
|
771
|
-
return _traverse_3(
|
|
828
|
+
return _traverse_3(beginRoot);
|
|
772
829
|
}
|
|
773
830
|
};
|
|
774
831
|
/**
|
|
@@ -802,15 +859,12 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
802
859
|
}
|
|
803
860
|
};
|
|
804
861
|
/**
|
|
805
|
-
* The `
|
|
806
|
-
* @param {N | null}
|
|
807
|
-
*
|
|
808
|
-
* is provided, the function will default to using the root node of the BST instance that
|
|
809
|
-
* @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
|
|
810
|
-
* tree, and `false` otherwise.
|
|
862
|
+
* The function `isBSTByRooted` checks if a binary tree is a binary search tree (BST) by rooted traversal.
|
|
863
|
+
* @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
|
|
864
|
+
* @returns a boolean value.
|
|
811
865
|
*/
|
|
812
|
-
AbstractBinaryTree.prototype.
|
|
813
|
-
|
|
866
|
+
AbstractBinaryTree.prototype.isBSTByRooted = function (node) {
|
|
867
|
+
// TODO there is a bug
|
|
814
868
|
if (!node)
|
|
815
869
|
return true;
|
|
816
870
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
@@ -840,6 +894,15 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
840
894
|
return true;
|
|
841
895
|
}
|
|
842
896
|
};
|
|
897
|
+
/**
|
|
898
|
+
* The function checks if a binary tree is a binary search tree.
|
|
899
|
+
* @param {N | null} [node] - The `node` parameter is of type `N` or `null`. It represents the root node of a binary
|
|
900
|
+
* search tree (BST).
|
|
901
|
+
* @returns a boolean value.
|
|
902
|
+
*/
|
|
903
|
+
AbstractBinaryTree.prototype.isBST = function (node) {
|
|
904
|
+
return this.isBSTByRooted(this.root);
|
|
905
|
+
};
|
|
843
906
|
/**
|
|
844
907
|
* The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
|
|
845
908
|
* traversal.
|
|
@@ -887,6 +950,8 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
887
950
|
*/
|
|
888
951
|
AbstractBinaryTree.prototype.subTreeSum = function (subTreeRoot, propertyName) {
|
|
889
952
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
953
|
+
if (typeof subTreeRoot === 'number')
|
|
954
|
+
subTreeRoot = this.get(subTreeRoot, 'id');
|
|
890
955
|
if (!subTreeRoot)
|
|
891
956
|
return 0;
|
|
892
957
|
var sum = 0;
|
|
@@ -928,17 +993,21 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
928
993
|
return sum;
|
|
929
994
|
};
|
|
930
995
|
/**
|
|
931
|
-
* The function `subTreeAdd` adds a
|
|
932
|
-
* @param subTreeRoot - The `subTreeRoot` parameter
|
|
996
|
+
* The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
|
|
997
|
+
* @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
998
|
+
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree root.
|
|
933
999
|
* @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
|
|
934
|
-
* each node in the subtree should be
|
|
1000
|
+
* each node in the subtree should be incremented or decremented.
|
|
935
1001
|
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
936
|
-
* specifies the property of the
|
|
937
|
-
*
|
|
1002
|
+
* specifies the property of the binary tree node that should be modified. It can be either 'id' or 'count'. If no
|
|
1003
|
+
* value is provided for `propertyName`, it defaults to 'id'.
|
|
1004
|
+
* @returns a boolean value.
|
|
938
1005
|
*/
|
|
939
1006
|
AbstractBinaryTree.prototype.subTreeAdd = function (subTreeRoot, delta, propertyName) {
|
|
940
1007
|
var _this = this;
|
|
941
1008
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
1009
|
+
if (typeof subTreeRoot === 'number')
|
|
1010
|
+
subTreeRoot = this.get(subTreeRoot, 'id');
|
|
942
1011
|
if (!subTreeRoot)
|
|
943
1012
|
return false;
|
|
944
1013
|
var _addByProperty = function (cur) {
|
|
@@ -981,7 +1050,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
981
1050
|
* represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
|
|
982
1051
|
* performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
|
|
983
1052
|
* performed starting from the root node
|
|
984
|
-
* @returns an object of type `
|
|
1053
|
+
* @returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
985
1054
|
*/
|
|
986
1055
|
AbstractBinaryTree.prototype.BFS = function (nodeOrPropertyName) {
|
|
987
1056
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|
|
@@ -1009,7 +1078,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1009
1078
|
* either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
|
|
1010
1079
|
* `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
|
|
1011
1080
|
* no value
|
|
1012
|
-
* @returns an object of type `
|
|
1081
|
+
* @returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1013
1082
|
*/
|
|
1014
1083
|
AbstractBinaryTree.prototype.DFS = function (pattern, nodeOrPropertyName) {
|
|
1015
1084
|
var _this = this;
|
|
@@ -1103,7 +1172,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1103
1172
|
* can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
|
|
1104
1173
|
* will accumulate results based on that property. If no property name is provided, the function will default to
|
|
1105
1174
|
* accumulating results
|
|
1106
|
-
* @returns The function `levelIterative` returns an object of type `
|
|
1175
|
+
* @returns The function `levelIterative` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1107
1176
|
*/
|
|
1108
1177
|
AbstractBinaryTree.prototype.levelIterative = function (node, nodeOrPropertyName) {
|
|
1109
1178
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
@@ -1133,7 +1202,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1133
1202
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1134
1203
|
* specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
|
|
1135
1204
|
* values:
|
|
1136
|
-
* @returns The function `listLevels` returns a 2D array of `
|
|
1205
|
+
* @returns The function `listLevels` returns a 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
|
|
1137
1206
|
*/
|
|
1138
1207
|
AbstractBinaryTree.prototype.listLevels = function (node, nodeOrPropertyName) {
|
|
1139
1208
|
nodeOrPropertyName = nodeOrPropertyName || 'id';
|
|
@@ -1217,7 +1286,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1217
1286
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
|
|
1218
1287
|
* property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
|
|
1219
1288
|
* property. If not provided, it defaults to `'id'`.
|
|
1220
|
-
* @returns The function `morris` returns an object of type `
|
|
1289
|
+
* @returns The function `morris` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
|
|
1221
1290
|
*/
|
|
1222
1291
|
AbstractBinaryTree.prototype.morris = function (pattern, nodeOrPropertyName) {
|
|
1223
1292
|
var _this = this;
|
|
@@ -1331,12 +1400,11 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1331
1400
|
this._maxId = value;
|
|
1332
1401
|
};
|
|
1333
1402
|
AbstractBinaryTree.prototype._setIsDuplicatedVal = function (value) {
|
|
1334
|
-
this.
|
|
1403
|
+
this._isMergeDuplicatedVal = value;
|
|
1335
1404
|
};
|
|
1336
1405
|
AbstractBinaryTree.prototype._setRoot = function (v) {
|
|
1337
1406
|
if (v) {
|
|
1338
|
-
v.parent =
|
|
1339
|
-
v.familyPosition = types_1.FamilyPosition.ROOT;
|
|
1407
|
+
v.parent = undefined;
|
|
1340
1408
|
}
|
|
1341
1409
|
this._root = v;
|
|
1342
1410
|
};
|
|
@@ -1433,7 +1501,7 @@ var AbstractBinaryTree = /** @class */ (function () {
|
|
|
1433
1501
|
* to 'id'.
|
|
1434
1502
|
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1435
1503
|
* can accept a value of type `NodeOrPropertyName`.
|
|
1436
|
-
* @returns The method returns an object of type `
|
|
1504
|
+
* @returns The method returns an object of type `AbstractBinaryTreeNodeProperties<T>`.
|
|
1437
1505
|
*/
|
|
1438
1506
|
AbstractBinaryTree.prototype._getResultByPropertyName = function (nodeOrPropertyName) {
|
|
1439
1507
|
nodeOrPropertyName = nodeOrPropertyName !== null && nodeOrPropertyName !== void 0 ? nodeOrPropertyName : 'id';
|