Solved

Iterative function

Posted on 1997-11-07
4
1,243 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 nested-loops in the C programming language.

708 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

15 Experts available now in Live!

Get 1:1 Help Now