Binary Search Trees - Part I/?
Published: 2020-03-16 04:22:43

I've often found binary trees as a data structure to be intimidating, and that will not stand! I refuse to let any binary defeat me! (Just some enby humor for you.) So I'll take my next few blog posts to break them down into very manageable bite sized pieces.

In this first installment, I'll cover just the basics of what a binary tree consists of, and what types are most common. We can get into coding them and traversing them down the line. Big props to Cracking the Coding Interview (CTCI) for being my source for basically all of this.

A tree is a data structure that starts with one root node, which has zero or more child nodes. Each child node also can have child nodes. The result is a spreading 'tree' of data. Trees don't have to be binary, they can have up to any number of nodes. Here is a ternary tree for instance, which has three child nodes off its initial root node.
 ![a tree with three nodes on the level below the root](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584329754/Blog%20Images/Screen_Shot_2020-03-15_at_8.35.09_PM.png) 

The most common type of tree used though, especially in a programming interview, is a binary tree. A binary tree can have up to two children per node. Here's an example: 
![a binary tree with letters in each node](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584329749/Blog%20Images/Screen_Shot_2020-03-15_at_8.35.15_PM.png)

If it's just a plain ol' binary tree, the order of these nodes doesn't matter. Where things start getting interesting and useful is with a Binary _Search_ Tree. Those are in a particular order so that you can search through them to find something. On page 101 of Cracking the Coding Interview, a binary search tree is defined as one where all nodes match this description: "all left descendants <= n < all right descendants." So basically, the nodes are ordered from left to right, bottom to top. Here is a sorted binary search tree: ![a sorted binary search tree](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584330793/Blog%20Images/download.png)

This sorting is what makes it useful for algorithms. You can traverse through these trees to determine various useful things about your data set, but we'll go over how to do that in another post. for now, let's go over a few more qualities of binary trees. Terms you may see are **Complete**, **Full**, and **Perfect**. These things are all similar but distinct.

A **Complete** binary tree is one where every level of the tree must be fully filled, except the bottom level. The bottom level fills left to right if it is not full. Here is an example of a complete binary tree: ![a complete binary tree](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584331476/Blog%20Images/Screen_Shot_2020-03-15_at_9.04.13_PM.png). 
You can see that in this example the bottom level is not full, but both leaf nodes are on the far left. (A **Leaf Node** is one with no children.)

Slightly different is a **Full** binary tree. On a full tree, every node has either two or zero children. Here's an example: ![](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584331476/Blog%20Images/Screen_Shot_2020-03-15_at_9.04.18_PM.png)

Finally there's **Perfect** trees, which are very uncommon. A perfect tree is both full and complete, meaning every node has two children, except those on the bottom most level, which are all leaf nodes with zero children. Here's the three side by side so you can get the idea: ![from left to right, a full, complete, and perfect binary tree](https://res.cloudinary.com/dwfqeeh5f/image/upload/v1584331843/Blog%20Images/download_1.png)
Perfect trees are very uncommon as we are just unlikely to get that lucky with our number of nodes.

Those are the basics of what forms a binary search tree! Next time, I'll get into the basics of executing one in code, as well as some of the complications that arise in implementing them - such as what to do with duplicate nodes.