This file contains 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
Method | Single | Double | |
---|---|---|---|
Insert | O(n) | O(1) | |
Erase | O(n) | O(1) | |
Find | O(n) | O(n) | |
Clear | O(n) | O(n) | |
Empty | O(1) | O(1) | |
Begin | O(1) | O(1) | |
End | O(n) | O(1) |
This file contains 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 Team { | |
constructor(name, zone){ | |
this.zone = zone; | |
this.name = name; | |
} | |
} | |
class Game { | |
constructor (team1, team2, week){ | |
this.team1 = team1; |
This file contains 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 BinaryNode { | |
constructor(key, value) { | |
this.value = value; | |
this.key = key; | |
this.left = null; | |
this.right = null; | |
} | |
// Cost: O(1) | |
free() { | |
this.left = null; |
This file contains 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
insert(key, value) { | |
if (!Number.isInteger(key)) return; | |
const newNode = new BinaryNode(key, value); | |
if (this.root === null) this.root = newNode; | |
else this.insertNode(this.root, newNode); | |
} | |
insertNode(node, newNode) { | |
if (newNode.key < node.key) { | |
if (node.left === null) node.left = newNode; |
This file contains 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
remove(key) { | |
if (!Number.isInteger(key)) return; | |
this.root = this.removeNode(this.root, key); | |
} | |
removeNode(node, key) { | |
if (node === null) return null; | |
else if (key < node.key) { | |
node.left = this.removeNode(node.left, key); | |
return node; | |
} else if (key > node.key) { |
This file contains 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
inorder(node, fn) { | |
if (node !== null) { | |
this.inorder(node.left, fn); | |
fn(node); | |
this.inorder(node.right, fn); | |
} | |
} |
This file contains 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
preorder(node, fn) { | |
if (node !== null) { | |
fn(node); | |
this.preorder(node.left, fn); | |
this.preorder(node.right, fn); | |
} | |
} |
This file contains 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
postorder(node, fn) { | |
if (node !== null) { | |
this.postorder(node.left, fn); | |
this.postorder(node.right, fn); | |
fn(node); | |
} | |
} |
This file contains 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
levelOrder() { | |
if (!this.root) return []; | |
var array = []; | |
search(this.root, 1, 1); | |
function search(node, level, index) { | |
if (node) { | |
const count = Math.pow(2, level - 1); | |
if (array.length < level) { | |
array.push(Array(count).fill("")); |
This file contains 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
find(node, key) { | |
if (node === null) return null; | |
else if (key < node.key) return this.find(node.left, key); | |
else if (key > node.key) return this.find(node.right, key); | |
else return node; | |
} |
OlderNewer