April 2018
Beginner to intermediate
426 pages
10h 19m
English
In previous chapters, we also implemented the find, search, and get methods to find a specific value in the data structure. We will implement the search method for the BST as well. Let’s take a look at its implementation:
search(key) {
return this.searchNode(this.root, key); // {1}
}
searchNode(node, key) {
if (node == null) { // {2}
return false;
}
if (this.compareFn(key, node.key) === Compare.LESS_THAN) { // {3}
return this.searchNode(node.left, key); // {4}
} else if ( this.compareFn(key, node.key) === Compare.BIGGER_THAN ) { // {5}
return this.searchNode(node.right, key); // {6}
} else {
return true; // {7}
}
}
The first thing we need to do is declare the search method. Following the pattern of other methods ...