Skip to content
Open
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
129 changes: 129 additions & 0 deletions algorithm/BinarySearchTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}

class BinarySearchTree {
constructor() {
this.root = null;
}

insert(value) {
const newNode = new Node(value);

if (this.root === null) {
this.root = newNode;
return newNode;
}

let currentNode = this.root;

while (currentNode !== null) {
if (value === currentNode.value) {
return null;
}

if (value < currentNode.value) {
if (currentNode.left === null) {
currentNode.left = newNode;
return newNode;
}

currentNode = currentNode.left;
} else {
if (currentNode.right === null) {
currentNode.right = newNode;
return newNode;
}

currentNode = currentNode.right;
}
}

return null;
}

find(value) {
let currentNode = this.root;

while (currentNode !== null) {
if (value === currentNode.value) {
return currentNode;
}

if (value < currentNode.value) {
currentNode = currentNode.left;
} else {
currentNode = currentNode.right;
}
}

return null;
}

remove(value) {
const removeNode = (node, targetValue) => {
if (node === null) {
return null;
}

if (targetValue < node.value) {
node.left = removeNode(node.left, targetValue);
return node;
}

if (targetValue > node.value) {
node.right = removeNode(node.right, targetValue);
return node;
}

if (node.left === null) {
return node.right;
}

if (node.right === null) {
return node.left;
}

let minimumNode = node.right;

while (minimumNode.left !== null) {
minimumNode = minimumNode.left;
}

node.value = minimumNode.value;
node.right = removeNode(node.right, minimumNode.value);
return node;
};

this.root = removeNode(this.root, value);
}
}

module.exports = BinarySearchTree;

if (require.main === module) {
const bst = new BinarySearchTree();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(3);
bst.insert(7);
bst.insert(12);
bst.insert(18);

console.log("find 7:", bst.find(7));
console.log("find 100:", bst.find(100));

bst.remove(3);
console.log("after remove leaf 3:", JSON.stringify(bst));

bst.remove(15);
console.log("after remove 15:", JSON.stringify(bst));

bst.remove(10);
console.log("after remove root 10:", JSON.stringify(bst));
}
123 changes: 123 additions & 0 deletions algorithm/DoublyLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
class Node {
constructor(value) {
this.value = value;
this.prev = null;
this.next = null;
}
}

class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}

addToHead(value) {
const newNode = new Node(value);

if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}

this.length++;
return newNode;
}

addToTail(value) {
const newNode = new Node(value);

if (this.tail === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}

this.length++;
return newNode;
}

insertAfter(targetValue, newValue) {
const targetNode = this.findNode(targetValue);

if (targetNode === null) {
return null;
}

const newNode = new Node(newValue);
newNode.prev = targetNode;
newNode.next = targetNode.next;

if (targetNode.next !== null) {
targetNode.next.prev = newNode;
} else {
this.tail = newNode;
}

targetNode.next = newNode;
this.length++;
return newNode;
}

findNode(value) {
let currentNode = this.head;

while (currentNode !== null) {
if (currentNode.value === value) {
return currentNode;
}

currentNode = currentNode.next;
}

return null;
}

removeNode(value) {
const removedNode = this.findNode(value);

if (removedNode === null) {
return null;
}

if (removedNode.prev !== null) {
removedNode.prev.next = removedNode.next;
} else {
this.head = removedNode.next;
}

if (removedNode.next !== null) {
removedNode.next.prev = removedNode.prev;
} else {
this.tail = removedNode.prev;
}

removedNode.prev = null;
removedNode.next = null;
this.length--;
return removedNode;
}
}

module.exports = DoublyLinkedList;

if (require.main === module) {
const list = new DoublyLinkedList();
list.addToHead(2);
list.addToHead(1);
list.addToTail(3);
list.insertAfter(2, 4);
console.log("find 4:", list.findNode(4));
console.log("remove 1:", list.removeNode(1));
console.log("remove 3:", list.removeNode(3));
console.log("head:", list.head);
console.log("tail:", list.tail);
}
95 changes: 95 additions & 0 deletions algorithm/LinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}

addNode(value) {
const newNode = new Node(value);

if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}

this.length++;
return newNode;
}

findNode(value) {
let currentNode = this.head;

while (currentNode !== null) {
if (currentNode.value === value) {
return currentNode;
}

currentNode = currentNode.next;
}

return null;
}

insertAfter(targetValue, newValue) {
const targetNode = this.findNode(targetValue);

if (targetNode === null) {
return null;
}

const newNode = new Node(newValue);
newNode.next = targetNode.next;
targetNode.next = newNode;

if (targetNode === this.tail) {
this.tail = newNode;
}

this.length++;
return newNode;
}

removeAfter(targetValue) {
const targetNode = this.findNode(targetValue);

if (targetNode === null || targetNode.next === null) {
return null;
}

const removedNode = targetNode.next;
targetNode.next = removedNode.next;

if (removedNode === this.tail) {
this.tail = targetNode;
}

removedNode.next = null;
this.length--;
return removedNode;
}
}

module.exports = LinkedList;

if (require.main === module) {
const list = new LinkedList();
list.addNode(1);
list.addNode(2);
list.addNode(3);
console.log("find 2:", list.findNode(2));
list.insertAfter(2, 4);
console.log("after insert:", JSON.stringify(list));
console.log("removed:", list.removeAfter(2));
console.log("after remove:", JSON.stringify(list));
}
43 changes: 43 additions & 0 deletions algorithm/Queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Queue {
constructor() {
this.items = [];
}

enqueue(value) {
this.items.push(value);
}

dequeue() {
if (this.isEmpty()) {
return null;
}

return this.items.shift();
}

peek() {
if (this.isEmpty()) {
return null;
}

return this.items[0];
}

isEmpty() {
return this.items.length === 0;
}
}

module.exports = Queue;

if (require.main === module) {
const queue = new Queue();
console.log(queue.isEmpty());
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
console.log(queue.peek());
console.log(queue.dequeue());
console.log(queue.dequeue());
console.log(queue.isEmpty());
}
Loading