Last active
May 3, 2022 15:01
-
-
Save guilsa/e8eda0d1e7bb2430b80f1287ea261d76 to your computer and use it in GitHub Desktop.
CS Data Structures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Min Heap | |
// since JS does not have a native heap, | |
// for an interview you can quickly code-up something like this | |
// letting interviewer know what you are doing | |
class MinHeap { | |
constructor(compareFunc) { | |
this.compareFunc = compareFunc; | |
this.heap = []; | |
} | |
insert(val) { | |
this.heap.unshift(val); | |
this.heap.sort(this.compareFunc); | |
} | |
extract() { | |
if (this.size === 0) return null; | |
return this.heap.shift(); | |
} | |
peek() { | |
if (this.size === 0) return null; | |
return this.heap[0]; | |
} | |
get size() { | |
return this.heap.length; | |
} | |
} | |
// Linked List Construction (2/14) | |
// set head -> if no head, set tail/head and return, otherwise insert before head | |
// set tail -> if no tail, set head and return, otherwise insert after tail | |
// implement in this order: | |
// search - value | |
// remove - node, value | |
// insert - before, after | |
// set - head, tail | |
// insert - at | |
// search/remove - time o(n) | space o(1) | |
// remove node - time o(1) | |
// insert - time o(1) | space o(1) | |
// for everything - space n(1) | |
class Node { | |
constructor(value) { | |
this.value = value; | |
this.prev = null; | |
this.next = null; | |
} | |
} | |
class DoublyLinkedList { | |
constructor() { | |
this.head = null; | |
this.tail = null; | |
} | |
setHead(node) { | |
if (this.head === null) { | |
this.head = node; | |
this.tail = node; | |
return; | |
} | |
this.insertBefore(this.head, node); | |
} | |
setTail(node) { | |
if (this.tail === null) { | |
this.setHead(node); | |
return; | |
} else { | |
this.insertAfter(this.tail, node); | |
} | |
} | |
insertBefore(node, nodeToInsert) { | |
if (nodeToInsert === this.head && nodeToInsert === this.tail) return; | |
this.remove(nodeToInsert); | |
nodeToInsert.prev = node.prev; | |
nodeToInsert.next = node; | |
if (node.prev === null) { | |
this.head = nodeToInsert; | |
} else { | |
node.prev.next = nodeToInsert; | |
} | |
node.prev = nodeToInsert; | |
} | |
insertAfter(node, nodeToInsert) { | |
if (nodeToInsert === this.head && nodeToInsert === this.tail) return; | |
this.remove(nodeToInsert); | |
nodeToInsert.prev = node; | |
nodeToInsert.next = node.next; | |
if (node.next === null) { | |
this.tail = nodeToInsert; | |
} else { | |
node.next.prev = nodeToInsert; | |
} | |
node.next = nodeToInsert; | |
} | |
insertAtPosition(position, nodeToInsert) { | |
if (position === 1) { | |
this.setHead(nodeToInsert); | |
return; | |
} | |
let node = this.head; | |
let currentPosition = 1; | |
while (node !== null && currentPosition++ !== position) node = node.next; | |
if (node !== null) { | |
this.insertBefore(node, nodeToInsert); | |
} else { | |
this.setTail(nodeToInsert); | |
} | |
} | |
removeNodesWithValue(value) { | |
let node = this.head; | |
while (node !== null) { | |
let nodeToRemove = node; | |
node = node.next; | |
if (nodeToRemove.value === value) this.remove(nodeToRemove); | |
} | |
} | |
remove(node) { | |
if (node === this.head) this.head = this.head.next; | |
if (node === this.tail) this.tail = this.tail.prev; | |
this.removeBindings(node); | |
} | |
removeBindings(node) { | |
if (node.prev !== null) node.prev.next = node.next; | |
if (node.next !== null) node.next.prev = node.prev; | |
node.prev = null; | |
node.next = null; | |
} | |
containsNodeWithValue(value) { | |
let node = this.head; | |
while (node !== null && node.value !== value) node = node.next; | |
return node !== null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment