Array used
class Stack {
constructor() {
this.stack = [];
}
push(element) {
this.stack.push(element);
console.log(element + ' pushed');
}
pop() {
let stackLength = this.stack.length;
if (stackLength != 0) {
console.log(this.stack[stackLength - 1] + ' popped');
return this.stack.pop();
} else return undefined;
}
peek() {
let stackLength = this.stack.length;
if (stackLength != 0) {
console.log(this.stack[stackLength - 1] + ' picked');
return this.stack[stackLength - 1];
}
}
}
Node used
class ListNode {
constructor(x) {
this.value = x;
this.next = null;
}
}
class Queue {
constructor() {
this.queue;
}
printList(head = this.queue) {
let selectedNode = head;
do {
console.log(`current = ${selectedNode.value} next = ${selectedNode.next ? selectedNode.next.value : null}`)
selectedNode = selectedNode.next;
}
while (selectedNode)
}
getUnderlyingList() {
return this.queue;
}
getLast() {
let selectedNode = this.queue
let head = this.queue
while (selectedNode) {
if (selectedNode) {
head = selectedNode;
}
selectedNode = selectedNode.next;
}
return head;
}
enqueue(value) {
let lastNode = this.getLast();
let newNode = new ListNode;
newNode.value = value;
if (lastNode) {
lastNode.next = newNode
} else {
this.queue = newNode;
}
}
dequeue() {
let deletedItem = this.queue.value;
this.queue = this.queue.next;
return deletedItem;
}
}
Node used
function createList(arr) {
let currentNode;
let head;
for (let i = 0; i < arr.length; i++) {
if (currentNode) {
let node = new ListNode;
node.value = arr[i];
currentNode.next = node;
currentNode = node;
} else {
let node = new ListNode;
node.value = arr[i];
currentNode = node;
if (!head) {
head = node;
}
}
}
return head;
}
function printList(head) {
let selectedNode = head;
do {
console.log(`current = ${selectedNode.value} next = ${selectedNode.next ? selectedNode.next.value : null}`)
selectedNode = selectedNode.next;
}
while (selectedNode)
}
function nodeToArr(head) {
let selectedNode = head;
let result = [];
do {
result.push(selectedNode.value);
selectedNode = selectedNode.next;
}
while (selectedNode)
return result;
}
function removeKFromList(list, num) {
let head = list;
let selectedNode = list;
let prevNode = list;
do {
if (selectedNode.value === num) {
if (prevNode == selectedNode) {
head = selectedNode.next;
} else {
prevNode.next = selectedNode.next;
}
} else {
prevNode = selectedNode;
}
selectedNode = selectedNode.next;
}
while (selectedNode)
return head;
}
Binary Node used
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor(data) {
this.tree = null;
}
root() {
return this.tree;
}
add(data) {
this.tree = addMethod(data, this.tree);
function addMethod(data, node) {
if (!node) {
return new Node(data);
}
if (node.data === data) {
return node;
}
if (data < node.data) {
node.left = addMethod(data, node.left)
} else {
node.right = addMethod(data, node.right)
}
return node;
}
}
has(data) {
return hasData(data, this.tree);
function hasData(data, node) {
if (!node) {
return false
}
if (node.data === data) {
return true
}
if (data < node.data) {
return hasData(data, node.left);
} else {
return hasData(data, node.right);
}
}
}
find(data) {
return findData(data, this.tree);
function findData(data, node) {
if (!node) {
return null
}
if (node.data === data) {
return node
}
if (data < node.data) {
return findData(data, node.left);
} else {
return findData(data, node.right);
}
}
}
remove(data) {
this.tree = removeData(data, this.tree);
function removeData(data, node) {
if (!node) {
return null;
}
if (data < node.data) {
node.left = removeData(data, node.left);
return node;
} else if (data > node.data) {
node.right = removeData(data, node.right)
return node;
} else {
if (!node.left && !node.right) {
return null;
}
if (!node.left) {
node = node.right;
return node;
}
if (!node.right) {
node = node.left;
return node;
}
let minFromRight = node.right;
while (minFromRight.left) {
minFromRight = minFromRight.left;
}
node.data = minFromRight.data;
node.right = removeData(minFromRight.data, node.right)
return node;
}
}
}
min() {
if (!this.tree) {
return;
}
let node = this.tree;
while (node.left) {
node = node.left;
}
return node.data;
}
max() {
if (!this.tree) {
return;
}
let node = this.tree;
while (node.right) {
node = node.right;
}
return node.data;
}
}