Sunday, February 16, 2014

Binary Trees, Pre-order, In-order, and Post-order Traversals

Pre-order, in-order, and post-order traversal is three ways that computer scientists travel through binary trees that hold different data structures. Depending on the order chosen, the final output may be different. Firstly, binary trees can be thought of as trees where each node is either a root, or a leaf. Roots are the nodes that have children and leafs are the nodes that do not have children. These roots may lead to another tree, or simply a leaf. From a computer science perspective, this can be modeled by recursion by breaking a big tree into a collection of smaller tree and leaves. The question that is left to be answered is "which order do we follow in the tree?"

Pre-order traversal is the method where we start from the root of the tree, then move on to the left sub-tree if available and finally move on to the right sub-tree if available. Note if the sub-trees have other sub-trees within the tree, this method is applied again.

In-order traversal is another method of traveling through a binary tree, but is slightly different from pre-order traversal. Instead of going from root to left sub-tree to right sub-tree, in-order traversal starts from left sub-tree, then move to the root, and finally move to the right sub-tree if available. Note the method is applied recursively if more sub-trees exist within other subtrees.

Post-order traversal is the last way to travel through a binary tree and the method goes as follows: starts from left sub-tree, then move on the root of the tree, and lastly move on to the right sub-tree. Note the recursive techniques applies to this method as well.

These three traversal methods can be coded through recursion and understanding these methods is key before any coding can be done. I myself looked through the tree diagrams and traced each method on paper a couple of times before I could say i was okay with the recursive techniques.When I first stumbled into these three methods in class, I was startled because traversals was completely new to me and I was still quite new to recursion. However, I believe the problem lies in me not being prepared for the lectures due to not reading the lecture notes ahead of time. Thus, that is the main problem at hand that I must fix. Concentration is key and slacking off only leads to falling behind and is a path I would not like to walk in.

No comments:

Post a Comment