Solved

Iterative function

Posted on 1997-11-07
4
1,248 Views
Last Modified: 2008-03-10
I am trying to figure out an exercise out that tries to take a copy
function(below) and make it into an iterative function of postorder traversal
that does not have any recursion. I think using a stack is appropriate
isn't it? Any ideas?

typedef struct node *tree_pointer;
typedef struct node
{
int data;
tree_pointer left_child, right child;
};

tree_pointer copy (tree_pointer original)
/*this function returns a tree_pointer to an exact copy of the original
tree*/
{
    tree_pointer temp;
    if(original)
{
    temp=(tree_pointer)malloc(sizeof(node));
    if (IS_FULL(temp))   /*IS_FULL is a macro*/
   { fprintf(stderr, "the memory is full\n");
     exit(1);
   }
  temp->left_child = copy(original->left_child);
  temp->right_child=copy(original->right_child);
  temp->data=original->data;
  return temp
 }
return NULL;
}
0
Comment
Question by:ricco
[X]
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
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256189
I think using a stack is very appropriate.
(But if you don't want to use the program stack, it may be convenient
to use the tree itself as a stack by adding a parent pointer)
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1256190
I agree with ozo.  for doing a tree, a stack is a good idea. though  I would say recursion would be faster and less code.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256191
(since recursion naturally comes with a very nice automatic stack)
0
 
LVL 4

Accepted Solution

by:
emmons earned 100 total points
ID: 1256192
If you use a stack, are you not going to end up with a program that looks really close to this recursive program, but you will handle the recursion yourself? Will this satisfy the assignment?
You walk down the tree and at every branch you push the reference to that branch onto a local stack, then, when you can't go down any more, you pop it off the stack and proceed with the right branch. I am not sure that I see the advantage of this algorithm over the recursive one iin your example.

I am thinking that you are going to want to do a tree walk and create a tree that has backwards links on it as you go. This would make it so that you would not use the stack (which is really just another way of implementing recursion.) It would be a bit more of a memory hog during the copy, but it would be a completly different mechanism.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

695 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