building up binary tree | | |
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? | | | | re: building up binary tree
"Jerry Khoo" <jeep1188_83@yahoo.com> wrote in message
news:b639627.0404052127.3e6cb64c@posting.google.co m...[color=blue]
> 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?[/color]
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 | | | | re: building up binary tree
"Jerry Khoo" <jeep1188_83@yahoo.com> wrote in message
news:b639627.0404052127.3e6cb64c@posting.google.co m...[color=blue]
> 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?[/color]
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 | | | | re: building up binary tree
Jerry Khoo wrote:[color=blue]
>
> 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?[/color]
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 | | | | re: building up binary tree
Jerry Khoo wrote:[color=blue]
>
> 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?[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|