Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
fazeelanizam13 / javascript-bfs.js
Created May 30, 2022 06:53
Implementation of breadth-first search algorithm on a binary search tree in JavaScript
class Node {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.left = null
this.right = null
}
}
class Tree {
@fazeelanizam13
fazeelanizam13 / javascript-dfs.js
Created May 29, 2022 14:54
Implementation of depth-first search algorithm on a binary search tree in JavaScript
class Node {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.left = null
this.right = null
}
}
class Tree {
@fazeelanizam13
fazeelanizam13 / javascript-max-heap.js
Created May 29, 2022 07:22
Implementation of a (max) heap data structure in JavaScript
class MaxHeap {
// creates an array of given length
constructor(length) {
this.data = new Array(length)
this.size = 0
}
// helpers
rehash = () => {
@fazeelanizam13
fazeelanizam13 / javascript-min-heap.js
Last active May 29, 2022 07:15
Implementation of a (min) heap data structure in JavaScript
class MinHeap {
// creates an array of given length
constructor(length) {
this.data = new Array(length)
this.size = 0
}
// helpers
rehash = () => {
@fazeelanizam13
fazeelanizam13 / javascript-bst.js
Last active May 29, 2022 14:39
Implementation of a binary search tree in JavaScript
class Node {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.left = null
this.right = null
}
}
class Tree {
@fazeelanizam13
fazeelanizam13 / javascript-queue.js
Created May 25, 2022 19:15
Implementation of the queue data structure in JavaScript
class Item {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.next = null
}
}
class Queue {
// initial size is 0 and initially just has two reference items that store nothing and point to nothing
@fazeelanizam13
fazeelanizam13 / javascript-stack.js
Last active May 25, 2022 19:15
Implementation of the stack data structure in JavaScript.
class Item {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.next = null
}
}
class Stack {
// initial size is 0 and initially just has a head node that stores nothing and points to nothing
@fazeelanizam13
fazeelanizam13 / javascript-linked-list.js
Last active May 25, 2022 16:19
Implementation of the linked list data structure in JavaScript.
class Node {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.next = null
}
}
class LinkedList {
// initial size is 0 and initially just has a head node that stores nothing and points to nothing
@fazeelanizam13
fazeelanizam13 / javascript-hash-table.js
Created May 21, 2022 15:00
Implementation of the hash table data structure in JavaScript.
const hashFunction = (key, tableSize) => {
// index we are going to return
let index = 0
// get list of characters in key string as an array
let numChars = String(key)
// assign numChars size to index to start
index = numChars.length
// assumption - each input would have exactly one solution, and the same element may not be used twice in the input array
// brute force solution
function twoSumNaive(array, sum) {
let indices = []
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === sum) {
indices.push(i)