Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中构建一个N元树(递归?)_C_Recursion - Fatal编程技术网

在C语言中构建一个N元树(递归?)

在C语言中构建一个N元树(递归?),c,recursion,C,Recursion,我必须构建一个树,从一个字符串开始,它根据一些转换规则不断创建新节点 例如: 给定一个字符串aab 以及以下两个转换规则: ab --> bba b --> ba 需要构建以下树: 请注意,构建是在广度模式下完成的。在每个步骤中,我为当前节点的每个子字符串应用所有转换规则,这些子字符串将是子字符串 以下是我到目前为止的情况: //Representing the n_ary tree typedef struct { char *value; struct t_c

我必须构建一个树,从一个字符串开始,它根据一些转换规则不断创建新节点

例如:

给定一个字符串
aab
以及以下两个转换规则:

ab --> bba
b --> ba
需要构建以下树:

请注意,构建是在广度模式下完成的。在每个步骤中,我为当前节点的每个子字符串应用所有转换规则,这些子字符串将是子字符串

以下是我到目前为止的情况:

//Representing the n_ary tree
typedef struct {
    char *value;
    struct t_children_list *children;
} tree;

typedef struct t_children_list {
    tree *child;
    struct t_children_list *next;
} children_list;

void initializeNode(tree **node, char *input)
{
  if((*node = malloc(sizeof(tree))) == NULL) { abort(); }
  (*node)->value = input;
  (*node)->children = NULL;
}

void createChildrenList(children_list **children, tree *transformation)
{
    if((*children = malloc(sizeof(children_list))) == NULL) { abort(); }
    (*children)->child = transformation;
    (*children)->next = NULL;
}

//Given a node, and a needle with a replacement. It will add the childrens to that node. 
void addTransformationsToNode(tree **origin, char *needle, char *replacement)
{
    char *str = (*origin)->value;
    for (char *p = str; *p != '\0'; p++) {
       //Logic to find the value of str_... Not relevant
             tree *transformation = NULL;
            initializeNode(&transformation, str_);
            //Add node to origin children list
            // If node doesn't have children yet, create a new list
            // Otherwise, add to end of children list
            children_list *children = NULL;
            createChildrenList(&children, transformation);
            if ((*origin)->children == NULL) {
                (*origin)->children = children;
            } else {
                children_list *current = (*origin)->children;
                while (current->next != NULL) {
                    current = current->next;
                }
                current->next = children;
            }
        }
    }

  }

void main()
{
  // Create the tree
  char *input = "aab";
  char *target = "bababab";
  tree *my_tree = NULL;
  initializeNode(&my_tree, input);

  addTransformationsToNode(&my_tree, "ab", "bba");
  addTransformationsToNode(&my_tree, "b", "ba");

}
这对于第一个级别是正确的。但我正在寻找一种方法,可以对每个节点和该节点的子节点执行相同的操作。所以,我从原点开始,找到所有的变换,然后对于到达变换,也做同样的事情。我看不出我怎么能递归地做到这一点

谢谢

对于“宽度优先”,您可能希望查看通用二叉树(可以为任何树构造),其中每个节点都链接到第一个子节点和下一个兄弟节点。您可以构建二叉树(呼吸优先),然后转换为n元

从单个字符串生成一代,将结果放入节点列表中,由下一个同级节点链接。下一代是从列表中的每个节点构建一代

通过迭代或递归,可以使用重复来协调应用于一个节点的调用序列

addTransformationsToNode(&my_tree, "ab", "bba");

addTransformationsToNode(&my_tree->children->child, "ab", "bba");
addTransformationsToNode(&my_tree->children->next->child, "ab", "bba");
addTransformationsToNode(&my_tree->children->next->next->child, "ab", "bba");

addTransformationsToNode(&my_tree->children->child->children->child, "ab", "bba");
addTransformationsToNode(&my_tree->children->child->children->next->child, "ab", "bba");
因此,对于主体,您将遵循下一个指针,并为每个子级调用
addTransformationsNode
(我将在循环中这样做)。然后,您可以递归并对每个子级的子级执行相同的操作

您需要一个额外的参数来控制递归的深度:某种结束树构造的方法


我试着写函数,结果弄糊涂了。我认为您的
儿童列表
结构过于复杂。我会从更简单的事情开始

typedef struct tree {
    char *val;
    struct tree *first_child;
    struct tree *next_sibling;
} tree;

tree *newnode(char *val){
    tree *node;
    node = malloc(sizeof(*node));
    if (node) {
        node->val = val;
        node->first_child = NULL;
        node->next_sibling = NULL;
    }
    return node;
}

void printtree(tree *node) {
    if (node) {
        if (node->val)
            printf("%s, ", node->val);
        printtree(node->next_sibling);
        puts("");
        printtree(node->first_child);
    }
}

事实上,我就是这样拥有它的。但是我没有看到如何递归地构建树…嗯。是 啊我发现迭代思考要容易得多。luser droog:迭代也可以,但是我如何为每个节点的每个子节点都这样做呢?luser droog感谢您的编辑。我看到这是静态的,但我没有看到的是动态的。我的意思是,递归/迭代会是什么样子?嗯..我有子元素列表,因为我认为树中的一个节点有多个子元素,因此子元素列表是合适的…可能是