Connecting Tech Pros Worldwide Forums | Help | Site Map

building up binary tree

Jerry Khoo
Guest
 
Posts: n/a
#1: Jul 22 '05
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?

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

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


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

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


Ares Lagae
Guest
 
Posts: n/a
#4: Jul 22 '05

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
Ares Lagae
Guest
 
Posts: n/a
#5: Jul 22 '05

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
Closed Thread


Similar C / C++ bytes