# Binary Search Trees: A Quick and Dirty Recap

I recently learned about Binary Search Trees. BSTs are a pretty useful data structure, in that they not only store data in sorted order, but the way they’re implemented allows for quick lookup, insert and remove operations all with a time complexity of O(log n) time.

Ok, so from what I understand, this is how it works:

A tree traversal is a special case of graph traversal, and they refer to the process by which each node in the tree is visited, but only once each. These traversals are further classified by the order in which they process or visit the nodes. There are several classification of orders, but I’ll be focusing on breadth-first search and depth-first search which has further classifications of in-order, pre-order, and post-order traversal.

Whew, that’s a lot of words! If I were to put all that in diagram form, it might look something like this:

Binary Search Trees have a couple of special properties, those being:

- Each node can only have two children.
- Of the children, the smaller key is always stored in the left node and the larger value is always stored in the right node.

# Terminology

Before I continue, let’s go over some terminology:

**Node:**a unit of data, typically connected/linked to other nodes**Key:**every node has a key (and it’s value pair)**Root:**the top node of the tree, ancestor of all other proceeding nodes.**Parent:**a node with at least one child**Child:**the left or right node/subtree of a parent node**Leaf:**nodes with no children**Left and Right**: Remember: A left child node’s key will always be less than its parent node’s key and a right child node’s key will always be greater than its parent’s node key

Here’s another diagram:

There are two main methods of searching through a binary tree: **Depth-First Search **and **Breadth-First Search**.** **As you may have surmised through their names, one method goes in deep first, and the other casts its net wide.

# Breadth First Search

**Breadth First Search** is implemented with a **Queue** and goes by level order.

Here’s a diagram for **Breadth-First Search**:

# Depth-First Search

With **Depth-First Search**, it’s implemented with a **Stack**. Each node has a visitation sequence it follows. There are three different ways to traverse the depths.

- Pre-Order(nlr)

-In-Order(lnr)

-Post-Order(lrn)

# Pre-Order Depth-First Search

With **Pre-Order DFS**, the sequence goes Node ⇒ Left ⇒Right, as in:

- Visit the Node (note your visit)
- Check for Left Node
- Check for Right Node

It’s important to note here that this sequence is for *each node* and because DFS uses a stack, that means that for every node with a left child, it will continue to travel deeper to the left first before ever getting to the right child of the parent.

# In-Order Depth-First Search

With **In-Order DFS**, the sequence goes Left ⇒ Node ⇒ Right:

- From given/current node: check for Left child,

2a. (If no left child) visit Node

2b. (If yes left child), Repeat from Step 1

3. Check for right child

4. Repeat from Step 1

So you can see how this is a stack implementation because it *stacks* up until the condition (visit a leaf node) is fulfilled/cleared from the stack before it processes the rest of the stack.

Also with In-Order DFS, because of the unique Binary Search Tree properties of the left always being smaller and the right being greater, you will always get a return output that is sorted in increasing numerical order.

# Post-Order Depth-First Search

Finally we get to **Post-Order DFS**! This sequence goes Left ⇒ Right ⇒Node.

- Starting from current node: is there a left node?
- Yes? Check if that one has a left node. If yes, (keep going until there’s no left node)
- Ok, no left node, is there a right node? No? Ok visit that node.
- Now backtrack and where we we last? Oh yeah, is there a right node? Yes?
- Ok proceed with step 1 (yay recursion)

Last diagram because words are hard:

Ok… maybe words are hard but diagrams may be harder.

The diagrams may make it seem unnecessarily complex (which they are), but I found a great video demonstrating these traversals in action, which is magnitudes easier to pick up than static diagrams (in particular, mine).

Check it out here: https://www.youtube.com/watch?v=BHB0B1jFKQc

**Sources:**

(All diagrams created fresh for this article)

**GROOT** is a trademark of Marvel Characters, Inc. **LEIF **is a character on **ANIMAL CROSSING **which is a trademark of Nintendo of America Inc. I do not own these characters. Please don’t sue me.

**Binary Search Trees - Wikipedia: **https://en.wikipedia.org/wiki/Binary_search_tree

**Binary Tree Bootcamp: Full, Complete, & Perfect Trees. Preorder, Inorder, & Postorder Traversal. - Back to Back SWE: **https://www.youtube.com/watch?v=BHB0B1jFKQc