473,661 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1734
On 26 Mar 2007 01:19:54 -0700, "su************ **@yahoo.com, India"
<su************ **@yahoo.comwro te:
>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.

Implementati on 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.

Implementati on 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
1850
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 single customer, the customer's orders, the order lines for those orders, the customer's invoices, and the invoice lines for those invoices. I'm thinking the term might be graph, but I'm not at all certain of this.
44
8771
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, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
1
2044
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
2654
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 the inorder traversal from http://www.python.org/peps/pep-0255.html as an example. def inorder(t): if t: for x in inorder(t.left):
26
12994
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 a memncmp function which is very fast but it doesn't work the same like strncmp so I don't think I can use it. I also tried to write the string_equal function myself like: int string_equal(const char* s1,const char* s2){ while(*s1 && *s2 &&...
6
4064
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
5
1661
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 libs and dlls that were used. We are loading up large (>20mb) xml files using DOM (we should use SAX, I know)- in 1.6 we can parse through the file and pull data on the order
15
4904
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 that it is almost two times slower (even without using reserve() before copy) and that's because it traverses the set twice. I'm posting it as an exception to the usual (and usually correct) advise "Prefer member functions to algorithms".
3
1561
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 this even possible and if so does anyone know of any articles websites dealing with this optimzation?
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.