Creating the Node and BinarySearchTree classes

Let’s start by creating our Node class that will represent each node of our binary search tree using the following code:

export class Node {
  constructor(key) {
    this.key = key; // {1} node value
    this.left = null; // left child node reference
    this.right = null; // right child node reference
  }
}

The following diagram exemplifies how a binary search tree (BST) is organized in terms of the data structure:

Just as in linked lists, we will work with pointers (references) again to represent the connection between the nodes (called edges in tree terminology). When we worked with doubly linked lists, each ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.