My First Data Structure: A very basic guide to Trees
Tags: Coding, Programming, Binary Search Tree, Trees, Data Structures

My First Data Structure: A very basic guide to Trees HopeGiometti Follow Jul 7 · 4 min read

Let me be honest: despite the name of this post, trees were definitely not my first data structure. Nor, should they be yours. However, I’m going to keep the title of my post the same to reassure you that trees are definitely a data structure that beginners like us are capable of understanding and implementing on our own. That all being said, I’d still recommend checking out my post on linked lists before diving into trees.

Now that we’ve gotten that little disclaimer out of the way, let’s jump right in!

Bob Ross painting trees

A (very) basic overview

Trees are a data structure.

What? Too basic?

Ok, let me try again. Trees are a hierarchical data structure that are unilinear and begin with a root and have descending child nodes.

Tree diagram

As you can see in the diagram above, the root of the tree is 2 and it’s descending child nodes are 7 and 5 which in turn have their own child nodes which in turn have their own child nodes, and so on.

This probably doesn’t seem too complicated and nor should it. This type of descending structure is something you’re most likely familiar with and probably use everyday. Take for example the files on your computer. You’ll have a folder like “documents” and inside that folder you might have more folders like “coding notes” or “totally rando” and inside each of those folders you might have more folders and so on.

Binary Trees

Now that you hopefully have a basic understanding of the structure of trees, let’s take a look at a very common type of tree: a binary tree.

To make a tree binary, we must follow two specific rules. First, each node can have at most two child nodes. Second, each child have have only one parent.

Take another look at our diagram above. Is it a binary tree?

While the tree in our diagram does follow our second rule (each child node does only have one parent node), it does not follow our first rule. The node with a value of 7 has three children, thus breaking the Binary Tree rule of only allowing two child nodes.

Here is one example of how a binary tree can look:

A perfect binary tree!

This diagram is an example of what we would call a perfect binary tree, meaning that all interior nodes (aka all nodes minus the root node and the lowest level of nodes) have two children and all leaves have the same depth/level. Perfect binary trees are very efficient and have some properties that can be useful to developers.

Binary Search Trees

Binary search trees can also be called ordered or sorted binary trees, which makes sense given how they work. Take a look at this diagram and see if you can figure out how a BST functions:

Binary Search Tree

Let’s start at the top of the tree. To the left of our root node 8, we can see the child node 3. To the right, we can see the child node 10. Now, let’s look again, but this time starting with our child node 3. To the left, we see a child node of 1. To the right, we see 6. Do you see the pattern yet?

Basically, the rule of a binary search tree is that all child nodes in the tree to the right of the root node must be greater than the current node and all child nodes to the left must be less than the current node.

Generally, binary search trees are a super useful data structure as they typically run faster than O(n)*, they are ordered, and they have a flexible size. Compared to an array, for example, a BST lookup will be much faster as it will generally take O(log N) in comparison to O(n). Similarly, compared to a hash table, BSTs are ordered and have preserved relationships.

*While BSTs are great and can be considered very efficient and useful data structures, they do have the potentially to become unbalanced:

Balanced tree vs unbalanced tree

If you take a look a the unbalanced binary tree, you might be reminded of another data structure: linked lists. Linked lists are actually considered trees, but unlike other trees they have only a single path. Moreover, if you are familiar with linked lists then you might understand why BSTs can run into some runtime issues as many linked list operations have a runtime of O(n). Ultimately, however, there are lots of ways to balance trees, making the issues of unbalanced trees minimal.

A good motto for both painting and coding!

I hope you enjoyed this very basic introduction to the trees data structure. If you are curious to see trees in action, you can check back in with me soon to see my own implementation of a binary search tree or give it a go and try to implement one on your own! As I always say, the best way to learn algorithms and data structures is to practice, so please give it a try!

Best of luck and happy coding!