The following algorithm can be very useful if you want to create a tree from a list of ordered elements. For example you can parse a XML file and create a list of tags and values, where you had calculated the level of each element.