BinarySearchTree

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node { constructor(value) { this.left = null; this.right = null; this.value = value; } } class BinarySearchTree { constructor () { this.root = null; } insert (value) { const newNode = new Node(value); if (this.root === null) { this.root = newNode; return; } let currentNode = this.root; while (currentNode) { if (value < currentNode.value) { if(!currentNode.left) { currentNode.left = newNode; return this; } currentNode = currentNode.left; } else { if (!currentNode.right) { currentNode.right = newNode; return this; } currentNode = currentNode.right; } } return this; } lookup (value) { if (this.root && this.root.value === value) return this.root; let currentNode = this.root; while (currentNode) { if (currentNode.value === value) { return currentNode; } else if (value < currentNode.value) { currentNode = currentNode.left; } else { currentNode = currentNode.right; } } return false; } traverse() { } remove(value) { } } const binarySearchTree = new BinarySearchTree(); binarySearchTree.insert(9); binarySearchTree.insert(40); binarySearchTree.insert(20); binarySearchTree.insert(6); binarySearchTree.insert(1); binarySearchTree.insert(15); binarySearchTree.insert(170); //console.log(binarySearchTree); console.log(binarySearchTree.lookup(170));
Editor Settings
Theme
Key bindings
Full width
Lines