Friday, 6 March 2015

All About The Trees..!

Trees.
To normal people outside of the world of CS, this word might seem completely harmless. Might even give them happy thoughts, trees..greenery .. who doesn't like that, right ? right ?

Wrong.
Because to programmers, it is like a mini nightmare in one word.
But no, don't be afraid because it's not all bad... at least not yet.
Let me tell you why...

So, like I explained in my previous post, recursion is a simple concept to understand and trace but not that easy to code. There's is a general idea to follow but unless you understand exactly how to you want your code to work, recursion won't seem like the best idea.

"A tree is a set of nodes with directed edges between some pair of nodes." These edges connect a parent node to a child node.
A specific type of tree is Binary Tree. In this type of tree each parent only has two children, the left and right. This makes navigating through the tree a little easier.
This week we learnt to traverse through a tree in order to find its height and to find if a value exists in the tree or not.
Another specific tree is the Binary Search Tree. Like it's name suggests, you search for something in a tree. The catch you ask ?
BST (we love abbreviations), has to be made in a specific order. While these specifications might make the task easier, it is slightly confusing to understand.


Binary Search Tree

This is an example to understand how the placing of numbers in a BST works.
Each node on the left has to be smaller than all the nodes on its right. As you can see, 
9 < 12<14 < 17<19 < 23.....<76.
In this way, searching for a number becomes increasingly simplified. 

As far as I understand, trees work in a certain way, which is the key to coding with recursion for trees. 

Until next time.. !

PS - Did anyone get the song reference ? *no treble*



No comments:

Post a Comment