Recursion, Part 5: Building Binary Trees
Binary Search Trees
Let’s revisit one of our trees from the last post.
11
/ \
7 15
/ \ / \
2 8 13 18
Did you notice anything special about it?
11
/ \
7 15
/ \ / \
2 8 13 18
In this tree, the root node is 11. It’s left child, 7, is smaller, and its right child, 15, is larger.
11
/ \
7 15
Let’s look at 11’s children. If we investigate 7, it follows the same patter, it’s left child, 2, is smaller, and it’s right child, 8, is larger.
7
/ \
2 8
This leaves us with 15. Its left child, 13 is smaller, and its right child, 18, is larger.
15
/ \
13 18
This is a specialized form of a binary tree called a Binary Search Tree. All the nodes to its left are smaller than the root node. All the nodes to its right are larger. If we were looking for a specific value, we’d only search a portion of the tree, making our search time much shorter.
Next, let’s build a binary search tree class.
Binary Search Tree Node Insertion
Let’s write a binary search tree class and give it an insert method. Let’s begin with a class that creates an instance with a value, a left child, and a right child.
Next, let’s give our binary search tree class an insert method.
Binary Search Tree Node Deletion
Now that we have an insert method, let’s also give our class a remove method.