[英]Print Binary Tree in C
我如何在c中顯示二叉樹?
37
/ \
16 3
/ \ / \
9 26 58 81
\ / \
35 55 74
我可以逐級顯示
1 23 4567 89
但是我需要以確切的格式畫出這些BT
void printLevel(NoArvore * node , int level)
{
if(node != NULL && level == 0)
{
printf("%d",node->number);
printf("====");
}
else if(node != NULL){
printLevel(node->left,level-1);
printLevel(node->right,level-1);
}
}
void printElements(NoArvore *node)
{
int i,spaces;
int levelCount = getHeight(node);
int Hspace = getHeight(node);
for(i = 0; i < levelCount;i++)
{
for(spaces = 0; spaces <= (Hspace*Hspace)-(i+3);spaces++)
printf("=");
printLevel(node,i);
printf("\n");
Hspace--;
}
}
用這個代碼,我有這個輸出
==============37====
======16====76====
9====26====58====81====
35====55====74====
@Stephan Schlecht是否可以使此功能更通用? 如果在其他樹中使用,我有這個輸出
39
\
87
/ \
47 92
\ \
67 97
/ \ \
62 72 99
/
56
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.