@gabrielrufino/cube
Version:
Data structures made in Typescript
198 lines (197 loc) • 7.06 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var BinarySearchTreeNode_1 = __importDefault(require("./BinarySearchTreeNode"));
var BinarySearchTree = /** @class */ (function () {
function BinarySearchTree(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.inputs, inputs = _c === void 0 ? [] : _c, lessThanOrEqualTo = _b.lessThanOrEqualTo;
this._root = null;
this._size = 0;
if (lessThanOrEqualTo) {
this._lessThanOrEqualTo = lessThanOrEqualTo;
}
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
var value = inputs_1[_i];
this.insert(value);
}
this._size = inputs.length;
}
Object.defineProperty(BinarySearchTree.prototype, "data", {
get: function () {
return this._root
? __assign({}, this._root) : { left: null, value: null, right: null };
},
enumerable: false,
configurable: true
});
Object.defineProperty(BinarySearchTree.prototype, "size", {
get: function () {
return this._size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BinarySearchTree.prototype, "min", {
get: function () {
var _a;
var current = this._root;
while (current && current.left) {
current = current.left;
}
return (_a = current === null || current === void 0 ? void 0 : current.value) !== null && _a !== void 0 ? _a : null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BinarySearchTree.prototype, "max", {
get: function () {
var current = this._root;
while (current === null || current === void 0 ? void 0 : current.right) {
current = current.right;
}
return (current === null || current === void 0 ? void 0 : current.value) || null;
},
enumerable: false,
configurable: true
});
BinarySearchTree.prototype.insert = function (value) {
var node = new BinarySearchTreeNode_1.default(value);
if (this._root) {
this._insertChild(this._root, node);
}
else {
this._root = node;
}
this._size += 1;
return value;
};
BinarySearchTree.prototype.walkInOrder = function (callback) {
this._visitNodeInOrder(this._root, callback);
};
BinarySearchTree.prototype.walkPreOrder = function (callback) {
this._visitNodePreOrder(this._root, callback);
};
BinarySearchTree.prototype.walkPostOrder = function (callback) {
this._visitNodePostOrder(this._root, callback);
};
BinarySearchTree.prototype.search = function (value) {
var current = this._root;
while (current && current.value !== value) {
if (this._lessThanOrEqualTo(value, current.value)) {
current = current.left;
}
else {
current = current.right;
}
}
return Boolean(current);
};
BinarySearchTree.prototype.remove = function (value) {
var _a;
var current = this._root;
var path = [];
while (current && current.value !== value) {
if (this._lessThanOrEqualTo(value, current.value)) {
current = current.left;
path.push('left');
}
else {
current = current.right;
path.push('right');
}
}
var found = __assign({}, current);
var parent = path
.slice(0, path.length - 1)
.reduce(function (accumulator, current) { return accumulator[current]; }, this._root);
var child = path[path.length - 1];
if ((current === null || current === void 0 ? void 0 : current.left) && current.right && parent) {
var head = current.right;
while (head.left) {
head = head.left;
}
this.remove(head.value);
current.value = head.value;
}
else if ((current === null || current === void 0 ? void 0 : current.left) && parent) {
parent[child] = current.left;
this._size -= 1;
}
else if ((current === null || current === void 0 ? void 0 : current.right) && parent) {
parent[child] = current.right;
this._size -= 1;
}
else if (parent) {
parent[child] = null;
this._size -= 1;
}
return (_a = found.value) !== null && _a !== void 0 ? _a : null;
};
BinarySearchTree.prototype._lessThanOrEqualTo = function (value1, value2) {
if (value1 <= value2) {
return true;
}
return false;
};
BinarySearchTree.prototype._insertChild = function (father, child) {
if (this._lessThanOrEqualTo(child.value, father.value)) {
if (father.left) {
this._insertChild(father.left, child);
}
else {
father.left = child;
}
return;
}
if (father.right) {
this._insertChild(father.right, child);
}
else {
father.right = child;
}
};
BinarySearchTree.prototype._visitNodeInOrder = function (node, callback) {
if (node) {
this._visitNodeInOrder(node.left, callback);
callback(node.value);
this._visitNodeInOrder(node.right, callback);
}
};
BinarySearchTree.prototype._visitNodePreOrder = function (node, callback) {
if (node) {
callback(node.value);
this._visitNodePreOrder(node.left, callback);
this._visitNodePreOrder(node.right, callback);
}
};
BinarySearchTree.prototype._visitNodePostOrder = function (node, callback) {
if (node) {
this._visitNodePostOrder(node.left, callback);
this._visitNodePostOrder(node.right, callback);
callback(node.value);
}
};
BinarySearchTree.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: "".concat(this._root),
};
return primitives[type];
};
return BinarySearchTree;
}());
exports.default = BinarySearchTree;