Data Structures: Check if an Element is Present in a Binary Search Tree
This commit is contained in:
parent
9bcc074f70
commit
7e2b75ac96
26
Data Structures/bstLookup.js
Normal file
26
Data Structures/bstLookup.js
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user