Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Searching for a specific value

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content