Day 49: Tree Traversals (In, Pre, Post) Order

Continued working on implementing a basic binary tree in C. I got to trying to write a function for printing out the values when the concept of tree traversals became important. I haven’t really addressed this before so I thought I should take some good notes on it. So far it seems like there are three main types of tree traversals and they all can be implemented using a recursive function that will cycle through all the nodes in the tree. Sounds simple enough but I doubt it will be LOL.

TLDR;

Okay, so here are the highlights of what I did:

  • Continued work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. I continued my work on Tree data structures. I started writing an implementation of a binary tree data structure in C I am doing so by following a YouTube Video. Started on writing the print tree function when I realized that I do not fully understand the differences between the types of tree traversals that can be used in a print function. I went back to my notes and found an decent article to read on the topic. I think I will try to implement a print function for each type of traversal as a proof of concept for my understanding… Maybe. Doing that will definitely add a bit more time to my studies on trees but I think it might be worth it.

Traversing a Tree

Trees can be traversed in multiple ways. Whereas linked lists, arrays, stacks, and many other data structures can only be traversed in a forwards or backwards order. Trees have 3 main types of traversals. “Inorder”, “Preorder”, and “Postorder” Traversals. There are also “Breadth-First” or “Level Order” Traversals. If we take a simple tree as an example, we can highlight the differences between the different types of traversals:

    1
   / \
  2   3
 / \
4   5
  • Inorder Traversal (left, root, right) = 4,2,5,1,3
  • Preorder Traversal (root, left, right) = 1,2,4,5,3
  • Postorder Traversal (Left, Right, Root) = 4,5,2,3,1
  • Breadth-First or Level Order Traversal = 1,2,3,4,5

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.