Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"standard"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": [
"error",
{
"classes": false,
"variables": true,
"typedefs": false
}
]
}
}
2 changes: 1 addition & 1 deletion dist/Executors/problemSolvingExec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = exec;
const arrays_1 = require("./types/arrays");
async function exec(strategy, params) {
const executor = new arrays_1.contextExecutor(strategy);
const executor = new arrays_1.ContextExecutor(strategy);
const result = await executor.execute(params);
console.log(`Executed-${strategy.constructor.name} and Result: ${result}`);
}
6 changes: 3 additions & 3 deletions dist/Executors/types/arrays.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.engineeringContextExecutor = exports.contextExecutor = void 0;
class contextExecutor {
exports.engineeringContextExecutor = exports.ContextExecutor = void 0;
class ContextExecutor {
constructor(Strategy) {
this.strategy = Strategy;
}
execute(params) {
return this.strategy.contextFunction(params);
}
}
exports.contextExecutor = contextExecutor;
exports.ContextExecutor = ContextExecutor;
class engineeringContextExecutor {
constructor(Strategy) {
this.strategy = Strategy;
Expand Down
17 changes: 4 additions & 13 deletions dist/modules/engineeringDrills/binaryTree.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryTree = void 0;
const _1 = require(".");
//Construct Tree
const root = new _1.TreeNode(1);
root.left = new _1.TreeNode(2);
root.right = new _1.TreeNode(3);
root.left.left = new _1.TreeNode(4);
root.left.right = new _1.TreeNode(5);
root.right.right = new _1.TreeNode(6);
//Traverse the Tree
class BinaryTree {
constructor(root = null) {
this.root = root;
}
preOrder(node = this.root, result) {
if (!node)
return result;
result.push(node.val);
result.push(node.value);
this.preOrder(node.left, result);
this.preOrder(node.right, result);
return result;
Expand All @@ -27,14 +18,14 @@ class BinaryTree {
return result;
this.postOrder(node.right, result);
this.postOrder(node.left, result);
result.push(node.val);
result.push(node.value);
return result;
}
inOrder(node = this.root, result) {
if (!node)
return result;
this.inOrder(node.left, result);
result.push(node.val);
result.push(node.value);
this.inOrder(node.right, result);
return result;
}
Expand All @@ -45,7 +36,7 @@ class BinaryTree {
queue.push(node);
while (queue.length > 0) {
const currentNode = queue.shift();
result.push(currentNode ? currentNode.val : 0);
result.push(currentNode ? currentNode.value : 0);
if (currentNode?.left)
queue.push(currentNode.left);
if (currentNode?.right)
Expand Down
86 changes: 86 additions & 0 deletions dist/modules/engineeringDrills/class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.heap = void 0;
class heap {
//Class constructor
constructor(compareFn) {
this.data = [];
this.compare = compareFn;
}
insert(element) {
this.data.push(element);
this.bubbleUp(this.data.length - 1);
}
peek() {
return this.data[0];
}
remove(element) {
const index = this.data.findIndex((elem) => elem == element);
if (index == -1)
return;
const last = this.data.pop();
if (index < this.data.length && last) {
this.data[index] = last;
this.bubbleUp(index);
this.bubbleDown(index);
}
}
poll() {
if (this.data.length === 0)
return undefined;
const root = this.data[0];
const last = this.data.pop();
if (this.data.length > 0 && last) {
this.data[0] = last;
this.bubbleDown(0);
}
return root;
}
size() {
return this.data.length;
}
//Private Helpers
parent(index) {
return Math.floor((index - 1) / 2);
}
left(index) {
return index * 2 + 1;
}
right(index) {
return index * 2 + 2;
}
bubbleUp(index) {
while (index > 0) {
const parent = this.parent(index);
if (this.compare(this.data[index], this.data[parent]) < 0) {
[this.data[index], this.data[parent]] = [this.data[parent], this.data[index]];
index = parent;
}
else {
break;
}
}
}
bubbleDown(index) {
const length = this.data.length;
while (true) {
const leftIndex = this.left(index);
const rightIndex = this.right(index);
let effecientIndex = index;
if (leftIndex < length && this.compare(this.data[leftIndex], this.data[effecientIndex]) < 0) {
effecientIndex = leftIndex;
}
if (rightIndex < length && this.compare(this.data[rightIndex], this.data[effecientIndex]) < 0) {
effecientIndex = rightIndex;
}
if (effecientIndex !== index) {
[this.data[index], this.data[effecientIndex]] = [this.data[effecientIndex], this.data[index]];
index = effecientIndex;
}
else {
break;
}
}
}
}
exports.heap = heap;
24 changes: 22 additions & 2 deletions dist/modules/engineeringDrills/helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTree = getTree;
function getTree(node, count) {
exports.getRoot = getRoot;
const helperClasses_1 = require("./helperClasses");
function getRoot(array) {
if (!array.length || array[0] == null)
return null;
const root = new helperClasses_1.TreeNode(array[0]);
const queue = [root];
let i = 1;
while (i < queue.length) {
const current = queue.shift();
if (!current)
continue;
const left = array[i++];
const right = array[i++];
if (left && right) {
current.left = new helperClasses_1.TreeNode(left);
current.right = new helperClasses_1.TreeNode(right);
queue.push(current.left);
queue.push(current.right);
}
}
return root;
}
94 changes: 94 additions & 0 deletions dist/modules/engineeringDrills/helperClasses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Heap = exports.TreeNode = void 0;
class TreeNode {
constructor(val) {
this.value = val;
this.left = null;
this.right = null;
}
}
exports.TreeNode = TreeNode;
class Heap {
// Class constructor
constructor(compareFn) {
this.data = [];
this.compare = compareFn;
}
insert(element) {
this.data.push(element);
this.bubbleUp(this.data.length - 1);
}
peek() {
return this.data[0];
}
remove(element) {
const index = this.data.findIndex((elem) => elem === element);
if (index === -1)
return;
const last = this.data.pop();
if (index < this.data.length && last) {
this.data[index] = last;
this.bubbleUp(index);
this.bubbleDown(index);
}
}
poll() {
if (this.data.length === 0)
return undefined;
const root = this.data[0];
const last = this.data.pop();
if (this.data.length > 0 && last) {
this.data[0] = last;
this.bubbleDown(0);
}
return root;
}
size() {
return this.data.length;
}
// Private Helpers
parent(index) {
return Math.floor((index - 1) / 2);
}
left(index) {
return index * 2 + 1;
}
right(index) {
return index * 2 + 2;
}
bubbleUp(index) {
while (index > 0) {
const parent = this.parent(index);
if (this.compare(this.data[index], this.data[parent]) < 0) {
[this.data[index], this.data[parent]] = [this.data[parent], this.data[index]];
index = parent;
}
else {
break;
}
}
}
bubbleDown(index) {
const length = this.data.length;
while (true) {
const leftIndex = this.left(index);
const rightIndex = this.right(index);
let effecientIndex = index;
if (leftIndex < length && this.compare(this.data[leftIndex], this.data[effecientIndex]) < 0) {
effecientIndex = leftIndex;
}
if (rightIndex < length && this.compare(this.data[rightIndex], this.data[effecientIndex]) < 0) {
effecientIndex = rightIndex;
}
if (effecientIndex !== index) {
[this.data[index], this.data[effecientIndex]] = [this.data[effecientIndex], this.data[index]];
index = effecientIndex;
}
else {
break;
}
}
}
}
exports.Heap = Heap;
31 changes: 12 additions & 19 deletions dist/modules/engineeringDrills/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeNode = void 0;
const binaryTree_1 = require("./binaryTree");
const helperClasses_1 = require("./helperClasses");
const taskManager_1 = require("./taskManager");
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
exports.TreeNode = TreeNode;
//Construct Tree
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(6);
//class instances
// Construct Tree
const root = new helperClasses_1.TreeNode(1);
root.left = new helperClasses_1.TreeNode(2);
root.right = new helperClasses_1.TreeNode(3);
root.left.left = new helperClasses_1.TreeNode(4);
root.left.right = new helperClasses_1.TreeNode(5);
root.right.right = new helperClasses_1.TreeNode(6);
// class instances
const tree = new binaryTree_1.BinaryTree();
const manager = new taskManager_1.TaskManager([
[1, 10, 5],
[2, 11, 10],
[3, 12, 3]
[3, 12, 3],
[0, 21, 10]
]);
//Function calls
// Function calls
const result = tree.levelOrder(root, []);
const exeTask = manager.execTop();
console.log(`Binary Traversal Result: ${result}`);
Expand Down
Loading