We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Insert Nodes

The building blocks of a BST are Nodes. In our implementation, we will only use a single class, the BSTNode class. Any BSTNode is technically also a full Binary Search Tree, with itself as the root node (it's not aware of any potential parents). Most of the methods that traverse the tree will do so recursively... have fun!

Our LockedIn BSTNode

Throughout this chapter we'll be building a binary search tree to power LockedIn's custom database. LockedIn's management doesn't trust so called "open-source"... so here we are. One of the primary features of databases is the ability to look up records by a single key, and binary search trees are the most common way to implement these fast lookups.

Each node in our BST will represent a LockedIn user. A BSTNode has three properties:

  • value: The value of the node, a User object in our case (see user.py). You'll notice that Users have a name and an ID. Comparison operators are already implemented for you on the class, so you should be able to compare User objects with ==, <, and > directly. The ID is the value that we'll use to determine the order of the nodes in the tree.
  • left: The left child of the node, another BSTNode or None
  • right: The right child of the node, another BSTNode or None

Assignment

Complete the insert method of the BSTNode class. It takes a User object as input and adds it to a new node if the value doesn't already exist in the tree.

Tip

I'd highly recommend using pencil/paper or some kind of drawing tool to visualize the tree as you go through the assignments in this chapter.