Image courtesy of Wikipedia.org. To learn more, visit http://en.wikipedia.org/wiki/Binary_tree for more.
It's a very abstract concept, and will take some time to fully understand. From wikipedia, the above picture is an example of an unbalanced binary tree.
A binary tree, for example, consists of nodes where each node may refer to two nodes or less. The top node is called a parent, and the nodes it connects to are called children. So, (2) is the parent, while (7) and (5) are children, and therefore called siblings. So in other words, this is a family data structure where the nodes are related to each other in some way.
One of the main features about a binary structure is that it can be 'balanced' in a way. For example, the parent node can contain some sort of data, say an integer, and the left child will have an integer that is less than the parent's integer, and the right child will have an integer that is greater than the parent's integer.
The below is an example from wikipedia.
Image courtesy of Wikipedia.org. To learn more, visit http://en.wikipedia.org/wiki/Binary_search_tree for more.
The most common example, as used by many sources such as wikipedia, users from Stack Overflow, and computer scientists are to make searching for data faster. Take the first picture for example. We want to find 11. At first glance, we can't just pick a child from the parent node and search from there. We have go through each node until we find 11. What if we wanted to find 4? That would take a bit more time, and we would probably have looked at the other nodes first already.
Now, take a look at the 'balanced' binary tree picture (second picture). What if we needed to find four? Well, we can simply check the parent. Is 4 less than 8? Yes. Onto the left child. Is 4 less than 3? No. It's greater than 3, so we check the right child. Is it less than six? Yes, let's check the left child, and there it is! What if we wanted to find 13? We don't have to look at all the nodes. We can simply look at the parent node (8), and find that we'll be going to take a look at the right child. From there, we will eventually find 13. We won't have a need to look at 3, 1, 6, 4 or 7. We've eliminated the unnecessary nodes.
It's a simple concept in itself, but actually applying it to practical situations will take some time. This concept will likely be revisited at a future date.
No comments:
Post a Comment