red-black-tree-typed
Advanced tools
Comparing version 1.51.0 to 1.51.1
@@ -197,3 +197,3 @@ "use strict"; | ||
*/ | ||
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) { | ||
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) { | ||
var _a; | ||
@@ -200,0 +200,0 @@ const deletedResult = []; |
@@ -111,3 +111,3 @@ /** | ||
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will | ||
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single | ||
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single | ||
* parameter of type `NODE | ||
@@ -114,0 +114,0 @@ * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`. |
@@ -136,7 +136,7 @@ "use strict"; | ||
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will | ||
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single | ||
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single | ||
* parameter of type `NODE | ||
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`. | ||
*/ | ||
delete(identifier, callback = this._defaultOneParamCallback) { | ||
delete(identifier, callback = this._DEFAULT_CALLBACK) { | ||
if (identifier instanceof AVLTreeNode) | ||
@@ -143,0 +143,0 @@ callback = (node => node); |
@@ -579,3 +579,3 @@ /** | ||
protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
protected _defaultOneParamCallback: (node: NODE | null | undefined) => K | undefined; | ||
protected _DEFAULT_CALLBACK: (node: NODE | null | undefined) => K | undefined; | ||
/** | ||
@@ -582,0 +582,0 @@ * Swap the data of two nodes in the binary tree. |
@@ -168,15 +168,15 @@ "use strict"; | ||
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') { | ||
let res; | ||
if (this.isRealNode(keyOrNodeOrEntry)) { | ||
res = keyOrNodeOrEntry; | ||
return keyOrNodeOrEntry; | ||
} | ||
else if (this.isEntry(keyOrNodeOrEntry)) { | ||
if (keyOrNodeOrEntry[0]) | ||
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) | ||
return; | ||
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
} | ||
else { | ||
if (keyOrNodeOrEntry) | ||
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) | ||
return; | ||
return this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
} | ||
return res; | ||
} | ||
@@ -365,7 +365,7 @@ /** | ||
getNodeByKey(key, iterationType = 'ITERATIVE') { | ||
// return this.getNodes(key, this._defaultOneParamCallback, true, this.root, iterationType)[0]; | ||
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0]; | ||
if (!this.isRealNode(this.root)) | ||
return undefined; | ||
return; | ||
if (iterationType === 'RECURSIVE') { | ||
const _dfs = (cur) => { | ||
const dfs = (cur) => { | ||
if (cur.key === key) | ||
@@ -375,20 +375,20 @@ return cur; | ||
return; | ||
if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left)) | ||
return _dfs(cur.left); | ||
if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right)) | ||
return _dfs(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') | ||
return dfs(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') | ||
return dfs(cur.right); | ||
}; | ||
return _dfs(this.root); | ||
return dfs(this.root); | ||
} | ||
else { | ||
const queue = new queue_1.Queue([this.root]); | ||
while (queue.size > 0) { | ||
const cur = queue.shift(); | ||
const stack = [this.root]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop(); | ||
if (this.isRealNode(cur)) { | ||
if (this._compare(cur.key, key) === 'EQ') | ||
return cur; | ||
if (this._compare(cur.key, key) === 'GT') | ||
this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, key) === 'LT') | ||
this.isRealNode(cur.right) && queue.push(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') | ||
stack.push(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') | ||
stack.push(cur.right); | ||
} | ||
@@ -426,3 +426,3 @@ } | ||
*/ | ||
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
beginRoot = this.ensureNode(beginRoot); | ||
@@ -433,3 +433,3 @@ if (!beginRoot) | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur) => { | ||
const dfs = (cur) => { | ||
const callbackResult = callback(cur); | ||
@@ -444,14 +444,14 @@ if (callbackResult === identifier) { | ||
// TODO potential bug | ||
if (callback === this._defaultOneParamCallback) { | ||
if (callback === this._DEFAULT_CALLBACK) { | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT') | ||
_traverse(cur.left); | ||
dfs(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT') | ||
_traverse(cur.right); | ||
dfs(cur.right); | ||
} | ||
else { | ||
this.isRealNode(cur.left) && _traverse(cur.left); | ||
this.isRealNode(cur.right) && _traverse(cur.right); | ||
this.isRealNode(cur.left) && dfs(cur.left); | ||
this.isRealNode(cur.right) && dfs(cur.right); | ||
} | ||
}; | ||
_traverse(beginRoot); | ||
dfs(beginRoot); | ||
} | ||
@@ -470,3 +470,3 @@ else { | ||
// TODO potential bug | ||
if (callback === this._defaultOneParamCallback) { | ||
if (callback === this._DEFAULT_CALLBACK) { | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT') | ||
@@ -515,3 +515,3 @@ stack.push(cur.right); | ||
*/ | ||
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') { | ||
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') { | ||
return super.dfs(callback, pattern, beginRoot, iterationType, false); | ||
@@ -540,3 +540,3 @@ } | ||
*/ | ||
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) { | ||
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) { | ||
return super.bfs(callback, beginRoot, iterationType, false); | ||
@@ -566,3 +566,3 @@ } | ||
*/ | ||
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) { | ||
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) { | ||
return super.listLevels(callback, beginRoot, iterationType, false); | ||
@@ -630,3 +630,3 @@ } | ||
*/ | ||
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) { | ||
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) { | ||
targetNode = this.ensureNode(targetNode); | ||
@@ -640,3 +640,3 @@ const ans = []; | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur) => { | ||
const dfs = (cur) => { | ||
const compared = this._compare(cur.key, targetKey); | ||
@@ -646,7 +646,7 @@ if (compared === lesserOrGreater) | ||
if (this.isRealNode(cur.left)) | ||
_traverse(cur.left); | ||
dfs(cur.left); | ||
if (this.isRealNode(cur.right)) | ||
_traverse(cur.right); | ||
dfs(cur.right); | ||
}; | ||
_traverse(this.root); | ||
dfs(this.root); | ||
return ans; | ||
@@ -653,0 +653,0 @@ } |
@@ -190,3 +190,3 @@ import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; | ||
* the binary tree based on its identifier. It is an optional parameter and if not provided, the | ||
* `_defaultOneParamCallback` function is used as the default callback. The callback function should | ||
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should | ||
* return the identifier of the node to | ||
@@ -193,0 +193,0 @@ * @returns an array of BinaryTreeDeleteResult<NODE> objects. |
@@ -193,3 +193,3 @@ "use strict"; | ||
*/ | ||
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) { | ||
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) { | ||
var _a; | ||
@@ -265,7 +265,7 @@ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined; | ||
* the binary tree based on its identifier. It is an optional parameter and if not provided, the | ||
* `_defaultOneParamCallback` function is used as the default callback. The callback function should | ||
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should | ||
* return the identifier of the node to | ||
* @returns an array of BinaryTreeDeleteResult<NODE> objects. | ||
*/ | ||
delete(identifier, callback = this._defaultOneParamCallback) { | ||
delete(identifier, callback = this._DEFAULT_CALLBACK) { | ||
if (identifier === null) | ||
@@ -272,0 +272,0 @@ return []; |
@@ -148,3 +148,3 @@ /** | ||
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the | ||
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is | ||
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is | ||
* used | ||
@@ -151,0 +151,0 @@ * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target |
@@ -201,3 +201,3 @@ "use strict"; | ||
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the | ||
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is | ||
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is | ||
* used | ||
@@ -210,3 +210,3 @@ * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target | ||
*/ | ||
delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) { | ||
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) { | ||
if (identifier === null) | ||
@@ -213,0 +213,0 @@ return []; |
{ | ||
"name": "red-black-tree-typed", | ||
"version": "1.51.0", | ||
"version": "1.51.1", | ||
"description": "RedBlackTree. Javascript & Typescript Data Structure.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -243,3 +243,3 @@ /** | ||
identifier: ReturnType<C>, | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
ignoreCount = false | ||
@@ -246,0 +246,0 @@ ): BinaryTreeDeleteResult<NODE>[] { |
@@ -167,3 +167,3 @@ /** | ||
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will | ||
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single | ||
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single | ||
* parameter of type `NODE | ||
@@ -174,3 +174,3 @@ * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`. | ||
identifier: ReturnType<C>, | ||
callback: C = this._defaultOneParamCallback as C | ||
callback: C = this._DEFAULT_CALLBACK as C | ||
): BinaryTreeDeleteResult<NODE>[] { | ||
@@ -177,0 +177,0 @@ if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C; |
@@ -217,11 +217,11 @@ /** | ||
): NODE | undefined { | ||
let res: NODE | undefined; | ||
if (this.isRealNode(keyOrNodeOrEntry)) { | ||
res = keyOrNodeOrEntry; | ||
return keyOrNodeOrEntry; | ||
} else if (this.isEntry(keyOrNodeOrEntry)) { | ||
if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) return; | ||
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType); | ||
} else { | ||
if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) return; | ||
return this.getNodeByKey(keyOrNodeOrEntry, iterationType); | ||
} | ||
return res; | ||
} | ||
@@ -430,22 +430,22 @@ | ||
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined { | ||
// return this.getNodes(key, this._defaultOneParamCallback, true, this.root, iterationType)[0]; | ||
if (!this.isRealNode(this.root)) return undefined; | ||
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0]; | ||
if (!this.isRealNode(this.root)) return; | ||
if (iterationType === 'RECURSIVE') { | ||
const _dfs = (cur: NODE): NODE | undefined => { | ||
const dfs = (cur: NODE): NODE | undefined => { | ||
if (cur.key === key) return cur; | ||
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return; | ||
if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left)) return _dfs(cur.left); | ||
if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right)) return _dfs(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') return dfs(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') return dfs(cur.right); | ||
}; | ||
return _dfs(this.root); | ||
return dfs(this.root); | ||
} else { | ||
const queue = new Queue<NODE>([this.root]); | ||
while (queue.size > 0) { | ||
const cur = queue.shift(); | ||
const stack = [this.root]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop(); | ||
if (this.isRealNode(cur)) { | ||
if (this._compare(cur.key, key) === 'EQ') return cur; | ||
if (this._compare(cur.key, key) === 'GT') this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, key) === 'LT') this.isRealNode(cur.right) && queue.push(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') stack.push(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') stack.push(cur.right); | ||
} | ||
@@ -486,3 +486,3 @@ } | ||
identifier: ReturnType<C> | undefined, | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
onlyOne = false, | ||
@@ -497,3 +497,3 @@ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur: NODE) => { | ||
const dfs = (cur: NODE) => { | ||
const callbackResult = callback(cur); | ||
@@ -507,12 +507,12 @@ if (callbackResult === identifier) { | ||
// TODO potential bug | ||
if (callback === this._defaultOneParamCallback) { | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') _traverse(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') _traverse(cur.right); | ||
if (callback === this._DEFAULT_CALLBACK) { | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') dfs(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') dfs(cur.right); | ||
} else { | ||
this.isRealNode(cur.left) && _traverse(cur.left); | ||
this.isRealNode(cur.right) && _traverse(cur.right); | ||
this.isRealNode(cur.left) && dfs(cur.left); | ||
this.isRealNode(cur.right) && dfs(cur.right); | ||
} | ||
}; | ||
_traverse(beginRoot); | ||
dfs(beginRoot); | ||
} else { | ||
@@ -529,3 +529,3 @@ const stack = [beginRoot]; | ||
// TODO potential bug | ||
if (callback === this._defaultOneParamCallback) { | ||
if (callback === this._DEFAULT_CALLBACK) { | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right); | ||
@@ -577,3 +577,3 @@ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left); | ||
override dfs<C extends BTNCallback<NODE>>( | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
pattern: DFSOrderPattern = 'IN', | ||
@@ -609,3 +609,3 @@ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
override bfs<C extends BTNCallback<NODE>>( | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
@@ -641,3 +641,3 @@ iterationType: IterationType = this.iterationType | ||
override listLevels<C extends BTNCallback<NODE>>( | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
@@ -712,3 +712,3 @@ iterationType: IterationType = this.iterationType | ||
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>( | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
lesserOrGreater: CP = 'LT', | ||
@@ -726,11 +726,11 @@ targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur: NODE) => { | ||
const dfs = (cur: NODE) => { | ||
const compared = this._compare(cur.key, targetKey); | ||
if (compared === lesserOrGreater) ans.push(callback(cur)); | ||
if (this.isRealNode(cur.left)) _traverse(cur.left); | ||
if (this.isRealNode(cur.right)) _traverse(cur.right); | ||
if (this.isRealNode(cur.left)) dfs(cur.left); | ||
if (this.isRealNode(cur.right)) dfs(cur.right); | ||
}; | ||
_traverse(this.root); | ||
dfs(this.root); | ||
return ans; | ||
@@ -737,0 +737,0 @@ } else { |
@@ -234,3 +234,3 @@ import type { | ||
identifier: ReturnType<C> | undefined, | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
beginRoot: BSTNKeyOrNode<K, NODE> = this.root, | ||
@@ -312,3 +312,3 @@ iterationType: IterationType = this.iterationType | ||
* the binary tree based on its identifier. It is an optional parameter and if not provided, the | ||
* `_defaultOneParamCallback` function is used as the default callback. The callback function should | ||
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should | ||
* return the identifier of the node to | ||
@@ -319,3 +319,3 @@ * @returns an array of BinaryTreeDeleteResult<NODE> objects. | ||
identifier: ReturnType<C> | null | undefined, | ||
callback: C = this._defaultOneParamCallback as C | ||
callback: C = this._DEFAULT_CALLBACK as C | ||
): BinaryTreeDeleteResult<NODE>[] { | ||
@@ -322,0 +322,0 @@ if (identifier === null) return []; |
@@ -245,3 +245,3 @@ /** | ||
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the | ||
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is | ||
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is | ||
* used | ||
@@ -256,3 +256,3 @@ * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target | ||
identifier: ReturnType<C> | null | undefined, | ||
callback: C = this._defaultOneParamCallback as C, | ||
callback: C = this._DEFAULT_CALLBACK as C, | ||
ignoreCount = false | ||
@@ -259,0 +259,0 @@ ): BinaryTreeDeleteResult<NODE>[] { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2120321