0

Recursion is soo confusing for me... below are the structs that are used to create the binary-like tree:

struct parent {
      char *name;
      Child *children;
};

typedef struct parent Parent;

struct child {
     struct Parent *Pptr;
     struct child *next;
};

typedef struct child Child;

I want to iterate through the entire tree and on each child/parent (basically every node) call a function called birthdaygift().

Below is what I've tried so far, but I don't think it works.

tree(Parent *parent)  {
   Child* child = parent->children;

   birthdaygift(parent);
  if (child == NULL) {
    return;
  }
  while (child->next != NULL) {
    Parent * recurseparent = child->Pptr;
    tree(recurseparent);
  }
  birthdaygift(parent);
}

It'd be really helpful if someone can give me some pointers?

1
  • It'd be good for you to clarify your question. Is this a misunderstanding of the structure you're trying to implement or the structure itself. Binary defines the tree structure as a tree comprised of nodes such that each node is a sub tree with at most 2 (left and right) children. That's different than a list of nodes with links to the next and previous nodes (a doubly linked list), which is what you have. Commented Nov 9, 2014 at 8:44

2 Answers 2

1

Your data structure looks very, very odd, it looks like you have the child nodes pointing back to the parents and are somehow maintaining the list of children in the child node as a linked list.... I'll give my answer assuming you are intending to work with a binary tree.

Now, assuming that is the case, wouldn't going up to the parent from one child and then going up from the next child take you to the same node? Typically, binary trees are parsed by starting from the top and going down.

A traditional binary tree has pointers from the parent (for this example, *left and *right), and you would traverse the entire tree by using either a Depth First Search algorithm (basically, you keep going left recursively until you run out of nodes, and then you go right), in pseudocode

function gift_node(*node) {
    birthday_gift(node);

    if (node->left != NULL) gift_node(node->left);
    if (node->right != NULL) gift_node(node->right);
}

Now, if you were to watch the process parsing this binary tree, you would see it start at the top and keep following the left node. It would then backtrack, process the right node and it's subordinate nodes and keep going until it has visited every node.

Sign up to request clarification or add additional context in comments.

Comments

1

The struct you declared is not a tree, it is similar to a doubly-linked list.

The struct for a binary tree node would be:

struct BST_Node {
    Value value;
    BST_Node *right;
    BST_Node *left;
};

For traversing a tree, you want to visit every single node. One way to do this is to visit all the nodes to the left recursively, then the node itself, then all the nodes to the right recursively (this is known as in-order traversal, but this isn't important for this situation).

If you want to call birthdaygift() on each node:

void traverse(Node *n) {
    if (n->left)
        traverse(n->left);
    birthdaygift(n);
    if (n->right)
        traverse(n->right);
}

A discussion of other tree traversal algorithms can be found here.

1 Comment

It's not a 2 LL but a fine n-ary tree structure with poor node type name choices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.