Solved

Is is possible to convert the following code without using recursive way??

Posted on 2006-06-10
1
222 Views
Last Modified: 2012-05-05
void level_order_aux ( node *tree, int level )
{
  if ( tree == 0 )
    return;

  if ( level == 0 )
    printf ( "%d ", tree->data );
  else if ( level > 0 ) {
    puts ( "Recurse left" );
    level_order_aux ( tree->left, level - 1 );
    puts ( "Recurse right" );
    level_order_aux ( tree->right, level - 1 );
  }
}

void level_order ( node *tree )
{
  for ( int d = 0; d <= height ( tree ); d++ )
    level_order_aux ( tree, d );
}
0
Comment
Question by:shingo43
1 Comment
 
LVL 22

Accepted Solution

by:
grg99 earned 125 total points
ID: 16879298
Sure, but you'd need an auxiliary array to remember where you've been, so you can back up.  SOmething very rougly like:

node * save [ MaxLevels ];

p = 0;

Save[ p ] = tree;  // save our place
while( tree->left != NULL ) {
  Save[ p++ ] = tree;  // save our place
  printf( "%d",  tree->data );
  tree = tree->left;
}

for( --p; p >= 0; p--) {  // now backtrack
tree = Save[ -- p ];  
while( tree->right != NULL ) {
  printf( "%d",  tree->data );
  tree = tree->right;
}
}
--- make that VERY roughly!

The basic idea is to instead of recursing, make an entry in the save array.  Then backup thru the info.


 

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
Which IDE to use to begin C++ training? 5 59
ASP.net build a IF/Then Walkthrough Guide 1 192
Using Diagnostic tools in VS2015: Unresoved allocations 19 66
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now