Category Archives: Projects

Today we will explain the groundwork for building the some basic binary tree functions. If you are familiar with nearly any programming language you have most likely used data structures to hold multiple pieces of information.

Common data structures/containers include:

Arrays

Vectors

Stacks

Queues

Linked Lists

Maps

Trees

and more!

The most commonly used tree is thebinary tree. So we will focus on this one for now. To begin, why would we want to use a binary tree in the first place?

The two main reasons for using a binary tree are:

To help visualize the data in a hierarchical fashion. (File systems use this, along with HTML DOM)

Speed up the time taken to find/modify an element within the set.

What does it mean for data to be stored hierarchically? Lets start of with an image to help convey the idea!

Think of these integers the same way you would if you were storing them in an array. Although now there is an ordering to the values!

This image shows the hierarchy (ordering by rank) of a list of integers. Each of these circles we will call a node with each node containing a value that can point to a maximum of two other nodes. The top node (8) is called the root node. You will always begin your search of a value from the root node.

You can think of this as the root of a tree, with connected values branching downward. From the very top root node, there are two connected values (using pointers) with the left value 3 being LESS THAN 8 and the right value 10 being GREATER THAN 8. If you need to find or manipulate any of the numbers in this list, you could do this using an algorithmic process explained below.

Look inside the root node.

If the value you are looking for is greater than the root node, go right.

Otherwise if the value is less than the root node, go left.

Repeat the process until the number you are looking for is found!

This not only helps us visualize how the data is being traversed, but also will allow us to find a value without having to potentially look at every single element/memory location to find it. In the worst case scenario of searching through an array, the number you are looking for could always be saved at the very last element of the array. This can give us very bad performance if we are traversing hundreds of millions of numbers!

In fact, if we have a number of elements the complexity approaches big O(N) (N being number of elements being looked at), while a Binary Tree helps to reduce the number of potentially searched elements to a complexity of O(log(N)) during the worst case scenario.