PDA

View Full Version : Trees



lordhaworth
August 15th, 2008, 04:13 PM
Hey all

Me again, I've only recently been programming with pointers and have realised how useful they can be by improving some old programs using linked lists etc and creating some new programs i never could before.
However, although I understand what it is, im having trouble constructing a tree. Ive searched a fair bit and not really found any tutorials on how to construct a tree - only on how to search them (which isnt very useful when i cant build one).

A lot of tutorials tell you how to build linked lists and then kind of go "this is a tree, you can build it in pretty much the same way" which makes me think itll prob be easy but Im having trouble!
This is in fortran90 again, direction to tutorials or advice would be much appreciated. Using a tree recursively will probably be the next big useful thing for me so Im hoping to get this nailed!

Cheers

Tom

lordhaworth
August 15th, 2008, 04:29 PM
To specify my trouble a bit. Im having a bit of a problem with the tree as it gets bigger. Am I going to have to come up with some kind of traversal condition and then when I get to a node that has null Left and Right pointers come up with some criteria as to which to add the next node to?

nvteighen
August 15th, 2008, 04:42 PM
To specify my trouble a bit. Im having a bit of a problem with the tree as it gets bigger. Am I going to have to come up with some kind of traversal condition and then when I get to a node that has null Left and Right pointers come up with some criteria as to which to add the next node to?
Yes.

mike_g
August 15th, 2008, 04:50 PM
To specify my trouble a bit. Im having a bit of a problem with the tree as it gets bigger. Am I going to have to come up with some kind of traversal condition and then when I get to a node that has null Left and Right pointers come up with some criteria as to which to add the next node to?
In an unbalanced tree you always follow the path until you reach a null node then add your new node there. Theres a little extra complexity when nodes have equal values.

Heres a good tutorial on binary trees: http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_bst1.aspx

The code examples are in C, but it explains the concepts well. Such as how to delete nodes and create balanced trees.

lordhaworth
August 16th, 2008, 12:11 AM
Cheers C and Fortran are similar enough for it to be useful! Shows what I mean about a lack of info about it online though I had a fair look!

mike_g
August 16th, 2008, 12:19 AM
No worries, that site is hard enough to find through google even when you're explicitly searching for it ;)