building up binary tree

J

Jerry Khoo

Thanks for the answer, and by the way, i would like to know that in
C++,
how u create a tree using a recursive envronment[ i mean how to
allocate memory, to connect to each node, how to display the binary
tree content, deleting a node, and adding a node]. It seems that most
people used struct to create a node than, using recursive function to
build it up, but than is there any way around i could built a tree
using iterative method like for .. loop?
 
J

John Harrison

Jerry Khoo said:
Thanks for the answer, and by the way, i would like to know that in
C++,
how u create a tree using a recursive envronment[ i mean how to
allocate memory, to connect to each node, how to display the binary
tree content, deleting a node, and adding a node]. It seems that most
people used struct to create a node than, using recursive function to
build it up, but than is there any way around i could built a tree
using iterative method like for .. loop?

No. Trees are recursive structures and are therefore most naturally dealt
with using recursive algorithms. Any attempt to use an iterative algorithm
gets horrendously complex very quickly.

john
 
A

Ares Lagae

Jerry said:
Thanks for the answer, and by the way, i would like to know that in
C++,
how u create a tree using a recursive envronment[ i mean how to
allocate memory, to connect to each node, how to display the binary
tree content, deleting a node, and adding a node]. It seems that most
people used struct to create a node than, using recursive function to
build it up, but than is there any way around i could built a tree
using iterative method like for .. loop?

Yes. Each recursive algorithm can be written as an iterative one.
However, for most you will need to explicitely use some kind of stack
structure (e.g. to remember the current path in the tree). Using
recursive functions, the system stack does this bookkeeping for you.

Expressing tree algorithms using recursive functions is simple compared
to iterative versions. Recursive algorithms are most of the time
somewhat slower (there can be a lot of function calls) but probably this
does not matter for your application. Recursive algorithms can use a lot
of (sometimes too much) stack memory, but most of the time this is not
problematic (because in many applications, the tree depth is typically
logarithmic in the total number of nodes in the tree).

Best regards,
Ares Lagae
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top