@amaui/amaui-binary-tree
Advanced tools
Comparing version 1.0.0 to 1.0.1
import { TMethod } from '@amaui/models'; | ||
export declare type TArrayVariant = 'inorder' | 'preorder' | 'postorder'; | ||
export interface IAmauiNode { | ||
value: any; | ||
left: AmauiNode; | ||
right: AmauiNode; | ||
left?: AmauiNode; | ||
right?: AmauiNode; | ||
[p: string]: any; | ||
} | ||
export declare class AmauiNode implements IAmauiNode { | ||
value: any; | ||
left: AmauiNode; | ||
right: AmauiNode; | ||
constructor(value: any); | ||
left?: AmauiNode; | ||
right?: AmauiNode; | ||
[p: string]: any; | ||
constructor(value: any, left?: AmauiNode, right?: AmauiNode); | ||
} | ||
export interface IAmauiBinaryTree { | ||
root?: AmauiNode; | ||
add: (value: any) => AmauiBinaryTree; | ||
find: (value: any) => AmauiNode | undefined; | ||
remove: (value: any) => void; | ||
removeNode: (amauiNode: AmauiNode, value: any) => AmauiNode | undefined; | ||
inorder: (value: AmauiNode, method: (value: AmauiNode) => any) => void; | ||
preorder: (value: AmauiNode, method: (value: AmauiNode) => any) => void; | ||
postorder: (value: AmauiNode, method: (value: AmauiNode) => any) => void; | ||
min(value: AmauiNode): AmauiNode; | ||
max(value: AmauiNode): AmauiNode; | ||
} | ||
export declare class AmauiBinaryTree implements IAmauiBinaryTree { | ||
root: AmauiNode; | ||
array(variant?: string): Array<any>; | ||
lowestCommonAncestor(root: AmauiNode, value: AmauiNode, value1: AmauiNode): AmauiNode | undefined; | ||
maxDepth(amauiNode: AmauiNode): number; | ||
get valid(): boolean; | ||
inorder(value: AmauiNode, method: TMethod): void; | ||
preorder(value: AmauiNode, method: TMethod): void; | ||
postorder(value: AmauiNode, method: TMethod): void; | ||
static build(value: any[]): AmauiBinaryTree; | ||
static lowestCommonAncestor(value: AmauiNode | any, value1: AmauiNode | any, root: AmauiNode): AmauiNode | undefined; | ||
static maxDepth(amauiNode: AmauiNode): number; | ||
static valid(value: AmauiBinaryTree): boolean; | ||
static preorder(value: AmauiNode, method: TMethod): void; | ||
static inorder(value: AmauiNode, method: TMethod): void; | ||
static postorder(value: AmauiNode, method: TMethod): void; | ||
static min(value: AmauiNode): AmauiNode; | ||
static max(value: AmauiNode): AmauiNode; | ||
array(variant?: TArrayVariant): Array<any>; | ||
build(value: any[]): AmauiBinaryTree; | ||
add(value: AmauiNode | any): AmauiBinaryTree; | ||
@@ -38,4 +36,2 @@ find(value: any): AmauiNode | undefined; | ||
removeNode(amauiNode: AmauiNode, value: any): AmauiNode | undefined; | ||
min(value: AmauiNode): AmauiNode; | ||
max(value: AmauiNode): AmauiNode; | ||
} |
@@ -11,6 +11,6 @@ "use strict"; | ||
class AmauiNode { | ||
constructor(value) { | ||
constructor(value, left, right) { | ||
this.value = value; | ||
this.left = void 0; | ||
this.right = void 0; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
@@ -27,9 +27,7 @@ | ||
array(variant = 'inorder') { | ||
const value = []; | ||
if (this[variant]) this[variant](this.root, amauiNode => value.push(amauiNode.value)); | ||
return value; | ||
static build(value) { | ||
return new AmauiBinaryTree().build(value); | ||
} | ||
lowestCommonAncestor(root = this.root, value, value1) { | ||
static lowestCommonAncestor(value, value1, root) { | ||
let lca; | ||
@@ -39,3 +37,3 @@ | ||
if (!amauiNode) return; | ||
const mid = amauiNode.value === (value === null || value === void 0 ? void 0 : value.value) || amauiNode.value === (value1 === null || value1 === void 0 ? void 0 : value1.value); | ||
const mid = amauiNode.value === (value instanceof AmauiNode ? value.value : value) || amauiNode.value === (value1 instanceof AmauiNode ? value1.value : value1); | ||
const left = lowestCommonAncestorMethod(amauiNode.left); | ||
@@ -51,3 +49,3 @@ const right = lowestCommonAncestorMethod(amauiNode.right); | ||
maxDepth(amauiNode) { | ||
static maxDepth(amauiNode) { | ||
const maxDepthMethod = value => { | ||
@@ -61,25 +59,27 @@ if (value === undefined) return 0; | ||
get valid() { | ||
static valid(value) { | ||
const isValid = amauiNode => { | ||
var _amauiNode$left, _amauiNode$right; | ||
if (amauiNode === undefined) return true; | ||
if (amauiNode.left && amauiNode.left.value >= amauiNode.value) return false; | ||
if (amauiNode.right && amauiNode.right.value <= amauiNode.value) return false; | ||
if (((_amauiNode$left = amauiNode.left) === null || _amauiNode$left === void 0 ? void 0 : _amauiNode$left.value) >= amauiNode.value) return false; | ||
if (((_amauiNode$right = amauiNode.right) === null || _amauiNode$right === void 0 ? void 0 : _amauiNode$right.value) <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(this.root); | ||
return isValid(value.root); | ||
} | ||
inorder(value, method) { | ||
static preorder(value, method) { | ||
if (value !== undefined && (0, _utils.is)('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value); | ||
this.inorder(value.right, method); | ||
method(value, value.left, value.right); | ||
this.preorder(value.left, method); | ||
this.preorder(value.right, method); | ||
} | ||
} | ||
preorder(value, method) { | ||
static inorder(value, method) { | ||
if (value !== undefined && (0, _utils.is)('function', method)) { | ||
method(value); | ||
this.inorder(value.left, method); | ||
method(value, value.left, value.right); | ||
this.inorder(value.right, method); | ||
@@ -89,10 +89,45 @@ } | ||
postorder(value, method) { | ||
static postorder(value, method) { | ||
if (value !== undefined && (0, _utils.is)('function', method)) { | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
method(value); | ||
this.postorder(value.left, method); | ||
this.postorder(value.right, method); | ||
method(value, value.left, value.right); | ||
} | ||
} | ||
static min(value) { | ||
let amauiNode = value; | ||
while (((_amauiNode = amauiNode) === null || _amauiNode === void 0 ? void 0 : _amauiNode.left) !== undefined) { | ||
var _amauiNode; | ||
amauiNode = amauiNode.left; | ||
} | ||
return amauiNode; | ||
} | ||
static max(value) { | ||
let amauiNode = value; | ||
while (((_amauiNode2 = amauiNode) === null || _amauiNode2 === void 0 ? void 0 : _amauiNode2.right) !== undefined) { | ||
var _amauiNode2; | ||
amauiNode = amauiNode.right; | ||
} | ||
return amauiNode; | ||
} | ||
array(variant = 'preorder') { | ||
const value = []; | ||
if (AmauiBinaryTree[variant]) AmauiBinaryTree[variant](this.root, amauiNode => value.push(amauiNode.value)); | ||
return value; | ||
} | ||
build(value) { | ||
if ((0, _utils.is)('array', value)) value.forEach(item => this.add(item)); | ||
return this; | ||
} | ||
add(value) { | ||
@@ -135,9 +170,9 @@ const amauiNode = value instanceof AmauiNode ? value : new AmauiNode(value); | ||
let atmNode = this.root; | ||
let output; | ||
let amauiNode; | ||
while (atmNode && !output) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else output = atmNode; | ||
while (atmNode && !amauiNode) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else amauiNode = atmNode; | ||
} | ||
return output; | ||
return amauiNode; | ||
} | ||
@@ -153,35 +188,22 @@ | ||
if (value === amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return;else if (amauiNode.left === undefined) return amauiNode.right;else if (amauiNode.right === undefined) return amauiNode.left;else { | ||
let atmNode = this.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
} else if (value < amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return; | ||
if (amauiNode.left === undefined) return amauiNode.right; | ||
if (amauiNode.right === undefined) return amauiNode.left; | ||
const atmNode = AmauiBinaryTree.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
if (value < amauiNode.value) { | ||
amauiNode.left = this.removeNode(amauiNode.left, value); | ||
return amauiNode; | ||
} else { | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
} | ||
min(value) { | ||
let amauiNode = value; | ||
while (!amauiNode.left === undefined) amauiNode = amauiNode.left; | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
max(value) { | ||
let amauiNode = value; | ||
while (!amauiNode.right === undefined) amauiNode = amauiNode.right; | ||
return amauiNode; | ||
} | ||
} | ||
exports.AmauiBinaryTree = AmauiBinaryTree; |
import _createClass from "@babel/runtime/helpers/createClass"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
import { is } from '@amaui/utils'; | ||
export var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value) { | ||
export var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value, left, right) { | ||
_classCallCheck(this, AmauiNode); | ||
this.value = value; | ||
this.left = void 0; | ||
this.right = void 0; | ||
this.left = left; | ||
this.right = right; | ||
}); | ||
@@ -21,5 +21,5 @@ export var AmauiBinaryTree = /*#__PURE__*/function () { | ||
value: function array() { | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inorder'; | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'preorder'; | ||
var value = []; | ||
if (this[variant]) this[variant](this.root, function (amauiNode) { | ||
if (AmauiBinaryTree[variant]) AmauiBinaryTree[variant](this.root, function (amauiNode) { | ||
return value.push(amauiNode.value); | ||
@@ -30,71 +30,12 @@ }); | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor() { | ||
var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.root; | ||
var value = arguments.length > 1 ? arguments[1] : undefined; | ||
var value1 = arguments.length > 2 ? arguments[2] : undefined; | ||
var lca; | ||
key: "build", | ||
value: function build(value) { | ||
var _this = this; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value === null || value === void 0 ? void 0 : value.value) || amauiNode.value === (value1 === null || value1 === void 0 ? void 0 : value1.value); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
if (is('array', value)) value.forEach(function (item) { | ||
return _this.add(item); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
get: function get() { | ||
var isValid = function isValid(amauiNode) { | ||
if (amauiNode === undefined) return true; | ||
if (amauiNode.left && amauiNode.left.value >= amauiNode.value) return false; | ||
if (amauiNode.right && amauiNode.right.value <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(this.root); | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value); | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
method(value); | ||
} | ||
} | ||
}, { | ||
key: "add", | ||
@@ -139,9 +80,9 @@ value: function add(value) { | ||
var atmNode = this.root; | ||
var output; | ||
var amauiNode; | ||
while (atmNode && !output) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else output = atmNode; | ||
while (atmNode && !amauiNode) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else amauiNode = atmNode; | ||
} | ||
return output; | ||
return amauiNode; | ||
} | ||
@@ -159,17 +100,93 @@ }, { | ||
if (value === amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return;else if (amauiNode.left === undefined) return amauiNode.right;else if (amauiNode.right === undefined) return amauiNode.left;else { | ||
var atmNode = this.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
} else if (value < amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return; | ||
if (amauiNode.left === undefined) return amauiNode.right; | ||
if (amauiNode.right === undefined) return amauiNode.left; | ||
var atmNode = AmauiBinaryTree.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
if (value < amauiNode.value) { | ||
amauiNode.left = this.removeNode(amauiNode.left, value); | ||
return amauiNode; | ||
} else { | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
}], [{ | ||
key: "build", | ||
value: function build(value) { | ||
return new AmauiBinaryTree().build(value); | ||
} | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor(value, value1, root) { | ||
var lca; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value instanceof AmauiNode ? value.value : value) || amauiNode.value === (value1 instanceof AmauiNode ? value1.value : value1); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
value: function valid(value) { | ||
var isValid = function isValid(amauiNode) { | ||
var _amauiNode$left, _amauiNode$right; | ||
if (amauiNode === undefined) return true; | ||
if (((_amauiNode$left = amauiNode.left) === null || _amauiNode$left === void 0 ? void 0 : _amauiNode$left.value) >= amauiNode.value) return false; | ||
if (((_amauiNode$right = amauiNode.right) === null || _amauiNode$right === void 0 ? void 0 : _amauiNode$right.value) <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(value.root); | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value, value.left, value.right); | ||
this.preorder(value.left, method); | ||
this.preorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value, value.left, value.right); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.postorder(value.left, method); | ||
this.postorder(value.right, method); | ||
method(value, value.left, value.right); | ||
} | ||
} | ||
}, { | ||
key: "min", | ||
@@ -179,3 +196,5 @@ value: function min(value) { | ||
while (!amauiNode.left === undefined) { | ||
while (((_amauiNode = amauiNode) === null || _amauiNode === void 0 ? void 0 : _amauiNode.left) !== undefined) { | ||
var _amauiNode; | ||
amauiNode = amauiNode.left; | ||
@@ -191,3 +210,5 @@ } | ||
while (!amauiNode.right === undefined) { | ||
while (((_amauiNode2 = amauiNode) === null || _amauiNode2 === void 0 ? void 0 : _amauiNode2.right) !== undefined) { | ||
var _amauiNode2; | ||
amauiNode = amauiNode.right; | ||
@@ -194,0 +215,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -3,0 +3,0 @@ * This source code is licensed under the MIT license found in the |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -3,0 +3,0 @@ * This source code is licensed under the MIT license found in the |
import _createClass from "@babel/runtime/helpers/createClass"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
import { is } from '@amaui/utils'; | ||
export var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value) { | ||
export var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value, left, right) { | ||
_classCallCheck(this, AmauiNode); | ||
this.value = value; | ||
this.left = void 0; | ||
this.right = void 0; | ||
this.left = left; | ||
this.right = right; | ||
}); | ||
@@ -21,5 +21,5 @@ export var AmauiBinaryTree = /*#__PURE__*/function () { | ||
value: function array() { | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inorder'; | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'preorder'; | ||
var value = []; | ||
if (this[variant]) this[variant](this.root, function (amauiNode) { | ||
if (AmauiBinaryTree[variant]) AmauiBinaryTree[variant](this.root, function (amauiNode) { | ||
return value.push(amauiNode.value); | ||
@@ -30,71 +30,12 @@ }); | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor() { | ||
var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.root; | ||
var value = arguments.length > 1 ? arguments[1] : undefined; | ||
var value1 = arguments.length > 2 ? arguments[2] : undefined; | ||
var lca; | ||
key: "build", | ||
value: function build(value) { | ||
var _this = this; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value === null || value === void 0 ? void 0 : value.value) || amauiNode.value === (value1 === null || value1 === void 0 ? void 0 : value1.value); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
if (is('array', value)) value.forEach(function (item) { | ||
return _this.add(item); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
get: function get() { | ||
var isValid = function isValid(amauiNode) { | ||
if (amauiNode === undefined) return true; | ||
if (amauiNode.left && amauiNode.left.value >= amauiNode.value) return false; | ||
if (amauiNode.right && amauiNode.right.value <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(this.root); | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value); | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
method(value); | ||
} | ||
} | ||
}, { | ||
key: "add", | ||
@@ -139,9 +80,9 @@ value: function add(value) { | ||
var atmNode = this.root; | ||
var output; | ||
var amauiNode; | ||
while (atmNode && !output) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else output = atmNode; | ||
while (atmNode && !amauiNode) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else amauiNode = atmNode; | ||
} | ||
return output; | ||
return amauiNode; | ||
} | ||
@@ -159,17 +100,93 @@ }, { | ||
if (value === amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return;else if (amauiNode.left === undefined) return amauiNode.right;else if (amauiNode.right === undefined) return amauiNode.left;else { | ||
var atmNode = this.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
} else if (value < amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return; | ||
if (amauiNode.left === undefined) return amauiNode.right; | ||
if (amauiNode.right === undefined) return amauiNode.left; | ||
var atmNode = AmauiBinaryTree.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
if (value < amauiNode.value) { | ||
amauiNode.left = this.removeNode(amauiNode.left, value); | ||
return amauiNode; | ||
} else { | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
}], [{ | ||
key: "build", | ||
value: function build(value) { | ||
return new AmauiBinaryTree().build(value); | ||
} | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor(value, value1, root) { | ||
var lca; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value instanceof AmauiNode ? value.value : value) || amauiNode.value === (value1 instanceof AmauiNode ? value1.value : value1); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
value: function valid(value) { | ||
var isValid = function isValid(amauiNode) { | ||
var _amauiNode$left, _amauiNode$right; | ||
if (amauiNode === undefined) return true; | ||
if (((_amauiNode$left = amauiNode.left) === null || _amauiNode$left === void 0 ? void 0 : _amauiNode$left.value) >= amauiNode.value) return false; | ||
if (((_amauiNode$right = amauiNode.right) === null || _amauiNode$right === void 0 ? void 0 : _amauiNode$right.value) <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(value.root); | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value, value.left, value.right); | ||
this.preorder(value.left, method); | ||
this.preorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value, value.left, value.right); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.postorder(value.left, method); | ||
this.postorder(value.right, method); | ||
method(value, value.left, value.right); | ||
} | ||
} | ||
}, { | ||
key: "min", | ||
@@ -179,3 +196,5 @@ value: function min(value) { | ||
while (!amauiNode.left === undefined) { | ||
while (((_amauiNode = amauiNode) === null || _amauiNode === void 0 ? void 0 : _amauiNode.left) !== undefined) { | ||
var _amauiNode; | ||
amauiNode = amauiNode.left; | ||
@@ -191,3 +210,5 @@ } | ||
while (!amauiNode.right === undefined) { | ||
while (((_amauiNode2 = amauiNode) === null || _amauiNode2 === void 0 ? void 0 : _amauiNode2.right) !== undefined) { | ||
var _amauiNode2; | ||
amauiNode = amauiNode.right; | ||
@@ -194,0 +215,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -3,0 +3,0 @@ * This source code is licensed under the MIT license found in the |
import { is } from '@amaui/utils'; | ||
export class AmauiNode { | ||
constructor(value) { | ||
constructor(value, left, right) { | ||
this.value = value; | ||
this.left = void 0; | ||
this.right = void 0; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
@@ -15,9 +15,7 @@ | ||
array(variant = 'inorder') { | ||
const value = []; | ||
if (this[variant]) this[variant](this.root, amauiNode => value.push(amauiNode.value)); | ||
return value; | ||
static build(value) { | ||
return new AmauiBinaryTree().build(value); | ||
} | ||
lowestCommonAncestor(root = this.root, value, value1) { | ||
static lowestCommonAncestor(value, value1, root) { | ||
let lca; | ||
@@ -27,3 +25,3 @@ | ||
if (!amauiNode) return; | ||
const mid = amauiNode.value === value?.value || amauiNode.value === value1?.value; | ||
const mid = amauiNode.value === (value instanceof AmauiNode ? value.value : value) || amauiNode.value === (value1 instanceof AmauiNode ? value1.value : value1); | ||
const left = lowestCommonAncestorMethod(amauiNode.left); | ||
@@ -39,3 +37,3 @@ const right = lowestCommonAncestorMethod(amauiNode.right); | ||
maxDepth(amauiNode) { | ||
static maxDepth(amauiNode) { | ||
const maxDepthMethod = value => { | ||
@@ -49,25 +47,25 @@ if (value === undefined) return 0; | ||
get valid() { | ||
static valid(value) { | ||
const isValid = amauiNode => { | ||
if (amauiNode === undefined) return true; | ||
if (amauiNode.left && amauiNode.left.value >= amauiNode.value) return false; | ||
if (amauiNode.right && amauiNode.right.value <= amauiNode.value) return false; | ||
if (amauiNode.left?.value >= amauiNode.value) return false; | ||
if (amauiNode.right?.value <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(this.root); | ||
return isValid(value.root); | ||
} | ||
inorder(value, method) { | ||
static preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value); | ||
this.inorder(value.right, method); | ||
method(value, value.left, value.right); | ||
this.preorder(value.left, method); | ||
this.preorder(value.right, method); | ||
} | ||
} | ||
preorder(value, method) { | ||
static inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value); | ||
this.inorder(value.left, method); | ||
method(value, value.left, value.right); | ||
this.inorder(value.right, method); | ||
@@ -77,10 +75,37 @@ } | ||
postorder(value, method) { | ||
static postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
method(value); | ||
this.postorder(value.left, method); | ||
this.postorder(value.right, method); | ||
method(value, value.left, value.right); | ||
} | ||
} | ||
static min(value) { | ||
let amauiNode = value; | ||
while (amauiNode?.left !== undefined) amauiNode = amauiNode.left; | ||
return amauiNode; | ||
} | ||
static max(value) { | ||
let amauiNode = value; | ||
while (amauiNode?.right !== undefined) amauiNode = amauiNode.right; | ||
return amauiNode; | ||
} | ||
array(variant = 'preorder') { | ||
const value = []; | ||
if (AmauiBinaryTree[variant]) AmauiBinaryTree[variant](this.root, amauiNode => value.push(amauiNode.value)); | ||
return value; | ||
} | ||
build(value) { | ||
if (is('array', value)) value.forEach(item => this.add(item)); | ||
return this; | ||
} | ||
add(value) { | ||
@@ -123,9 +148,9 @@ const amauiNode = value instanceof AmauiNode ? value : new AmauiNode(value); | ||
let atmNode = this.root; | ||
let output; | ||
let amauiNode; | ||
while (atmNode && !output) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else output = atmNode; | ||
while (atmNode && !amauiNode) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else amauiNode = atmNode; | ||
} | ||
return output; | ||
return amauiNode; | ||
} | ||
@@ -141,33 +166,20 @@ | ||
if (value === amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return;else if (amauiNode.left === undefined) return amauiNode.right;else if (amauiNode.right === undefined) return amauiNode.left;else { | ||
let atmNode = this.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
} else if (value < amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return; | ||
if (amauiNode.left === undefined) return amauiNode.right; | ||
if (amauiNode.right === undefined) return amauiNode.left; | ||
const atmNode = AmauiBinaryTree.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
if (value < amauiNode.value) { | ||
amauiNode.left = this.removeNode(amauiNode.left, value); | ||
return amauiNode; | ||
} else { | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
} | ||
min(value) { | ||
let amauiNode = value; | ||
while (!amauiNode.left === undefined) amauiNode = amauiNode.left; | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
max(value) { | ||
let amauiNode = value; | ||
while (!amauiNode.right === undefined) amauiNode = amauiNode.right; | ||
return amauiNode; | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -3,0 +3,0 @@ * This source code is licensed under the MIT license found in the |
{ | ||
"name": "@amaui/amaui-binary-tree", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Binary tree", | ||
@@ -21,2 +21,4 @@ "repository": "https://github.com/amaui-org/amaui-binary-tree.git", | ||
"javascript", | ||
"js", | ||
"typescript", | ||
"node", | ||
@@ -23,0 +25,0 @@ "nodejs", |
@@ -51,6 +51,6 @@ | ||
// Add a amaui node / value | ||
amauiMinHeap.add(4); | ||
[4, 2, 7, 14, 1, 3, 5].map(value => amauiBinaryTree.add(value)); | ||
// You can also build a heap from array of values | ||
amauiMinHeap.build([10, 44, 54, 14, 31, 37, 24]); | ||
// or use a build method or a static method | ||
amauiBinaryTree.build([4, 2, 7, 14, 1, 3, 5]); | ||
@@ -62,7 +62,7 @@ // Binary tree | ||
2 7 | ||
/ \ / \ | ||
1 3 5 14 | ||
/ \ / \ | ||
1 3 5 14 | ||
// Remove value at any index | ||
amauiMinHeap.remove(2); | ||
// Remove any value | ||
amauiBinaryTree.remove(2); | ||
@@ -74,4 +74,4 @@ // Binary tree | ||
3 7 | ||
/ / \ | ||
1 5 14 | ||
/ / \ | ||
1 5 14 | ||
``` | ||
@@ -78,0 +78,0 @@ |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -238,8 +238,8 @@ * This source code is licensed under the MIT license found in the | ||
var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value) { | ||
var AmauiNode = /*#__PURE__*/_createClass(function AmauiNode(value, left, right) { | ||
_classCallCheck(this, AmauiNode); | ||
this.value = value; | ||
this.left = void 0; | ||
this.right = void 0; | ||
this.left = left; | ||
this.right = right; | ||
}); | ||
@@ -256,5 +256,5 @@ var AmauiBinaryTree = /*#__PURE__*/function () { | ||
value: function array() { | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inorder'; | ||
var variant = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'preorder'; | ||
var value = []; | ||
if (this[variant]) this[variant](this.root, function (amauiNode) { | ||
if (AmauiBinaryTree[variant]) AmauiBinaryTree[variant](this.root, function (amauiNode) { | ||
return value.push(amauiNode.value); | ||
@@ -265,71 +265,12 @@ }); | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor() { | ||
var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.root; | ||
var value = arguments.length > 1 ? arguments[1] : undefined; | ||
var value1 = arguments.length > 2 ? arguments[2] : undefined; | ||
var lca; | ||
key: "build", | ||
value: function build(value) { | ||
var _this = this; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value === null || value === void 0 ? void 0 : value.value) || amauiNode.value === (value1 === null || value1 === void 0 ? void 0 : value1.value); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
if (is('array', value)) value.forEach(function (item) { | ||
return _this.add(item); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
get: function get() { | ||
var isValid = function isValid(amauiNode) { | ||
if (amauiNode === undefined) return true; | ||
if (amauiNode.left && amauiNode.left.value >= amauiNode.value) return false; | ||
if (amauiNode.right && amauiNode.right.value <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(this.root); | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value); | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
this.inorder(value.right, method); | ||
method(value); | ||
} | ||
} | ||
}, { | ||
key: "add", | ||
@@ -374,9 +315,9 @@ value: function add(value) { | ||
var atmNode = this.root; | ||
var output; | ||
var amauiNode; | ||
while (atmNode && !output) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else output = atmNode; | ||
while (atmNode && !amauiNode) { | ||
if (value < atmNode.value) atmNode = atmNode.left;else if (value > atmNode.value) atmNode = atmNode.right;else amauiNode = atmNode; | ||
} | ||
return output; | ||
return amauiNode; | ||
} | ||
@@ -394,17 +335,93 @@ }, { | ||
if (value === amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return;else if (amauiNode.left === undefined) return amauiNode.right;else if (amauiNode.right === undefined) return amauiNode.left;else { | ||
var atmNode = this.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
} else if (value < amauiNode.value) { | ||
if (amauiNode.left === undefined && amauiNode.right === undefined) return; | ||
if (amauiNode.left === undefined) return amauiNode.right; | ||
if (amauiNode.right === undefined) return amauiNode.left; | ||
var atmNode = AmauiBinaryTree.min(amauiNode.right); | ||
amauiNode.value = atmNode.value; | ||
amauiNode.right = this.removeNode(amauiNode.right, atmNode.value); | ||
return amauiNode; | ||
} | ||
if (value < amauiNode.value) { | ||
amauiNode.left = this.removeNode(amauiNode.left, value); | ||
return amauiNode; | ||
} else { | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
amauiNode.right = this.removeNode(amauiNode.right, value); | ||
return amauiNode; | ||
} | ||
}], [{ | ||
key: "build", | ||
value: function build(value) { | ||
return new AmauiBinaryTree().build(value); | ||
} | ||
}, { | ||
key: "lowestCommonAncestor", | ||
value: function lowestCommonAncestor(value, value1, root) { | ||
var lca; | ||
var lowestCommonAncestorMethod = function lowestCommonAncestorMethod(amauiNode) { | ||
if (!amauiNode) return; | ||
var mid = amauiNode.value === (value instanceof AmauiNode ? value.value : value) || amauiNode.value === (value1 instanceof AmauiNode ? value1.value : value1); | ||
var left = lowestCommonAncestorMethod(amauiNode.left); | ||
var right = lowestCommonAncestorMethod(amauiNode.right); | ||
if (mid && left || mid && right || left && right) lca = amauiNode; | ||
return left || right || mid; | ||
}; | ||
lowestCommonAncestorMethod(root); | ||
return lca; | ||
} | ||
}, { | ||
key: "maxDepth", | ||
value: function maxDepth(amauiNode) { | ||
var maxDepthMethod = function maxDepthMethod(value) { | ||
if (value === undefined) return 0; | ||
return Math.max(1 + maxDepthMethod(value.left), 1 + maxDepthMethod(value.right)); | ||
}; | ||
return maxDepthMethod(amauiNode); | ||
} | ||
}, { | ||
key: "valid", | ||
value: function valid(value) { | ||
var isValid = function isValid(amauiNode) { | ||
var _amauiNode$left, _amauiNode$right; | ||
if (amauiNode === undefined) return true; | ||
if (((_amauiNode$left = amauiNode.left) === null || _amauiNode$left === void 0 ? void 0 : _amauiNode$left.value) >= amauiNode.value) return false; | ||
if (((_amauiNode$right = amauiNode.right) === null || _amauiNode$right === void 0 ? void 0 : _amauiNode$right.value) <= amauiNode.value) return false; | ||
return isValid(amauiNode.left) && isValid(amauiNode.right); | ||
}; | ||
return isValid(value.root); | ||
} | ||
}, { | ||
key: "preorder", | ||
value: function preorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
method(value, value.left, value.right); | ||
this.preorder(value.left, method); | ||
this.preorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "inorder", | ||
value: function inorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.inorder(value.left, method); | ||
method(value, value.left, value.right); | ||
this.inorder(value.right, method); | ||
} | ||
} | ||
}, { | ||
key: "postorder", | ||
value: function postorder(value, method) { | ||
if (value !== undefined && is('function', method)) { | ||
this.postorder(value.left, method); | ||
this.postorder(value.right, method); | ||
method(value, value.left, value.right); | ||
} | ||
} | ||
}, { | ||
key: "min", | ||
@@ -414,3 +431,5 @@ value: function min(value) { | ||
while (!amauiNode.left === undefined) { | ||
while (((_amauiNode = amauiNode) === null || _amauiNode === void 0 ? void 0 : _amauiNode.left) !== undefined) { | ||
var _amauiNode; | ||
amauiNode = amauiNode.left; | ||
@@ -426,3 +445,5 @@ } | ||
while (!amauiNode.right === undefined) { | ||
while (((_amauiNode2 = amauiNode) === null || _amauiNode2 === void 0 ? void 0 : _amauiNode2.right) !== undefined) { | ||
var _amauiNode2; | ||
amauiNode = amauiNode.right; | ||
@@ -429,0 +450,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/** @license AmauiBinaryTree v1.0.0 | ||
/** @license AmauiBinaryTree v1.0.1 | ||
* | ||
@@ -6,2 +6,2 @@ * This source code is licensed under the MIT license found in the | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AmauiBinaryTree={})}(this,(function(e){"use strict";function t(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function r(e,r,o){return r&&t(e.prototype,r),o&&t(e,o),Object.defineProperty(e,"prototype",{writable:!1}),e}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function i(e){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i(e)}function a(){return a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o])}return e},a.apply(this,arguments)}var u={elementIsObject:!1},s=function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:u,n=a({},u,o),s="object"===i(r)&&Object.getPrototypeOf(r);switch(t){case"string":return"string"==typeof r;case"number":return"number"==typeof r&&!Number.isNaN(r);case"boolean":return"boolean"==typeof r;case"array":return Array.isArray(r);case"object":var l="object"===i(r)&&!!r&&r.constructor===Object,f=e("element",r,n);return l&&(!f||n.elementIsObject);case"object-like":return"object"===i(r)&&(null===r||r.constructor!==Object);case"class":return("object"===i(r)||"function"==typeof r)&&(/class/gi.test(String(r))||/class/gi.test(String(null==r?void 0:r.constructor)));case"function":case"promise":return!!(r&&r instanceof Function);case"async":return!(!e("function",r)||!(c("browser")?"AsyncFunction"===r.constructor.name:r()instanceof Promise));case"map":return!(s!==Map.prototype);case"weakmap":return!(s!==WeakMap.prototype);case"set":return!(s!==Set.prototype);case"weakset":return!(s!==WeakSet.prototype);case"promise":return!(s!==Promise.prototype);case"int8array":return!(s!==Int8Array.prototype);case"uint8array":return!(s!==Uint8Array.prototype);case"uint8clampedarray":return!(s!==Uint8ClampedArray.prototype);case"int16array":return!(s!==Int16Array.prototype);case"uint16array":return!(s!==Uint16Array.prototype);case"int32array":return!(s!==Int32Array.prototype);case"uint32array":return!(s!==Uint32Array.prototype);case"float32array":return!(s!==Float32Array.prototype);case"float64array":return!(s!==Float64Array.prototype);case"dataview":return!(s!==DataView.prototype);case"arraybuffer":return!(s!==ArrayBuffer.prototype);case"symbol":return!("symbol"!==i(r));case"error":return!!(r&&r instanceof Error);case"date":return!!(r&&r instanceof Date);case"regexp":return!!(r&&r instanceof RegExp);case"arguments":return!(!r||"[object Arguments]"!==r.toString());case"null":return null===r;case"undefined":return void 0===r;case"element":return!(!r||!r.elementType&&!r.hasOwnProperty("$$typeof"));case"simple":return e("string",r,n)||e("number",r,n)||e("boolean",r,n)||e("undefined",r,n)||e("null",r,n)||e("element",r,n)&&!n.elementIsObject;case"not-array-object":return!e("array",r,n)&&!e("object",r,n);case"blob":return c("browser")&&r instanceof Blob;case"buffer":return!!(c("nodejs")&&r&&null!==r.constructor&&"function"==typeof r.constructor.isBuffer&&r.constructor.isBuffer(r));default:return!1}},c=function e(t){var r,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0;switch(t){case"browser":return"undefined"!=typeof window&&void 0!==window.document;case"worker":return"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope;case"nodejs":return!(void 0===n||"undefined"==typeof module||!module.exports);case"localhost":return r=void 0!==o?o:e("browser")&&window.location.hostname,s("string",r)&&["localhost","127.0.0.1"].some((function(e){return r.indexOf(e)>-1}));default:return!1}},l=r((function e(t){o(this,e),this.value=t,this.left=void 0,this.right=void 0})),f=function(){function e(){o(this,e),this.root=void 0}return r(e,[{key:"array",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"inorder",t=[];return this[e]&&this[e](this.root,(function(e){return t.push(e.value)})),t}},{key:"lowestCommonAncestor",value:function(){var e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.root,r=arguments.length>1?arguments[1]:void 0,o=arguments.length>2?arguments[2]:void 0,n=function t(n){if(n){var i=n.value===(null==r?void 0:r.value)||n.value===(null==o?void 0:o.value),a=t(n.left),u=t(n.right);return(i&&a||i&&u||a&&u)&&(e=n),a||u||i}};return n(t),e}},{key:"maxDepth",value:function(e){return function e(t){return void 0===t?0:Math.max(1+e(t.left),1+e(t.right))}(e)}},{key:"valid",get:function(){return function e(t){return void 0===t||!(t.left&&t.left.value>=t.value)&&(!(t.right&&t.right.value<=t.value)&&(e(t.left)&&e(t.right)))}(this.root)}},{key:"inorder",value:function(e,t){void 0!==e&&s("function",t)&&(this.inorder(e.left,t),t(e),this.inorder(e.right,t))}},{key:"preorder",value:function(e,t){void 0!==e&&s("function",t)&&(t(e),this.inorder(e.left,t),this.inorder(e.right,t))}},{key:"postorder",value:function(e,t){void 0!==e&&s("function",t)&&(this.inorder(e.left,t),this.inorder(e.right,t),t(e))}},{key:"add",value:function(e){var t=e instanceof l?e:new l(e);if(!this.root)return this.root=t,this;for(var r=this.root;r;){if(t.value===r.value)return this;if(t.value<r.value){if(void 0===r.left)return r.left=t,this;r=r.left}if(t.value>r.value){if(void 0===r.right)return r.right=t,this;r=r.right}}}},{key:"find",value:function(e){if(this.root){for(var t,r=this.root;r&&!t;)e<r.value?r=r.left:e>r.value?r=r.right:t=r;return t}}},{key:"remove",value:function(e){this.root=this.removeNode(this.root,e)}},{key:"removeNode",value:function(e,t){if(void 0!==e){if(t===e.value){if(void 0===e.left&&void 0===e.right)return;if(void 0===e.left)return e.right;if(void 0===e.right)return e.left;var r=this.min(e.right);return e.value=r.value,e.right=this.removeNode(e.right,r.value),e}return t<e.value?(e.left=this.removeNode(e.left,t),e):(e.right=this.removeNode(e.right,t),e)}}},{key:"min",value:function(e){for(var t=e;void 0===!t.left;)t=t.left;return t}},{key:"max",value:function(e){for(var t=e;void 0===!t.right;)t=t.right;return t}}]),e}();e.AmauiBinaryTree=f,e.AmauiNode=l,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).AmauiBinaryTree={})}(this,(function(e){"use strict";function t(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function r(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function i(e){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i(e)}function a(){return a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},a.apply(this,arguments)}var u={elementIsObject:!1},s=function e(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:u,o=a({},u,n),s="object"===i(r)&&Object.getPrototypeOf(r);switch(t){case"string":return"string"==typeof r;case"number":return"number"==typeof r&&!Number.isNaN(r);case"boolean":return"boolean"==typeof r;case"array":return Array.isArray(r);case"object":var f="object"===i(r)&&!!r&&r.constructor===Object,c=e("element",r,o);return f&&(!c||o.elementIsObject);case"object-like":return"object"===i(r)&&(null===r||r.constructor!==Object);case"class":return("object"===i(r)||"function"==typeof r)&&(/class/gi.test(String(r))||/class/gi.test(String(null==r?void 0:r.constructor)));case"function":case"promise":return!!(r&&r instanceof Function);case"async":return!(!e("function",r)||!(l("browser")?"AsyncFunction"===r.constructor.name:r()instanceof Promise));case"map":return!(s!==Map.prototype);case"weakmap":return!(s!==WeakMap.prototype);case"set":return!(s!==Set.prototype);case"weakset":return!(s!==WeakSet.prototype);case"promise":return!(s!==Promise.prototype);case"int8array":return!(s!==Int8Array.prototype);case"uint8array":return!(s!==Uint8Array.prototype);case"uint8clampedarray":return!(s!==Uint8ClampedArray.prototype);case"int16array":return!(s!==Int16Array.prototype);case"uint16array":return!(s!==Uint16Array.prototype);case"int32array":return!(s!==Int32Array.prototype);case"uint32array":return!(s!==Uint32Array.prototype);case"float32array":return!(s!==Float32Array.prototype);case"float64array":return!(s!==Float64Array.prototype);case"dataview":return!(s!==DataView.prototype);case"arraybuffer":return!(s!==ArrayBuffer.prototype);case"symbol":return!("symbol"!==i(r));case"error":return!!(r&&r instanceof Error);case"date":return!!(r&&r instanceof Date);case"regexp":return!!(r&&r instanceof RegExp);case"arguments":return!(!r||"[object Arguments]"!==r.toString());case"null":return null===r;case"undefined":return void 0===r;case"element":return!(!r||!r.elementType&&!r.hasOwnProperty("$$typeof"));case"simple":return e("string",r,o)||e("number",r,o)||e("boolean",r,o)||e("undefined",r,o)||e("null",r,o)||e("element",r,o)&&!o.elementIsObject;case"not-array-object":return!e("array",r,o)&&!e("object",r,o);case"blob":return l("browser")&&r instanceof Blob;case"buffer":return!!(l("nodejs")&&r&&null!==r.constructor&&"function"==typeof r.constructor.isBuffer&&r.constructor.isBuffer(r));default:return!1}},l=function e(t){var r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0;switch(t){case"browser":return"undefined"!=typeof window&&void 0!==window.document;case"worker":return"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope;case"nodejs":return!(void 0===o||"undefined"==typeof module||!module.exports);case"localhost":return r=void 0!==n?n:e("browser")&&window.location.hostname,s("string",r)&&["localhost","127.0.0.1"].some((function(e){return r.indexOf(e)>-1}));default:return!1}},f=r((function e(t,r,o){n(this,e),this.value=t,this.left=r,this.right=o})),c=function(){function e(){n(this,e),this.root=void 0}return r(e,[{key:"array",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"preorder",r=[];return e[t]&&e[t](this.root,(function(e){return r.push(e.value)})),r}},{key:"build",value:function(e){var t=this;return s("array",e)&&e.forEach((function(e){return t.add(e)})),this}},{key:"add",value:function(e){var t=e instanceof f?e:new f(e);if(!this.root)return this.root=t,this;for(var r=this.root;r;){if(t.value===r.value)return this;if(t.value<r.value){if(void 0===r.left)return r.left=t,this;r=r.left}if(t.value>r.value){if(void 0===r.right)return r.right=t,this;r=r.right}}}},{key:"find",value:function(e){if(this.root){for(var t,r=this.root;r&&!t;)e<r.value?r=r.left:e>r.value?r=r.right:t=r;return t}}},{key:"remove",value:function(e){this.root=this.removeNode(this.root,e)}},{key:"removeNode",value:function(t,r){if(void 0!==t){if(r===t.value){if(void 0===t.left&&void 0===t.right)return;if(void 0===t.left)return t.right;if(void 0===t.right)return t.left;var n=e.min(t.right);return t.value=n.value,t.right=this.removeNode(t.right,n.value),t}return r<t.value?(t.left=this.removeNode(t.left,r),t):(t.right=this.removeNode(t.right,r),t)}}}],[{key:"build",value:function(t){return(new e).build(t)}},{key:"lowestCommonAncestor",value:function(e,t,r){var n;return function r(o){if(o){var i=o.value===(e instanceof f?e.value:e)||o.value===(t instanceof f?t.value:t),a=r(o.left),u=r(o.right);return(i&&a||i&&u||a&&u)&&(n=o),a||u||i}}(r),n}},{key:"maxDepth",value:function(e){return function e(t){return void 0===t?0:Math.max(1+e(t.left),1+e(t.right))}(e)}},{key:"valid",value:function(e){return function e(t){var r,n;return void 0===t||!((null===(r=t.left)||void 0===r?void 0:r.value)>=t.value)&&(!((null===(n=t.right)||void 0===n?void 0:n.value)<=t.value)&&(e(t.left)&&e(t.right)))}(e.root)}},{key:"preorder",value:function(e,t){void 0!==e&&s("function",t)&&(t(e,e.left,e.right),this.preorder(e.left,t),this.preorder(e.right,t))}},{key:"inorder",value:function(e,t){void 0!==e&&s("function",t)&&(this.inorder(e.left,t),t(e,e.left,e.right),this.inorder(e.right,t))}},{key:"postorder",value:function(e,t){void 0!==e&&s("function",t)&&(this.postorder(e.left,t),this.postorder(e.right,t),t(e,e.left,e.right))}},{key:"min",value:function(e){for(var t=e;void 0!==(null===(r=t)||void 0===r?void 0:r.left);){var r;t=t.left}return t}},{key:"max",value:function(e){for(var t=e;void 0!==(null===(r=t)||void 0===r?void 0:r.right);){var r;t=t.right}return t}}]),e}();e.AmauiBinaryTree=c,e.AmauiNode=f,Object.defineProperty(e,"__esModule",{value:!0})})); |
51106
1133