Data Structures: Check if an Element is Present in a Binary Search Tree

This commit is contained in:
Manish 2023-08-29 10:52:42 +10:00
parent 9bcc074f70
commit 7e2b75ac96

View File

@ -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
}