Hi all,
I was always a bit confused about how to create a recursive function, so thats where i need you help. :)

I have a treeview, in wich the user adds/removes dynamicaly nodes and a button wich should save the treeview structure in a database table when clicked. The thing is, that the depth is not known, so i need a way that i can get all-depth nodes and save the whole Treeview structure. I thought that i need a recursive function, but if you have any better solution, be my guest. :P


Thanks in advance,
Alex

I have a treeview, in wich the user adds/removes dynamicaly nodes and a button wich should save the treeview structure in a database table when clicked. The thing is, that the depth is not known, so i need a way that i can get all-depth nodes and save the whole Treeview structure.

The Wikipedia article on tree traversal has some good explanations of the task you're describing, including some examples.

Exactly how you write the function will depend on your data model. If it uses a recursive relationship, you'll probably want to use a preorder traversal so that you have the parent row's key available when you're creating the child records.

I was always a bit confused about how to create a recursive function, so thats where i need you help. :)

... I thought that i need a recursive function, but if you have any better solution, be my guest.

I think recursion is appropriate here. You can traverse a tree without recursion, but I wouldn't sacrifice its expressive power unless you have constraints, e.g., limited stack space.


P.S. For a laugh, search for "recursion" on Google. =)

This article has been dead for over six months. Start a new discussion instead.