Trees are important not only in nature because they provide us oxygen to breath, they are also a significant data structure that exists in computer science. I was introduced to this type of system in my boot camp. Where we were taking on algorithms of how to traverse Binary search trees to see if they contained a node with some value or finding the max depth of a tree. I had not encountered these types of data compositions before, and in an effort to learn more about how to tackle these types of algorithms. I’m writing an introductory article to explore the different ways to traverse trees, and covering a few different types.

To start here is some guidance about why you would want to use this data system to store information. They reflect structural relationships in the data, and can be used to represent hierarchies. For example, if you want to represent a file system on a computer this would be an excellent way to achieve that. Some of them such as Binary Search trees are especially efficient structure for looking through, inserting and removing values from. And finally, they are generally flexible and it’s easy to move sub-trees around.

Trees come in many forms and get complex quickly. There are a myriad of different types and a number of ways to navigate through them. So as a starting point I will first talk about Binary search trees which I will now abbreviate as BST. These are a type of tree in where each node can have at max two children. They also have a trait in where the values on the left are smaller than the ones on the right. This allows you to cut about half of the remaining nodes out at each junction, making this an easy way to store information, and access it. This is such a useful certainty, that one strategy that I won’t cover here for solving more complex trees algorithms is to first convert them down into a BST. So how do we actually maneuver through to find the node we are looking for? For these basic types of trees. There are two main strategies for searching, based on when they move on to the next level. I will begin by covering depth first search. There are three methods under this term which are called: inorder, preorder, postorder.

Inorder is when we go left child node, current node, then right child node.

#data-structures #programming #algorithms #data-science

The Many Ways to Traverse a Tree
2.15 GEEK