Search Trees

In this reading, we'll learn about how to use trees as an efficient way to search for data.

By the end, you should be comfortable with the following terms:

formatting...

Binary Tree

Below is a binary tree, cleaned up from last time.

Remember that a tree is a directed graph. It has one root node (that is, a node without a parent). Every other node has a parent. Nodes without children are called leaves.

This tree is a binary tree because each node has at most two children.

Search

What if we want to check whether a tree contains a value? We know it does if one of the following is true:

  1. the root has that value
  2. the left subtree contains that value
  3. the right subtree contains that value

Let's write a recursive function, contains, to do this search. At each step, we'll display the subtree being searched.

Cool, we found the value in the second place we looked because we check left first. What if the data is deep on the right side? Worse, what if the thing we're searching for isn't even in the tree? Let's try that:

Search Tree

Ouch, that was slow. It would be great if we could determine that an entry isn't in the tree without needing to look at every entry.

One way we can guarantee this is if every value in a left subtree is less than the value of the parent and every value in the right subtree is greater than the value of the parent.

Constructing a Search Tree

Let's create a function for adding values that guarantees this.

Using a Search Tree

Now that we've built a search tree, we can write a function for efficiently searching it. It's like the previous contains function, but now we only need to check one child instead of checking both each time.

Range Query

For the previous lookups, using a Python set would probably do about as well. But what if we want get all the values in some range?

We can write a similar function. But now, we'll sometimes need to search both sides (depending on the width of the range).

Rather than returning found values, we can accumulate everything in a list.

Balancing

If the depth of all nodes are roughly equal, the time to check a values will be O(log N), which is pretty great! But the insertion order matters a lot. Let's consider these 8 numbers:

Balance

The second tree is definitely a lot more balanced than the first. If we really want to measure this, we would like to identify openings that are shallower than the deepest nodes.

Conclusion

In this reading, we have seen that a binary tree is a BST (Binary Search Tree) if all the left descendents of a node have lesser values than the node, and all the right descendents have greater values. Binary search trees allow us to find values and ranges of values without checking every node.

In a perfectly balanced tree, looking for a single item is O(log N). A tree if balanced if there are no nodes that could be moved closer to the root.

Randomizing insertion order can improve balance. There are also algorithms (not covered) to rearrange trees as values are inserted, maintaining balance (perhaps within some tolerance).