我正在尝试创建一个可以打印节点及其所有子节点的函数,但是我正在尝试使其高效且递归。但这实际上不起作用。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define SIZE 100
typedef struct tree {
int value;
struct tree *child, *sibling, *parent;
} *Tree;
Tree initTree(int value) {
Tree root = malloc(sizeof(struct tree));
root->value = value;
root->parent = NULL;
root->child = NULL;
root->sibling = NULL;
return root;
}
void drawTreeHelper(Tree tree, FILE* stream) {
Tree tmp;
if (tree == NULL) {
return;
}
fprintf(stream, " %ld[label=\"%d\", fillcolor=red]\n", (intptr_t) tree, tree->value);
tmp = tree->child;
while (tmp != NULL) {
fprintf(stream, " %ld -> %ld \n", (intptr_t) tree, (intptr_t) tmp);
drawTreeHelper(tmp, stream);
tmp = tmp->sibling;
}
}
void drawTree(Tree tree, char *fileName) {
FILE* stream = fopen("test.dot", "w");
char buffer[SIZE];
fprintf(stream, "digraph tree {\n");
fprintf(stream, " node [fontname=\"Arial\", shape=circle, style=filled, fillcolor=yellow];\n");
if (tree == NULL)
fprintf(stream, "\n");
else if (!tree->child)
fprintf(stream, " %ld [label=\"%d\"];\n", (intptr_t) tree, tree->value);
else
drawTreeHelper(tree, stream);
fprintf(stream, "}\n");
fclose(stream);
sprintf(buffer, "dot test.dot | neato -n -Tpng -o %s", fileName);
system(buffer);
}
Tree uniteTries(Tree child, Tree parent)
{
if (parent)
{
if (!parent->child) parent->child = child;
else
{
Tree iter = parent->child;
while (iter->sibling) iter = iter->sibling;
iter->sibling = child;
}
}
return parent;
}
Tree uniteForest(Tree root, Tree *forest, int n)
{
int i;
for (i = 0; i < n; ++i)
{
if (forest[i]) root = uniteTries(forest[i], forest[i]->parent);
}
root = forest[0];
return root;
}
void printParentChildRec(Tree root)
{
if(!root) return;
printf("%d ", root->value);
printParentChildRec(root->sibling);
printParentChildRec(root->child);
}
int main() {
int i;
char buffer[SIZE];
Tree *forest = malloc(6 * sizeof(Tree));
for (i = 0; i < 6; i++) {
forest[i] = initTree(i);
}
forest[1]->parent = forest[0];
forest[2]->parent = forest[0];
forest[3]->parent = forest[0];
forest[4]->parent = forest[1];
forest[5]->parent = forest[1];
Tree root = uniteForest(root, forest, 6);
printParentChildRec(root);
drawTree(root, "tree.png");
return 0;
}
此代码将为您提供一个可验证的示例,这是我尝试做的事情:
void printParentChildRec(Tree root) {
if (!root)
return;
printf("%d ", root->value);
printParentChildRec(root->sibling);
printParentChildRec(root->child);
}
我得到的结果只是0 1 2 3 4 5
,它是所有节点,但是我想打印如下内容:
0 1 2 3
1 4 5
2
3
4
5
答案 0 :(得分:0)
您的代码中存在一些问题:
intptr_t
打印%ld
值,这在intptr_t
不是long
的别名且具有不同大小的平台(例如Windows 64)上是未定义的行为位。该类型没有特定的printf
格式,您应该将值重铸为(long)(intptr_t)tree
或(long long)(intptr_t)tree
并使用%lld
或它们的未签名版本或使用{{1 }}格式为%p
。您的代码中还有更多问题:
(void *)tree
中,main()
将未定义的变量Tree root = uniteForest(root, forest, 6);
传递给root
。uniteForest
中的自变量root
,它仅用于存储临时结果。您只需删除参数并将代码简化为:
Tree uniteForest(Tree root, Tree *forest, int n)
Tree uniteForest(Tree *forest, int n) {
for (int i = 0; i < n; i++) {
if (forest[i])
uniteTries(forest[i], forest[i]->parent);
}
return forest[0];
}
仅打印树的根,因此main
的值及其后代的值是递归的。相反,您要打印节点及其直接子节点的值,然后为每个子节点然后递归。
这是更正的版本:
forest[0]
输出:
0 1 2 3 1 4 5 4 5 2 3
答案 1 :(得分:0)
我认为您的统一树功能是这里的问题。我用调试器运行了这段代码,这就是从根开始的样子
这是在unite tree方法之后输入到打印功能中的根:
您能告诉我您在这里想要实现的目标吗,也许我可以为您提供帮助!