简体   繁体   English

C中的二叉树-递归

[英]Binary-like tree in C - recursion

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(). 我想遍历整个树,并在每个子级/父级(基本上是每个节点)上调用一个名为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? 如果有人可以给我一些指示,那真的有帮助吗?

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 传统的二叉树具有来自父级的指针(在本例中为* left和* right),并且您将使用“深度优先搜索”算法遍历整个树(基本上,您会递归地左移直到节点用完,然后向右),用伪代码

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. 然后它将回溯,处理正确的节点及其从属节点,并继续进行直到它访问了每个节点。

The struct you declared is not a tree, it is similar to a doubly-linked list. 您声明的struct不是树,它类似于双向链表。

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: 如果要在每个节点上调用birthdaygift()

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 . 其他树遍历算法的讨论可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM