freeCodeCamp/Data Structures/bstLookup.js

27 lines
702 B
JavaScript

// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
// Only change code below this line
this.isPresent = (item) => {
let node = this.root;
while (node !== null) {
if (node.value === item) {
return true;
} else if (node.value < item) {
node = node.right;
} else {
node = node.left;
}
}
return false;
};
// Only change code above this line
}