www.delorie.com/gnu/docs/avl/libavl_64.html   search  
Buy GNU books!

GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.10.2 Iterative Copying

Now we can factor out the recursion, starting with the tail recursion. This process is very similar to what we did with the traversal code, so the details are left for Exercise 1. Let's look at the results part by part:

/* Copies org to a newly created tree, which is returned. */
struct bst_table *
bst_copy_iterative (const struct bst_table *org)
{ struct bst_node *stack[2 * (BST_MAX_HEIGHT + 1)];
/* Stack. */ int height = 0; /* Stack height. */
See also @refalso{79

This time, our stack will have two pointers added to it at a time, one from the original tree and one from the copy. Thus, the stack needs to be twice as big. In addition, we'll see below that there'll be an extra item on the stack representing the pointer to the tree's root, so our stack needs room for an extra pair of items, which is the reason for the "+ 1" in stack[]'s size.

  struct bst_table *new;             /* New tree. */
  const struct bst_node *x;          /* Node currently being copied. */
  struct bst_node *y;                /* New node being copied from x. */
  new = bst_create (org-&#62;bst_compare, org-&#62;bst_param, org-&#62;bst_alloc);
  new-&#62;bst_count = org-&#62;bst_count;
  if (new-&#62;bst_count == 0)
    return new;
  x = (const struct bst_node *) &org-&#62;bst_root;
  y = (struct bst_node *) &new-&#62;bst_root;

This is the same kind of "dirty trick" already described in Exercise 4.7-1.

  for (;;) 
{ while (x-&#62;bst_link[0] != NULL)
{ y-&#62;bst_link[0]
= org-&#62;bst_alloc-&#62;libavl_malloc (org-&#62;bst_alloc, sizeof *y-&#62;bst_link[0]); stack[height++] = (struct bst_node *) x; stack[height++] = y; x = x-&#62;bst_link[0]; y = y-&#62;bst_link[0]; } y-&#62;bst_link[0] = NULL;

This code moves x down and to the left in the tree until it runs out of nodes, allocating space in the new tree for left children and pushing nodes from the original tree and the copy onto the stack as it goes. The cast on x suppresses a warning or error due to x, a pointer to a const structure, being stored into a non-constant pointer in stack[]. We won't ever try to store into the pointer that we store in there, so this is legitimate.

We've switched from using malloc() to using the allocation function provided by the user. This is easy now because we have the tree structure to work with. To do this earlier, we would have had to somehow pass the tree structure to each recursive call of the copy function, wasting time and space.

      for (;;) 
{ y-&#62;bst_data = x-&#62;bst_data; if (x-&#62;bst_link[1] != NULL)
{ y-&#62;bst_link[1] =
org-&#62;bst_alloc-&#62;libavl_malloc (org-&#62;bst_alloc, sizeof *y-&#62;bst_link[1]); x = x-&#62;bst_link[1]; y = y-&#62;bst_link[1]; break; } else
y-&#62;bst_link[1] = NULL; if (height <= 2) return new; y = stack[--height]; x = stack[--height]; } } }

We do not pop the bottommost pair of items off the stack because these items contain the fake struct bst_node pointer that is actually the address of bst_root. When we get down to these items, we're done copying and can return the new tree.

See also: [ Knuth 1997], algorithm 2.3.1C; [ ISO 1990], section


1. Suggest a step between bst_copy_recursive_2() and bst_copy_iterative(). [answer]

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003