Solved

Iterative function

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
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 and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

770 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