-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|585|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Misc


 
JL235
Created : 07 October 2007
Edited : 18 September 2010
System : Windows
Language : Blitz

Algorithms: Binary Trees Example



Here is an example of a Binary Tree. Binary Trees are used for storing data where each node holds two more nodes on it's left and right branch. When you input a value to the root node it is passed to the left branch if it is less then the current value or to the right branch if it is greater. This occurs until it can find an empty branch where it can be added as a new node.

This is useful for storing large amounts of data, such as a dictionary. This is because as you look down through the tree for data you miss out other large branches of data, essentially refining your search. It also allows data to be structured.

For example if you add the numbers 1, 2, 3, 4 and 5 into a new Tree in any order, the tree will automatically have the numbers ordered. In my implementation the highest (like 5) are on the left side, the lowest (like 1) are on the right and all other numbers are ordered in between (note the trees printed in the image are rotated 90 degrees, so left side is at the bottom and right side is at the top).

In the above image the numbers are always in the order 1 to 5 from top to bottom, despite being entered in different orders.


 

Comments