?
Solved

Iterative function

Posted on 1997-11-07
4
Medium Priority
?
1,249 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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