[英]Print tree node and all of it's childs efficiently
我試圖創建一個可以打印節點及其所有子節點的函數,但是我試圖使其高效且遞歸。 但這並沒有真正起作用。
#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
您的代碼中存在一些問題:
%ld
打印intptr_t
值,在intptr_t
long
不是別名並且大小不同的平台(例如Windows 64位)上,這是未定義的行為。 該類型沒有特定的printf
格式,您應該將值重鑄為(long)(intptr_t)tree
或(long long)(intptr_t)tree
並使用%lld
或它們的無符號版本,或者將%p
格式與(void *)tree
。 這是您的代碼中的更多問題:
main()
, Tree root = uniteForest(root, forest, 6);
將未定義的變量root
傳遞給uniteForest
。 Tree uniteForest(Tree root, Tree *forest, int n)
參數root
永遠不會使用,它僅用於存儲臨時結果。 您只需刪除參數並將代碼簡化為:
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]
及其后代的值是遞歸的。 相反,您希望打印節點及其直接子節點的值, 然后為每個子節點遞歸。
這是更正的版本:
void printParentChildRec(Tree node) {
if (node) {
printf("%d ", node->value);
for (Tree child = node->child; child; child = child->sibling) {
printf("%d ", child->value);
}
printf("\n");
for (Tree child = node->child; child; child = child->sibling) {
printParentChildRec(child);
}
}
}
輸出:
0 1 2 3 1 4 5 4 5 2 3
我認為您的統一樹功能是這里的問題。 我用調試器運行了這段代碼,這從根本上看起來就是
這是在unite trees方法之后輸入到print函數中的根:
您能告訴我您在這里想要實現的目標嗎,也許我可以為您提供幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.