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
to_array(){ | |
if(!this.root) return [] | |
let queue = [this.root] | |
let result = [] | |
while(queue.length !== 0) { | |
let current = queue.shift() | |
result.push(current.val) | |
if(current.left) queue.push(current.left) | |
if(current.right) queue.push(current.right) | |
} |
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
//Deletes the lowest value from the tree by tracing leftmost from the root | |
removeLowest(){ | |
if(!this.root) return false; | |
this.delete(this.leftmost(this.root)) | |
} | |
//Deletes the highest value from the tree by tracing rightmost from the root | |
removeHighest(){ | |
if(!this.root) return false; | |
this.delete(this.rightmost(this.root)) |
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
else if(node.right && node.left) { | |
let right = this.leftmost(node.right); | |
node.val = right.val; | |
this.delete(right) | |
return true; | |
} |
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
//If it has only a right child, we replace the node we are deleting with its child. | |
else if (node.right && !node.left){ | |
let right = node.right; | |
node.val = right.val; | |
node.left = right.left; | |
node.right = right.right; | |
if(node.right) node.right.parent = node; | |
if(node.left) node.left.parent = node; | |
right = null; | |
return true; |
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
//If it has no children, it can be safely plucked. | |
if(!node.right && !node.left){ | |
if(!node.parent){ | |
this.root = null | |
} else if(node.parent.left === node){ | |
node.parent.left = null; | |
} else { | |
node.parent.right = null; | |
} | |
return true; |
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
delete(node){ | |
if(!node.right && !node.left){ | |
} | |
else if (node.right && !node.left){ | |
} | |
else if (!node.right && node.left){ | |
} | |
else if(node.right && node.left) { | |
} | |
} |
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
//Removing from a binary tree | |
remove(val){ | |
let node = this.getNode(val) | |
if(!node){ | |
return false; | |
} else { | |
return this.delete(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
//Inserting into a binary tree | |
insert(val) { | |
//If the root is null, initialize the tree with the value as its root. | |
if(!this.root) { | |
this.root = new Node(val) | |
return; | |
} | |
//Otherwise, search for the correct location. | |
let current = this.root |
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
import Node from './node' | |
export default class BST { | |
constructor(){ | |
this.root = null; | |
} | |
//functions go here | |
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
export default class Node { | |
constructor(val){ | |
this.val = val; | |
this.parent = null; | |
this.left = null; | |
this.right = null; | |
} | |
} |
NewerOlder