文字列から始めて、いくつかの変換規則に従って新しいノードを作成し続けるツリーを構築する必要があります。
例えば:
与えられた文字列aab
と、次の 2 つの変換規則:
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");
}
これは、最初のレベルでは正しく機能します。しかし、各ノードとそのノードの子に対して同じことができる方法を探しています。したがって、原点から始めて、すべての変換を見つけてから、リーチ変換についても同じことを行います。これを再帰的に行う方法がわかりません...
ありがとう!