Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > performance question related to BST traversal

Reply
Thread Tools

performance question related to BST traversal

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      03-26-2007
This is not a homework assignment question.
Kindly excuse me for posting this question here.

Suppose a BST contains 100,000 ndoes.

Suppose I have to traverse the BST by Preorder, say, without using
recursion. Suppose I know the count of nodes. Suppose I use a stack
approach for traversal.

Implementation 1:
I allocate memory for an array of nodeCount times the size of a tree
node. Then I traverse the tree using this stack for pushing and
popping the nodes temporarily.

Advantage: malloc and free will be called only once.
DIsadvantage: huge memory will be needed, whose availability may not
be assured and even if available, all of it may not be needed.

Implementation 2:
Instead of an array of nodes, I use a singly linked list. Push and pop
in the beginning of the list each time.

Advantage: memory is allocated only on need based.
Disadvantage : frequent calls to malloc and free.

QUESTION: Kindly explain as to which approach should be preferred and
why ?
Is there any other way of traversing the tree without using recursion?

Thanks

 
Reply With Quote
 
 
 
 
Richard Harter
Guest
Posts: n/a
 
      03-26-2007
On 26 Mar 2007 01:19:54 -0700, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:

>This is not a homework assignment question.
>Kindly excuse me for posting this question here.
>
>Suppose a BST contains 100,000 ndoes.
>
>Suppose I have to traverse the BST by Preorder, say, without using
>recursion. Suppose I know the count of nodes. Suppose I use a stack
>approach for traversal.
>
>Implementation 1:
>I allocate memory for an array of nodeCount times the size of a tree
>node. Then I traverse the tree using this stack for pushing and
>popping the nodes temporarily.
>
>Advantage: malloc and free will be called only once.
>DIsadvantage: huge memory will be needed, whose availability may not
>be assured and even if available, all of it may not be needed.
>
>Implementation 2:
>Instead of an array of nodes, I use a singly linked list. Push and pop
>in the beginning of the list each time.
>
>Advantage: memory is allocated only on need based.
>Disadvantage : frequent calls to malloc and free.
>
>QUESTION: Kindly explain as to which approach should be preferred and
>why ?
>Is there any other way of traversing the tree without using recursion?


Given your constraints you are going to have to grab some memory one way
or another. However the considerations you are running into are typical
issues in C and it is worth while reviewing common, useful strategies.

In implementation one, consider allocating a predetermined size for the
stack and doubling the size with realloc whenever the stack is about to
overflow. The number of calls to malloc/realloc will be O(log n). An
advantage of this approach is that there will only be one call to free.

In implementation two, consider allocating a number of nodes at once.
That is, allocate an array of nodes and link them into a free list.
When you push something on the stack you get its node from the free
list, and when you pop something from the stack you put the node back on
the free list. In this implementation you don't have to realloc. In
fact you had better not if you are using pointers. If the free list is
empty grab more space as before and link it into the free list.

In implementation two there is one thing you have to be careful about
and that is that you have to keep track of the arrays that you allocated
so that you can free them later. One way to do that is to reserve the
first node (index 0) as use as a linked list that links the arrays
together.

Again, you can double the size of each array allocation for O(log n)
allocations. However you can also allocate a fixed size array of nodes
each time. Which is more efficient depends on the implementation but it
probably doesn't matter.

HTH


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LL TO BST puzzlecracker C++ 2 01-19-2005 11:10 PM
Applications of stacks/queues/trees/heap/BST Ice C++ 4 12-19-2004 06:04 PM
BST: remove less than Ridimz C++ 8 10-07-2003 01:40 AM
BST Rupert harrison C++ 2 09-15-2003 08:38 AM
BST & recursion Andrew Edwards C++ 12 08-04-2003 10:54 AM



Advertisments