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
class Node{ | |
constructor(value, priority){ | |
this.value = value | |
this.priority = priority | |
} | |
} | |
class PriorityQueue{ | |
constructor(){ |
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
//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; |
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
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; |
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
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; |
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
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 |
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
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; |
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
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; |
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
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; | |
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
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 |
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
class QueueNode { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
class Queue { | |
constructor() { | |
this.first = null; |
NewerOlder