Skip to content

Instantly share code, notes, and snippets.

View tdubs42's full-sized avatar
🐘
constantly coding

tdubs tdubs42

🐘
constantly coding
View GitHub Profile
@tdubs42
tdubs42 / minHeap-class.js
Created February 23, 2024 18:18
MinHeap Class - JavaScript
class MinHeap {
constructor() {
this.heap = [ null ];
this.size = 0;
}
popMin() {
if (this.size === 0) {
return null
}
@tdubs42
tdubs42 / binary-tree-class.js
Created February 21, 2024 18:54
Binary Tree Class - JavaScript
class BinaryTree {
constructor(value, depth = 1) {
this.value = value;
this.depth = depth;
this.left = null;
this.right = null;
}
insert(value) {
if (value < this.value) {
@tdubs42
tdubs42 / binary-search-sorted-list.js
Created February 20, 2024 00:45
Binary Search Function for Sorted List - JavaScript
const binarySearch = (arr, target) => {
let left = 0;
let right = arr.length;
while (right > left) {
const indexToCheck = Math.floor((left + right) / 2);
const checking = arr[indexToCheck];
console.log(indexToCheck);
if (checking === target) {
@tdubs42
tdubs42 / hash-map-class.js
Created February 19, 2024 06:43
Hash Map Class - JavaScript
const LinkedList = require('./LinkedList');
const Node = require('./Node');
class HashMap {
constructor(size = 0) {
this.hashmap = new Array(size)
.fill(null)
.map(() => new LinkedList());
}
hash(key) {
@tdubs42
tdubs42 / hash-map-for-dummbies.md
Created February 19, 2024 05:58
Hash Map for Dummies - JavaScript

Hash map: A key-value store that uses an array and a hashing function to save and retrieve values. Key: The identifier given to a value for later retrieval. Hash function: A function that takes some input and returns a number. Compression function: A function that transforms its inputs into some smaller range of possible outputs.

Recipe for saving to a hash table:

  • Take the key and plug it into the hash function, getting the hash code.
  • Modulo that hash code by the length of the underlying array, getting an array index.
  • Check if the array at that index is empty, if so, save the value (and the key) there.
  • If the array is full at that index continue to the next possible position depending on your collision strategy.
@tdubs42
tdubs42 / stack-class.js
Created February 19, 2024 04:37
Stack Class - JavaScript
const LinkedList = require('./LinkedList');
class Stack {
constructor(maxSize = Infinity) {
this.stack = new LinkedList();
this.maxSize = maxSize;
this.size = 0;
}
// Add helper methods below this line
@tdubs42
tdubs42 / queue-class.js
Created February 19, 2024 02:55
Queue Class - JavaScript
const LinkedList = require("./LinkedList");
class Queue {
constructor(maxSize = Infinity) {
this.queue = new LinkedList();
this.maxSize = maxSize;
this.size = 0;
}
isEmpty() {
@tdubs42
tdubs42 / node-class-single.js
Created February 19, 2024 01:30
Node Class for Linked List - JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
setNextNode(node) {
if (node instanceof Node || node === null) {
this.next = node;
} else {
@tdubs42
tdubs42 / linked-list-class.js
Last active February 19, 2024 18:51
Linked List Class - JavaScript
const Node = require('./Node');
class LinkedList {
constructor() {
this.head = null;
}
addToHead(data) {
const newHead = new Node(data);
const currentHead = this.head;
@tdubs42
tdubs42 / doubly-linked-list-class.js
Last active February 19, 2024 01:30
Doubly Linked List Class - JavaScript
const Node = require('./Node');
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
addToHead(data) {
const newHead = new Node(data);