Skip to content

Instantly share code, notes, and snippets.

View Giagnus64's full-sized avatar

Gianfranco Nuschese Giagnus64

View GitHub Profile
@Giagnus64
Giagnus64 / app.js
Created March 12, 2020 03:07
Priority Queue or Min Binary Heap
class Node{
constructor(value, priority){
this.value = value
this.priority = priority
}
}
class PriorityQueue{
constructor(){
@Giagnus64
Giagnus64 / app.js
Created March 12, 2020 02:29
Binary Search Heap removeMax
//bubble down elements to readjust heap after removing max element
bubbleDown(){
let parentIndex = 0;
const length = this.values.length;
const element = this.values[0];
//loop breaks if no swaps are needed
while (true){
//get indexes of child elements by following formula
let leftChildIndex = (2 * parentIndex) + 1;
let rightChildIndex = (2 * parentIndex) + 2;
@Giagnus64
Giagnus64 / app.js
Last active March 12, 2020 02:39
Binary Search Heap
class MaxBinaryHeap{
constructor(){
this.values = []
}
//helper method that swaps the values and two indexes of an array
swap(index1, index2){
let temp = this.values[index1];
this.values[index1] = this.values[index2];
this.values[index2] = temp;
@Giagnus64
Giagnus64 / app.js
Created February 26, 2020 22:11
DFS without helpers
traverseDFS(type) {
//if there is no root, return false
if (!this.root) {
return false;
}
//make a variable for tree values
const treeValues = [];
//current values always starts at root
let current = this.root;
@Giagnus64
Giagnus64 / app.js
Last active February 26, 2020 22:32
inOrder helper for DFS
const inOrderHelper = node => {
//if node had children, split nodes into left and right halves in case tree is not binary FIRST
if (node.children.length !== 0) {
//get halfway point
const halfway = Math.floor(node.children.length / 2);
//recursively call function on all node children left of halfway point
for (let i = 0; i < halfway; i++) {
inOrderHelper(node.children[i]);
}
// push parent node value to array
@Giagnus64
Giagnus64 / app.js
Last active February 26, 2020 22:27
PostOrder Helper for DFS
const postOrderHelper = node => {
//recursively call function on all node children FIRST
if (node.children.length !== 0) {
node.children.forEach(child => {
postOrderHelper(child);
});
}
//push value onto array
treeValues.push(node.value);
return true;
@Giagnus64
Giagnus64 / app.js
Last active February 26, 2020 22:19
PreOrder DFS
const preOrderHelper = node => {
//push value onto array FIRST
treeValues.push(node.value);
//recursively call function on all node children
if (node.children.length !== 0) {
node.children.forEach(child => {
preOrderHelper(child);
});
}
return true;
@Giagnus64
Giagnus64 / app.js
Last active February 25, 2020 00:24
Depth First Search
traverseDFS(type) {
//if there is no root, return false
if (!this.root) {
return false;
}
//make a variable for tree values
const treeValues = [];
//current values always starts at root
let current = this.root;
@Giagnus64
Giagnus64 / app.js
Last active February 26, 2020 21:45
Breadth First Search for Trees
traverseBFS() {
//if there is no root, return false
if (!this.root) {
return false;
}
//start a new Queue
const queue = new Queue();
//keep a tally of all values in the tree
const treeValues = [];
//add root to queue
@Giagnus64
Giagnus64 / app.js
Last active February 26, 2020 21:41
Basic Queue Structure for BFS Tree
class QueueNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;