diff --git a/Data Structures/bstLookup.js b/Data Structures/bstLookup.js new file mode 100644 index 0000000..e5d1d0f --- /dev/null +++ b/Data Structures/bstLookup.js @@ -0,0 +1,26 @@ +// 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 +}