Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-06-10
1
232 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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