adt-legacy-js
Version:
Faster way to implement Auxiliary Data Types
165 lines (158 loc) • 4.37 kB
JavaScript
class BSTNode {
constructor(data, left, right) {
this.data = data;
this.left = null;
this.right = null;
}
async show() {
return this.data;
}
}
class BSTClass {
constructor() {
this.root = null;
}
/**
*
* @param {*} data
*/
async insert(data) {
let n = new BSTNode(data, null, null);
if (this.root == null) {
this.root = n;
} else {
let current = this.root;
let parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current == null) {
parent.left = n;
break;
}
} else {
current = current.right;
if (current == null) {
parent.right = n;
break;
}
}
}
}
}
/**
* @returns {Promise<Boolean>}
*/
async flush() {
this.root = null;
return true;
}
/**
*
* @param {*} node
* @returns {Promise<Array>}
*/
async inOrder(node, inOrderArr = []) {
if (!(node == null)) {
await this.inOrder(node.left, inOrderArr);
inOrderArr.push(await node.show());
await this.inOrder(node.right, inOrderArr);
}
return inOrderArr;
}
/**
*
* @param {*} node
* @returns {Promise<Array>}
*/
async preOrder(node, preOrderArr = []) {
if (!(node == null)) {
preOrderArr.push(await node.show());
await this.preOrder(node.left, preOrderArr);
await this.preOrder(node.right, preOrderArr);
}
return preOrderArr;
}
/**
*
* @param {*} node
* @returns {Promise<Array>}
*/
async postOrder(node, postOrderArr = []) {
if (!(node == null)) {
await this.postOrder(node.left, postOrderArr);
await this.postOrder(node.right);
postOrderArr.push(await node.show());
}
return postOrderArr;
}
async #gothroughNode(node, level, dataMap) {
if (!(node == null)) {
const fetchLevel = dataMap.get(level) || [];
const nodeData = await node.show();
dataMap.set(level, [...fetchLevel, nodeData]);
await this.#gothroughNode(node.left, level + 1, dataMap);
await this.#gothroughNode(node.right, level + 1, dataMap);
}
return dataMap;
}
/**
*
* @param {*} node
* @returns {Promise<any>}
*/
async getMin(node) {
var current = node;
while (!(current.left == null)) {
current = current.left;
}
return current;
}
async #removeNode(node, data) {
if (node == null) {
return null;
}
if (data == node.data) {
// node has no children
if (node.left == null && node.right == null) {
return null;
}
// node has no left child
if (node.left == null) {
return node.right;
}
// node has no right child
if (node.right == null) {
return node.left;
}
// node has two children
var tempNode = await this.getMin(node.right);
node.data = tempNode.data;
node.right = await this.#removeNode(node.right, tempNode.data);
return node;
} else if (data < node.data) {
node.left = await this.#removeNode(node.left, data);
return node;
} else {
node.right = await this.#removeNode(node.right, data);
return node;
}
}
async remove(data) {
this.root = await this.#removeNode(this.root, data);
console.log(this.root);
}
/**
*
* @param {*} node
* @returns {Promise<Map>}
*/
async linearOrder(node) {
let level = 0;
let dataMap = new Map();
let dataFinal = await this.#gothroughNode(node, level, dataMap);
return dataFinal;
}
}
module.exports = BSTClass;