red-black-tree-typed
Advanced tools
Comparing version 1.50.7 to 1.50.8
@@ -9,3 +9,2 @@ /** | ||
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types'; | ||
import { IterationType } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -161,3 +160,3 @@ import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
*/ | ||
perfectlyBalance(iterationType?: IterationType): boolean; | ||
perfectlyBalance(iterationType?: import("../../types").IterationType): boolean; | ||
/** | ||
@@ -164,0 +163,0 @@ * Time complexity: O(n) |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0; | ||
const types_1 = require("../../types"); | ||
const avl_tree_1 = require("./avl-tree"); | ||
@@ -208,6 +207,6 @@ class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode { | ||
const { familyPosition: fp } = curr; | ||
if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) { | ||
if (fp === 'LEFT' || fp === 'ROOT_LEFT') { | ||
parent.left = curr.right; | ||
} | ||
else if (fp === types_1.FamilyPosition.RIGHT || fp === types_1.FamilyPosition.ROOT_RIGHT) { | ||
else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') { | ||
parent.right = curr.right; | ||
@@ -279,3 +278,3 @@ } | ||
this.clear(); | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l, r) => { | ||
@@ -282,0 +281,0 @@ if (l > r) |
@@ -138,7 +138,7 @@ /** | ||
* type of iteration to be used when searching for a node by key. It has a default value of | ||
* `IterationType.ITERATIVE`. | ||
* `'ITERATIVE'`. | ||
* @returns either the node corresponding to the given key if it is a valid node key, or the key | ||
* itself if it is not a valid node key. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | null | undefined; | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | null | undefined; | ||
/** | ||
@@ -252,3 +252,3 @@ * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class. | ||
*/ | ||
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined; | ||
getNodeByKey(key: K, iterationType?: string): NODE | undefined; | ||
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined; | ||
@@ -255,0 +255,0 @@ get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined; |
@@ -112,6 +112,6 @@ /** | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* type of iteration to be performed. It has a default value of `'ITERATIVE'`. | ||
* @returns either a node object (NODE) or undefined. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | undefined; | ||
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: string): NODE | undefined; | ||
/** | ||
@@ -183,3 +183,3 @@ * The function checks if an keyOrNodeOrEntry is an instance of BSTNode. | ||
*/ | ||
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined; | ||
getNodeByKey(key: K, iterationType?: string): NODE | undefined; | ||
/** | ||
@@ -377,6 +377,8 @@ * Time Complexity: O(log n) | ||
* @param {K} b - The parameter "b" in the above code represents a K. | ||
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater | ||
* than), CP.lt (less than), or CP.eq (equal). | ||
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater | ||
* than), 'LT' (less than), or 'EQ' (equal). | ||
*/ | ||
protected _compare(a: K, b: K): CP; | ||
protected _lt(a: K, b: K): boolean; | ||
protected _gt(a: K, b: K): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BST = exports.BSTNode = void 0; | ||
const types_1 = require("../../types"); | ||
const binary_tree_1 = require("./binary-tree"); | ||
@@ -73,3 +72,3 @@ const queue_1 = require("../queue"); | ||
super([], options); | ||
this._variant = types_1.BSTVariant.STANDARD; | ||
this._variant = 'STANDARD'; | ||
if (options) { | ||
@@ -166,6 +165,6 @@ const { variant } = options; | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* type of iteration to be performed. It has a default value of `'ITERATIVE'`. | ||
* @returns either a node object (NODE) or undefined. | ||
*/ | ||
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) { | ||
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') { | ||
let res; | ||
@@ -220,3 +219,3 @@ if (this.isRealNode(keyOrNodeOrEntry)) { | ||
while (current !== undefined) { | ||
if (this._compare(current.key, newNode.key) === types_1.CP.eq) { | ||
if (this._compare(current.key, newNode.key) === 'EQ') { | ||
// if (current !== newNode) { | ||
@@ -232,3 +231,3 @@ // The key value is the same but the reference is different, update the value of the existing node | ||
} | ||
else if (this._compare(current.key, newNode.key) === types_1.CP.gt) { | ||
else if (this._compare(current.key, newNode.key) === 'GT') { | ||
if (current.left === undefined) { | ||
@@ -342,3 +341,3 @@ current.left = newNode; | ||
}; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
_dfs(sorted); | ||
@@ -369,6 +368,6 @@ } | ||
*/ | ||
getNodeByKey(key, iterationType = types_1.IterationType.ITERATIVE) { | ||
getNodeByKey(key, iterationType = 'ITERATIVE') { | ||
if (!this.isRealNode(this.root)) | ||
return undefined; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _dfs = (cur) => { | ||
@@ -379,5 +378,5 @@ if (cur.key === key) | ||
return; | ||
if (this._compare(cur.key, key) === types_1.CP.gt && this.isRealNode(cur.left)) | ||
if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left)) | ||
return _dfs(cur.left); | ||
if (this._compare(cur.key, key) === types_1.CP.lt && this.isRealNode(cur.right)) | ||
if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right)) | ||
return _dfs(cur.right); | ||
@@ -392,7 +391,7 @@ }; | ||
if (this.isRealNode(cur)) { | ||
if (this._compare(cur.key, key) === types_1.CP.eq) | ||
if (this._compare(cur.key, key) === 'EQ') | ||
return cur; | ||
if (this._compare(cur.key, key) === types_1.CP.gt) | ||
if (this._compare(cur.key, key) === 'GT') | ||
this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, key) === types_1.CP.lt) | ||
if (this._compare(cur.key, key) === 'LT') | ||
this.isRealNode(cur.right) && queue.push(cur.right); | ||
@@ -436,3 +435,3 @@ } | ||
const ans = []; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur) => { | ||
@@ -449,6 +448,6 @@ const callbackResult = callback(cur); | ||
if (callback === this._defaultOneParamCallback) { | ||
if (this._compare(cur.key, identifier) === types_1.CP.gt) | ||
this.isRealNode(cur.left) && _traverse(cur.left); | ||
if (this._compare(cur.key, identifier) === types_1.CP.lt) | ||
this.isRealNode(cur.right) && _traverse(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT') | ||
_traverse(cur.left); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT') | ||
_traverse(cur.right); | ||
} | ||
@@ -463,5 +462,5 @@ else { | ||
else { | ||
const queue = new queue_1.Queue([beginRoot]); | ||
while (queue.size > 0) { | ||
const cur = queue.shift(); | ||
const stack = [beginRoot]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop(); | ||
if (this.isRealNode(cur)) { | ||
@@ -476,10 +475,16 @@ const callbackResult = callback(cur); | ||
if (callback === this._defaultOneParamCallback) { | ||
if (this._compare(cur.key, identifier) === types_1.CP.gt) | ||
this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, identifier) === types_1.CP.lt) | ||
this.isRealNode(cur.right) && queue.push(cur.right); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT') | ||
stack.push(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT') | ||
stack.push(cur.left); | ||
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right); | ||
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left); | ||
// // @ts-ignore | ||
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right); | ||
// // @ts-ignore | ||
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left); | ||
} | ||
else { | ||
this.isRealNode(cur.left) && queue.push(cur.left); | ||
this.isRealNode(cur.right) && queue.push(cur.right); | ||
this.isRealNode(cur.right) && stack.push(cur.right); | ||
this.isRealNode(cur.left) && stack.push(cur.left); | ||
} | ||
@@ -514,3 +519,3 @@ } | ||
*/ | ||
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) { | ||
dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = 'ITERATIVE') { | ||
return super.dfs(callback, pattern, beginRoot, iterationType, false); | ||
@@ -588,3 +593,3 @@ } | ||
return undefined; | ||
if (this._variant === types_1.BSTVariant.STANDARD) { | ||
if (this._variant === 'STANDARD') { | ||
// For BSTVariant.MIN, find the rightmost node | ||
@@ -628,3 +633,3 @@ while (current.right !== undefined) { | ||
*/ | ||
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) { | ||
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) { | ||
targetNode = this.ensureNode(targetNode); | ||
@@ -637,3 +642,3 @@ const ans = []; | ||
const targetKey = targetNode.key; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur) => { | ||
@@ -688,3 +693,3 @@ const compared = this._compare(cur.key, targetKey); | ||
return false; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l, r) => { | ||
@@ -747,3 +752,3 @@ if (l > r) | ||
let balanced = true; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _height = (cur) => { | ||
@@ -805,4 +810,4 @@ if (!cur) | ||
* @param {K} b - The parameter "b" in the above code represents a K. | ||
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater | ||
* than), CP.lt (less than), or CP.eq (equal). | ||
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater | ||
* than), 'LT' (less than), or 'EQ' (equal). | ||
*/ | ||
@@ -812,6 +817,22 @@ _compare(a, b) { | ||
const extractedB = this.extractor(b); | ||
const compared = this.variant === types_1.BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA; | ||
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq; | ||
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA; | ||
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ'; | ||
} | ||
_lt(a, b) { | ||
const extractedA = this.extractor(a); | ||
const extractedB = this.extractor(b); | ||
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB; | ||
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB; | ||
// return extractedA < extractedB; | ||
// return a < b; | ||
} | ||
_gt(a, b) { | ||
const extractedA = this.extractor(a); | ||
const extractedB = this.extractor(b); | ||
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB; | ||
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB; | ||
// return extractedA > extractedB; | ||
// return a > b; | ||
} | ||
} | ||
exports.BST = BST; |
@@ -196,5 +196,4 @@ "use strict"; | ||
var _a; | ||
if (identifier instanceof RedBlackTreeNode) | ||
callback = (node => node); | ||
return (_a = super.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined; | ||
// if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C; | ||
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined; | ||
} | ||
@@ -238,3 +237,3 @@ /** | ||
const insertStatus = this._insert(newNode); | ||
if (insertStatus === types_1.CRUD.CREATED) { | ||
if (insertStatus === 'CREATED') { | ||
// Ensure the root is black | ||
@@ -251,3 +250,3 @@ if (this.isRealNode(this._root)) { | ||
else | ||
return insertStatus === types_1.CRUD.UPDATED; | ||
return insertStatus === 'UPDATED'; | ||
} | ||
@@ -385,3 +384,3 @@ /** | ||
this._replaceNode(current, node); | ||
return types_1.CRUD.UPDATED; | ||
return 'UPDATED'; | ||
} | ||
@@ -403,3 +402,3 @@ } | ||
this._insertFixup(node); | ||
return types_1.CRUD.CREATED; | ||
return 'CREATED'; | ||
} | ||
@@ -406,0 +405,0 @@ /** |
@@ -9,3 +9,2 @@ /** | ||
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types'; | ||
import { IterationType } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -168,3 +167,3 @@ import { RedBlackTree, RedBlackTreeNode } from './rb-tree'; | ||
*/ | ||
perfectlyBalance(iterationType?: IterationType): boolean; | ||
perfectlyBalance(iterationType?: import("../../types").IterationType): boolean; | ||
/** | ||
@@ -171,0 +170,0 @@ * Time complexity: O(n) |
@@ -315,3 +315,3 @@ "use strict"; | ||
this.clear(); | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l, r) => { | ||
@@ -318,0 +318,0 @@ if (l > r) |
@@ -1,10 +0,3 @@ | ||
export declare enum BSTVariant { | ||
STANDARD = "STANDARD", | ||
INVERSE = "INVERSE" | ||
} | ||
export declare enum CP { | ||
lt = "lt", | ||
eq = "eq", | ||
gt = "gt" | ||
} | ||
export type BSTVariant = 'STANDARD' | 'INVERSE'; | ||
export type CP = 'LT' | 'EQ' | 'GT'; | ||
/** | ||
@@ -16,15 +9,4 @@ * Enum representing different loop types. | ||
*/ | ||
export declare enum IterationType { | ||
ITERATIVE = "ITERATIVE", | ||
RECURSIVE = "RECURSIVE" | ||
} | ||
export declare enum FamilyPosition { | ||
ROOT = "ROOT", | ||
LEFT = "LEFT", | ||
RIGHT = "RIGHT", | ||
ROOT_LEFT = "ROOT_LEFT", | ||
ROOT_RIGHT = "ROOT_RIGHT", | ||
ISOLATED = "ISOLATED", | ||
MAL_NODE = "MAL_NODE" | ||
} | ||
export type IterationType = 'ITERATIVE' | 'RECURSIVE'; | ||
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE'; | ||
export type Comparator<K> = (a: K, b: K) => number; | ||
@@ -56,7 +38,2 @@ export type DFSOrderPattern = 'pre' | 'in' | 'post'; | ||
}; | ||
export declare enum CRUD { | ||
CREATED = "CREATED", | ||
READ = "READ", | ||
UPDATED = "UPDATED", | ||
DELETED = "DELETED" | ||
} | ||
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CRUD = exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0; | ||
var BSTVariant; | ||
(function (BSTVariant) { | ||
BSTVariant["STANDARD"] = "STANDARD"; | ||
BSTVariant["INVERSE"] = "INVERSE"; | ||
})(BSTVariant = exports.BSTVariant || (exports.BSTVariant = {})); | ||
var CP; | ||
(function (CP) { | ||
CP["lt"] = "lt"; | ||
CP["eq"] = "eq"; | ||
CP["gt"] = "gt"; | ||
})(CP = exports.CP || (exports.CP = {})); | ||
/** | ||
* Enum representing different loop types. | ||
* | ||
* - `iterative`: Indicates the iterative loop type (with loops that use iterations). | ||
* - `recursive`: Indicates the recursive loop type (with loops that call themselves). | ||
*/ | ||
var IterationType; | ||
(function (IterationType) { | ||
IterationType["ITERATIVE"] = "ITERATIVE"; | ||
IterationType["RECURSIVE"] = "RECURSIVE"; | ||
})(IterationType = exports.IterationType || (exports.IterationType = {})); | ||
var FamilyPosition; | ||
(function (FamilyPosition) { | ||
FamilyPosition["ROOT"] = "ROOT"; | ||
FamilyPosition["LEFT"] = "LEFT"; | ||
FamilyPosition["RIGHT"] = "RIGHT"; | ||
FamilyPosition["ROOT_LEFT"] = "ROOT_LEFT"; | ||
FamilyPosition["ROOT_RIGHT"] = "ROOT_RIGHT"; | ||
FamilyPosition["ISOLATED"] = "ISOLATED"; | ||
FamilyPosition["MAL_NODE"] = "MAL_NODE"; | ||
})(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {})); | ||
var CRUD; | ||
(function (CRUD) { | ||
CRUD["CREATED"] = "CREATED"; | ||
CRUD["READ"] = "READ"; | ||
CRUD["UPDATED"] = "UPDATED"; | ||
CRUD["DELETED"] = "DELETED"; | ||
})(CRUD = exports.CRUD || (exports.CRUD = {})); |
{ | ||
"name": "red-black-tree-typed", | ||
"version": "1.50.7", | ||
"version": "1.50.8", | ||
"description": "RedBlackTree. Javascript & Typescript Data Structure.", | ||
@@ -145,4 +145,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.50.7" | ||
"data-structure-typed": "^1.50.8" | ||
} | ||
} |
@@ -17,3 +17,2 @@ /** | ||
} from '../../types'; | ||
import { FamilyPosition, IterationType } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -253,5 +252,5 @@ import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
const { familyPosition: fp } = curr; | ||
if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) { | ||
if (fp === 'LEFT' || fp === 'ROOT_LEFT') { | ||
parent.left = curr.right; | ||
} else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) { | ||
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') { | ||
parent.right = curr.right; | ||
@@ -329,3 +328,3 @@ } | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l: number, r: number) => { | ||
@@ -332,0 +331,0 @@ if (l > r) return; |
@@ -129,3 +129,3 @@ /** | ||
protected _variant = BSTVariant.STANDARD; | ||
protected _variant: BSTVariant = 'STANDARD'; | ||
@@ -211,9 +211,6 @@ /** | ||
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the | ||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`. | ||
* type of iteration to be performed. It has a default value of `'ITERATIVE'`. | ||
* @returns either a node object (NODE) or undefined. | ||
*/ | ||
override ensureNode( | ||
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, | ||
iterationType = IterationType.ITERATIVE | ||
): NODE | undefined { | ||
override ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType = 'ITERATIVE'): NODE | undefined { | ||
let res: NODE | undefined; | ||
@@ -268,3 +265,3 @@ if (this.isRealNode(keyOrNodeOrEntry)) { | ||
while (current !== undefined) { | ||
if (this._compare(current.key, newNode.key) === CP.eq) { | ||
if (this._compare(current.key, newNode.key) === 'EQ') { | ||
// if (current !== newNode) { | ||
@@ -281,3 +278,3 @@ // The key value is the same but the reference is different, update the value of the existing node | ||
// } | ||
} else if (this._compare(current.key, newNode.key) === CP.gt) { | ||
} else if (this._compare(current.key, newNode.key) === 'GT') { | ||
if (current.left === undefined) { | ||
@@ -404,3 +401,3 @@ current.left = newNode; | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
_dfs(sorted); | ||
@@ -433,5 +430,5 @@ } else { | ||
*/ | ||
override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined { | ||
override getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined { | ||
if (!this.isRealNode(this.root)) return undefined; | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _dfs = (cur: NODE): NODE | undefined => { | ||
@@ -441,4 +438,4 @@ if (cur.key === key) return cur; | ||
if (this._compare(cur.key, key) === CP.gt && this.isRealNode(cur.left)) return _dfs(cur.left); | ||
if (this._compare(cur.key, key) === CP.lt && this.isRealNode(cur.right)) return _dfs(cur.right); | ||
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); | ||
}; | ||
@@ -452,5 +449,5 @@ | ||
if (this.isRealNode(cur)) { | ||
if (this._compare(cur.key, key) === CP.eq) return cur; | ||
if (this._compare(cur.key, key) === CP.gt) this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, key) === CP.lt) this.isRealNode(cur.right) && queue.push(cur.right); | ||
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); | ||
} | ||
@@ -500,3 +497,3 @@ } | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur: NODE) => { | ||
@@ -512,4 +509,4 @@ const callbackResult = callback(cur); | ||
if (callback === this._defaultOneParamCallback) { | ||
if (this._compare(cur.key, identifier as K) === CP.gt) this.isRealNode(cur.left) && _traverse(cur.left); | ||
if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(cur.right) && _traverse(cur.right); | ||
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); | ||
} else { | ||
@@ -523,5 +520,5 @@ this.isRealNode(cur.left) && _traverse(cur.left); | ||
} else { | ||
const queue = new Queue<NODE>([beginRoot]); | ||
while (queue.size > 0) { | ||
const cur = queue.shift(); | ||
const stack = [beginRoot]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop(); | ||
if (this.isRealNode(cur)) { | ||
@@ -535,7 +532,15 @@ const callbackResult = callback(cur); | ||
if (callback === this._defaultOneParamCallback) { | ||
if (this._compare(cur.key, identifier as K) === CP.gt) this.isRealNode(cur.left) && queue.push(cur.left); | ||
if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(cur.right) && queue.push(cur.right); | ||
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right); | ||
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left); | ||
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right); | ||
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left); | ||
// // @ts-ignore | ||
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right); | ||
// // @ts-ignore | ||
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left); | ||
} else { | ||
this.isRealNode(cur.left) && queue.push(cur.left); | ||
this.isRealNode(cur.right) && queue.push(cur.right); | ||
this.isRealNode(cur.right) && stack.push(cur.right); | ||
this.isRealNode(cur.left) && stack.push(cur.left); | ||
} | ||
@@ -577,3 +582,3 @@ } | ||
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
iterationType: IterationType = IterationType.ITERATIVE | ||
iterationType: IterationType = 'ITERATIVE' | ||
): ReturnType<C>[] { | ||
@@ -666,3 +671,3 @@ return super.dfs(callback, pattern, beginRoot, iterationType, false); | ||
if (this._variant === BSTVariant.STANDARD) { | ||
if (this._variant === 'STANDARD') { | ||
// For BSTVariant.MIN, find the rightmost node | ||
@@ -709,3 +714,3 @@ while (current.right !== undefined) { | ||
callback: C = this._defaultOneParamCallback as C, | ||
lesserOrGreater: CP = CP.lt, | ||
lesserOrGreater: CP = 'LT', | ||
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root, | ||
@@ -721,3 +726,3 @@ iterationType = this.iterationType | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _traverse = (cur: NODE) => { | ||
@@ -771,3 +776,3 @@ const compared = this._compare(cur.key, targetKey); | ||
if (sorted.length < 1) return false; | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l: number, r: number) => { | ||
@@ -832,3 +837,3 @@ if (l > r) return; | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const _height = (cur: NODE | undefined): number => { | ||
@@ -889,4 +894,4 @@ if (!cur) return 0; | ||
* @param {K} b - The parameter "b" in the above code represents a K. | ||
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater | ||
* than), CP.lt (less than), or CP.eq (equal). | ||
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater | ||
* than), 'LT' (less than), or 'EQ' (equal). | ||
*/ | ||
@@ -896,6 +901,24 @@ protected _compare(a: K, b: K): CP { | ||
const extractedB = this.extractor(b); | ||
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA; | ||
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA; | ||
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq; | ||
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ'; | ||
} | ||
protected _lt(a: K, b: K): boolean { | ||
const extractedA = this.extractor(a); | ||
const extractedB = this.extractor(b); | ||
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB; | ||
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB; | ||
// return extractedA < extractedB; | ||
// return a < b; | ||
} | ||
protected _gt(a: K, b: K): boolean { | ||
const extractedA = this.extractor(a); | ||
const extractedB = this.extractor(b); | ||
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB; | ||
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB; | ||
// return extractedA > extractedB; | ||
// return a > b; | ||
} | ||
} |
@@ -237,4 +237,4 @@ import type { | ||
): NODE | null | undefined { | ||
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C; | ||
return super.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined; | ||
// if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C; | ||
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined; | ||
} | ||
@@ -283,3 +283,3 @@ | ||
if (insertStatus === CRUD.CREATED) { | ||
if (insertStatus === 'CREATED') { | ||
// Ensure the root is black | ||
@@ -293,3 +293,3 @@ if (this.isRealNode(this._root)) { | ||
return true; | ||
} else return insertStatus === CRUD.UPDATED; | ||
} else return insertStatus === 'UPDATED'; | ||
} | ||
@@ -441,3 +441,3 @@ | ||
this._replaceNode(current, node); | ||
return CRUD.UPDATED; | ||
return 'UPDATED'; | ||
} | ||
@@ -461,3 +461,3 @@ } | ||
this._insertFixup(node); | ||
return CRUD.CREATED; | ||
return 'CREATED'; | ||
} | ||
@@ -464,0 +464,0 @@ |
@@ -17,3 +17,3 @@ /** | ||
} from '../../types'; | ||
import { IterationType, RBTNColor } from '../../types'; | ||
import { RBTNColor } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -367,3 +367,3 @@ import { RedBlackTree, RedBlackTreeNode } from './rb-tree'; | ||
if (iterationType === IterationType.RECURSIVE) { | ||
if (iterationType === 'RECURSIVE') { | ||
const buildBalanceBST = (l: number, r: number) => { | ||
@@ -370,0 +370,0 @@ if (l > r) return; |
@@ -1,12 +0,4 @@ | ||
export enum BSTVariant { | ||
STANDARD = 'STANDARD', | ||
INVERSE = 'INVERSE' | ||
} | ||
export type BSTVariant = 'STANDARD' | 'INVERSE'; | ||
export type CP = 'LT' | 'EQ' | 'GT'; | ||
export enum CP { | ||
lt = 'lt', | ||
eq = 'eq', | ||
gt = 'gt' | ||
} | ||
/** | ||
@@ -18,16 +10,5 @@ * Enum representing different loop types. | ||
*/ | ||
export enum IterationType { | ||
ITERATIVE = 'ITERATIVE', | ||
RECURSIVE = 'RECURSIVE' | ||
} | ||
export type IterationType = 'ITERATIVE' | 'RECURSIVE'; | ||
export enum FamilyPosition { | ||
ROOT = 'ROOT', | ||
LEFT = 'LEFT', | ||
RIGHT = 'RIGHT', | ||
ROOT_LEFT = 'ROOT_LEFT', | ||
ROOT_RIGHT = 'ROOT_RIGHT', | ||
ISOLATED = 'ISOLATED', | ||
MAL_NODE = 'MAL_NODE' | ||
} | ||
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE'; | ||
@@ -68,7 +49,2 @@ export type Comparator<K> = (a: K, b: K) => number; | ||
export enum CRUD { | ||
CREATED = 'CREATED', | ||
READ = 'READ', | ||
UPDATED = 'UPDATED', | ||
DELETED = 'DELETED' | ||
} | ||
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2115771
39746
Updateddata-structure-typed@^1.50.8