From 7e2b75ac964910e081ef96956a5eb47755ddc771 Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 29 Aug 2023 10:52:42 +1000 Subject: [PATCH] Data Structures: Check if an Element is Present in a Binary Search Tree --- Data Structures/bstLookup.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Data Structures/bstLookup.js 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 +}