打印tree数据结构
使用递归实现,每次打印子节点前,都要把前面的“|”和空格打印出来,代码如下:
方法一:
#include "stdlib.h" #include "stdio.h" #include "string.h" typedef struct Node { char *name; int num; // 子节点数量 struct Node **children; // 子节点 } NodeT; void PrintTree(char *gap, NodeT *node) { printf("%s\r\n", node->name); if (node->children == NULL) { return; } char *newGap = malloc(strlen(gap) + strlen(" ")); for (int i = node->num - 1; i >= 0; i--) { printf("%s", gap); if (i == 0) { printf("└——— "); sprintf(newGap, "%s ", gap); } else { printf("├─── "); sprintf(newGap, "%s│ ", gap); } PrintTree(newGap, node->children[i]); }
free(newGap); } int main(void) { NodeT *node11 = malloc(sizeof(NodeT)); node11->name = malloc(sizeof('l') * 2); memset(node11->name, '\0', sizeof('l') * 2); node11->name[0] = 'l'; node11->name[1] = '\0'; node11->num = 0; node11->children = NULL; NodeT** children6 = malloc(sizeof(NodeT*)); memset(children6, '\0', sizeof(NodeT*)); children6[0] = node11; NodeT *node10 = malloc(sizeof(NodeT)); node10->name = malloc(sizeof('k') * 2); memset(node10->name, '\0', sizeof('k') * 2); node10->name[0] = 'k'; node10->name[1] = '\0'; node10->num = 1; node10->children = children6; NodeT *node9 = malloc(sizeof(NodeT)); node9->name = malloc(sizeof('j') * 2); memset(node9->name, '\0', sizeof('j') * 2); node9->name[0] = 'j'; node9->name[1] = '\0'; node9->num = 0; node9->children = NULL; NodeT **children5 = malloc(sizeof(NodeT*) * 2); memset(children5, '\0', sizeof(NodeT*) * 2); children5[0] = node9; children5[1] = node10; NodeT *node8 = malloc(sizeof(NodeT)); node8->name = malloc(sizeof('i')); memset(node8->name, '\0', sizeof('i')); node8->name[0] = 'i'; node8->num = 0; node8->children = NULL; NodeT **children4 = malloc(sizeof(NodeT *) * 1); memset(children4, '\0', sizeof(NodeT *)); children4[0] = node8; NodeT *node7 = malloc(sizeof(NodeT)); node7->name = malloc(sizeof('h')); memset(node7->name, '\0', sizeof('h')); node7->name[0] = 'h'; node7->num = 0; node7->children = NULL; NodeT *node6 = malloc(sizeof(NodeT)); node6->name = malloc(sizeof('g')); memset(node6->name, '\0', sizeof('g')); node6->name[0] = 'g'; node6->num = 0; node6->children = NULL; NodeT **children3 = malloc(sizeof(NodeT *) * 2); memset(children3, '\0', sizeof(NodeT *)); children3[0] = node6; children3[1] = node7; NodeT *node5 = malloc(sizeof(NodeT)); node5->name = malloc(sizeof('f')); memset(node5->name, '\0', sizeof('f')); node5->name[0] = 'f'; node5->num = 0; node5->children = NULL; NodeT *node4 = malloc(sizeof(NodeT)); node4->name = malloc(sizeof('e')); memset(node4->name, '\0', sizeof('e')); node4->name[0] = 'e'; node4->num = 1; node4->children = children4; NodeT *node3 = malloc(sizeof(NodeT)); node3->name = malloc(sizeof('d')); memset(node3->name, '\0', sizeof('d')); node3->name[0] = 'd'; node3->num = 2; node3->children = children5; NodeT **children2 = malloc(sizeof(NodeT *) * 3); memset(children2, '\0', sizeof(NodeT *)); children2[0] = node3; children2[1] = node4; children2[2] = node5; NodeT *node1 = malloc(sizeof(NodeT)); node1->name = malloc(sizeof('b')); memset(node1->name, '\0', sizeof('b')); node1->name[0] = 'b'; node1->num = 2; node1->children = children3; NodeT *node2 = malloc(sizeof(NodeT)); node2->name = malloc(sizeof('c')); memset(node2->name, '\0', sizeof('c')); node2->name[0] = 'c'; node2->num = 3; node2->children = children2; NodeT **children1 = malloc(sizeof(NodeT *) * 2); memset(children1, '\0', sizeof(NodeT *)); children1[0] = node1; children1[1] = node2; NodeT *node = malloc(sizeof(NodeT)); node->name = malloc(sizeof('a')*2); memset(node->name, '\0', sizeof('a')*2); node->name[0] = 'a'; node->name[1] = '\0'; node->num = 2; node->children = children1; char *gap = malloc(sizeof('\0')); memset(gap, '\0', sizeof('\0')); PrintTree(gap, node); return 0; }
打印多行:
void PrintTree(char *gap, NodeT *node) { printf("%s\r\n", node->name); printf("%s", gap); if (node->children == NULL) { printf(" =======\n"); return; } printf("│ =======\n"); char *newGap = malloc(strlen(gap) + strlen(" ")); for (int i = node->num - 1; i >= 0; i--) { printf("%s", gap); if (i == 0) { sprintf(newGap, "%s ", gap); printf("└——— "); } else { sprintf(newGap, "%s│ ", gap); printf("├─── "); } PrintTree(newGap, node->children[i]); } free(newGap); }
还有一种打印格式:
方法一:
void PrintTree(char *gap, NodeT *node) { printf("%s\r\n", node->name); printf("%s ", gap); printf("*******\n"); if (node->children == NULL) { return; } char *newGap = malloc(strlen(gap) + strlen(" ")); for (int i = node->num - 1; i >= 0; i--) { printf("%s -> ", gap); sprintf(newGap, "%s ", gap); PrintTree(newGap, node->children[i]); } free(newGap); }
方法二:
void PrintTree(int level, NodeT *node) { printf("%s\r\n", node->name); for (int i=0;i<2+level*6;i++){ printf(" "); } printf("*******\n"); if (node->children == NULL) { return; } for (int i = node->num - 1; i >= 0; i--) { for (int j=0;j<2+level*6;j++){ printf(" "); } printf("-> "); PrintTree(level+1, node->children[i]); } }