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 BTreeNode { | |
constructor(isLeaf) { | |
/** | |
* @type {number[]} list of values in the node | |
*/ | |
this.values = []; | |
/** | |
* @type {boolean} is a leaf | |
*/ | |
this.leaf = isLeaf; |
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 RedBlackTree { | |
constructor() { | |
this.root = null | |
} | |
insert(value) { | |
// Define an inner helper function for the recursive insertion | |
const insertHelper = (node) => { | |
// The current node we're considering | |
const currNode = node |
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 NodeColor = { | |
RED: 'RED', | |
BLACK: 'BLACK', | |
} | |
class RBTNode { | |
constructor(value, parent = null) { | |
this.value = value | |
this.left = null | |
this.right = null |
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 BinarySearchTree { | |
constructor(key, value = key) { | |
this.root = new BinarySearchTreeNode(key, value); | |
} | |
*inOrderTraversal(node = this.root) { | |
if (node.left) yield* this.inOrderTraversal(node.left); | |
yield node; | |
if (node.right) yield* this.inOrderTraversal(node.right); | |
} |
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 BinarySearchTreeNode { | |
constructor(key, value = key, parent = null) { | |
this.key = key; | |
this.value = value; | |
this.parent = parent; | |
this.left = null; | |
this.right = null; | |
} | |
get isLeaf() { |
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 getItems = (data, start, end) => { | |
if (data && | |
data.length > 0 && | |
start >= 0 && | |
end <= data.length && | |
start < end) { | |
return data.slice(start, end); // immutable | |
} | |
return ([]); |
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 vintageLinearSearch = (data, target) => { | |
for (let i in data) { | |
if (data[i] === target) return i | |
} | |
return -1 | |
} | |
console.log(vintageLinearSearch([1, 2, 3, 4], 1)) // 0 | |
console.log(vintageLinearSearch([1, 2, 3, 4], 4)) // 3 | |
console.log(vintageLinearSearch([1, 2, 3, 4], 6)) // -1 |
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 vintageInsertionSort = (data) => { | |
console.log('data input : ', data); | |
for (let i = 1; i < data.length; i++) { | |
console.log('data i : ', data); | |
// First, choose the element at index 1 | |
let current = data[i]; | |
let j; |
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 merge = (arr1, arr2) => { | |
let sorted = []; | |
while (arr1.length && arr2.length) { | |
if (arr1[0] < arr2[0]) { | |
sorted.push(arr1.shift()); | |
} else { | |
sorted.push(arr2.shift()); | |
} | |
} |
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 vintageBubbleSort = (data) => { | |
console.log('data input : ', data); | |
for (let i = 0; i < data.length; i++) { | |
console.log('data i : ', data); | |
for (let j = 0; j < data.length; j++) { | |
if (data[j] > data[j + 1]) { | |
let temp = data[j]; | |
data[j] = data[j + 1]; | |
data[j + 1] = temp; | |
} |