1

I have the following tree structure:

enter image description here

this one shows 3 levels. My actual problem will have 8 to 12 levels. I have the following program that I believe will traverse the tree in the right order. Two children nodes report to a parent node. If we know both children we can find the parent. Essentially we want to traverse the tree from right to left and from bottom to top. The numbers indicate the order the nodes need to be traversed.

Here's my code that I believe will accomplish this:

#include <stdio.h>

int main(void)
{
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            for (int k = 0; k < 2; k++)
            {
                printf("k loop: %d   ", i * 7 + j * 3 + k);
            }
            printf("\n");
            printf("j loop: %d  \n", i * 7 + j * 3 + 2);
        }
        printf("i loop: %d  \n", i * 7 + 6);
    }
    printf("final node: %d\n", 2 * 2 * 2 * 2 - 2);
}

This isn't very pretty and not very scalable as I would need to add another for loop for each additional level.

three questions:

  1. how would I do this with recursion?
  2. Is there a more scalable way of doing this without recursion?
  3. which will be faster a for loop approach or a recursion approach
chqrlie
  • 131,814
  • 10
  • 121
  • 189
DCR
  • 14,737
  • 12
  • 52
  • 115
  • Is the tree always complete? Or can some nodes have just one child? – rici Aug 30 '20 at 17:03
  • it's possible that the tree may not be complete in that a parent node may have no children and one node (and only one node) might have only 1 child. for instance I might have 20 tree structures where the first 19 of them have 511 nodes and the last one may only have 205 nodes – DCR Aug 30 '20 at 17:35
  • "Right to left and bottom to top" is a bit hard to interpret, particularly when you draw the tree on its side :-) But I have a hard time finding a way to interpret it which aligns with your program. – rici Aug 30 '20 at 17:52
  • I guess what you're looking for is a [postorder traverse](https://en.wikipedia.org/wiki/Tree_traversal#Post-order_(LRN)) – rici Aug 30 '20 at 18:02

2 Answers2

2

You can do this recursively with these steps for p(n, level):

  • if level > 0, first print the substrees with
    • call n = p(n, level - 1) for the left subtree
    • call n = p(n, level - 1) for the right subtree
  • then print n and return n+1

Here is a naive implementation:

#include <stdio.h>

int p(int n, int level) {
    if (level > 0) {
        n = p(n, level - 1);
        n = p(n, level - 1);
    }
    printf("%i\n", n);
    return n + 1;
}

// initial call for a depth of 8:
int main() {
    p(0, 8);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • this just prints out the numbers 0 - 510 in order. Not really very helpful – DCR Aug 30 '20 at 17:46
  • @dcr: Your code prints the numbers 0 to 14 in order too with some extra info that is hardly useful. What do you want the output to look like? – chqrlie Aug 30 '20 at 17:50
  • @dcr: Your program also prints out numbers 0-14 in order, although there's some variation on the lines. Maybe you need to add the desired enumeration order to your question. – rici Aug 30 '20 at 17:50
  • thanks,you are right. When I print out the level I can see that the tree is being traversed correctly. Thanks. – DCR Aug 30 '20 at 17:57
  • Your answer works but I'm having a hard time understanding how: consider p(0,2). doesn't that first ask for p(0,1) which asks for p(0,0) which returns 1. so why does the first number printed out = 0 (which is correct) instead of 1? – DCR Aug 30 '20 at 18:32
  • @DCR: the value of `n` passed to `p()` will be the first one printed. The instance of `p` that prints it returns `n+1` which is stored into `n` upon returning from a recursive call, so the next value either passed to `p()` for the second recursive call or printed after the second recursive call (if any) is the next in numeric order... – chqrlie Aug 30 '20 at 18:40
0
  1. What you're looking for is called an In-Order Traversal. It is generally independent of the number of levels in your tree because it is a type of Depth-First Traversal.

It generally follows the following algorithm

  1. Recursively traverse left subtree
  2. Visit root node
  3. Recursively traverse right subtree

Here is a link for more information


  1. It is entirely possible, and generally recommended, to use iterative methods of tree traversal. Although for small trees it's effects aren't really felt, for large recursive traversal takes up exponential amounts of memory space. Here is an example of in-order traversal using a stack on geekforgeeks

  1. Although you won't notice it on a small tree, recursive approach is always slower than iteration.
Eazash
  • 129
  • 1
  • 8
  • 1. This desired traverse is post-order, not in-order. 2. The iterative and recursive solutions use the same amount of memory: a stack of continuation records. In both cases, the storage requirement is linear, not exponential. – rici Aug 30 '20 at 20:01