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

Iterative function

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

861 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