473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

performance question related to BST traversal

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

Mar 26 '07 #1
1 1718
On 26 Mar 2007 01:19:54 -0700, "su**************@yahoo.com, India"
<su**************@yahoo.comwrote:
>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
Mar 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Steve Jorgensen | last post by:
Terminology question: Is there a term for a set of records related directly or indirectly by key value in several tables? For example, a single invoice record and its line item records -or- a...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
by: guy001 | last post by:
Hi, I'm trying to traverse the DOM in a bit of a non-traditional manner and am struggling to get my head around it. Just say i have some elements like so: A |-B |-C | |-D |
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
26
by: pembed2003 | last post by:
Hi, I have an application where I use the strncmp function extensively to compare 2 string to see if they are the same or not. Does anyone know a faster replacement for strncmp? I notice there is...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
5
by: Bryan | last post by:
Hi, We have an application that we just upgraded to xerces-c-2_7_0-win32. This same application used to use xerces-c1_6_0-win32. We didnt change any other code in our app other than the xerces...
15
by: kostas | last post by:
Hi, Trying to dump a set<int(s) in a vector (v) my guess was that v.insert(v.end(),s.begin(),s.end()) would be at least as efficient as copy(s.begin(), s.end(), back_inserter(v)) It turn out...
3
by: Johs | last post by:
I have implemented a red-black tree based on the description in Introduction to Algorithms Cormen section 13. But I would like to make all iterator operations to take O(1) time in worst case. Is...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.