From 880fa14d10539cc0214c3cee782198c37bea1810 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 13 Jun 2018 16:32:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8Edebug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.c | 16 +++- tree/tree.c | 6 +- tree/tree_expand.c | 226 +++++++++++++++++++++++---------------------- 3 files changed, 133 insertions(+), 115 deletions(-) diff --git a/test.c b/test.c index 89b0134..4eaf628 100644 --- a/test.c +++ b/test.c @@ -37,6 +37,12 @@ int list(void) { return 0; } +int _useTreeThroughDown(TNode *p_tnode, unsigned long long height) { + printTNode(p_tnode,0); + return 0; +} + + int tree(void) { safeModeForTree(1); Tree *t_tree = initTree(); @@ -44,12 +50,14 @@ int tree(void) { addChildInRight(t_tnode, cl_tnode); addChildInRight(t_tnode, cr_tnode); addChildInRight(cl_tnode, tnodeWithInt(4)); - addChildInRight(cl_tnode, tnodeWithInt(5)); + TNode *gs_tnode = tnodeWithInt(5); + addChildInRight(cl_tnode,gs_tnode); addChildInRight(cr_tnode, tnodeWithInt(6)); addChildInRight(cr_tnode, tnodeWithInt(7)); - - removeChildByIndex(cr_tnode, 1); - printTNodeWithFamily(t_tnode, 0); + addChildInRight(gs_tnode, tnodeWithInt(8)); + setRoot(t_tree, t_tnode); + TreeThroughUp(t_tree, _useTreeThroughDown); + //printTNodeWithFamily(t_tnode, 0); releaseAllForTree(); return 0; } diff --git a/tree/tree.c b/tree/tree.c index 9c0c123..1bd035e 100644 --- a/tree/tree.c +++ b/tree/tree.c @@ -107,7 +107,7 @@ int addChildInRight(TNode *f_tnode, TNode *c_tnode) { TNode *getBrotherInLeft(TNode *p_tnode) { List *p_home = p_tnode->father->home; - Node *p_node = p_home->head; + Node *p_node = p_tnode->room; unsigned long long index = getIndexByNode(p_home, p_node); if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value); return NULL; @@ -293,7 +293,6 @@ int removeChildByIndex(TNode *p_tnode, unsigned long long index) { int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { TNode *p_tnode = p_tree->root; if (p_tnode != NULL) { - func(p_tnode, 0); if (p_tnode->child_num > 0) { for (int i = 0; i < p_tnode->child_num; i++) { if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) { @@ -301,6 +300,7 @@ int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) } } } + func(p_tnode, 0); } return 0; } @@ -320,8 +320,8 @@ int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned lo int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { TNode *p_tnode = p_tree->root; if (p_tree->root != NULL) { + func(p_tnode, 0); if (p_tree->root->child_num > 0) { - func(p_tnode, 0); for (int i = 0; i < p_tnode->child_num; i++) { if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { break; diff --git a/tree/tree_expand.c b/tree/tree_expand.c index 478929a..062934d 100644 --- a/tree/tree_expand.c +++ b/tree/tree_expand.c @@ -58,135 +58,145 @@ void *getValueByPointerForTree(TNode *p_tnode) { } int printTNode(TNode *p_tnode, int priority) { - for (int i = 0; i < priority; i++) printf(" "); - if (priority == 0) printf("###"); - else printf("#"); - printf("TNode(id: %llu)\n", p_tnode->id); - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("ifMalloc: "); - if (p_tnode->if_malloc) { - printf("YES\n"); + if (p_tnode != NULL) { + for (int i = 0; i < priority; i++) printf(" "); + if (priority == 0) printf("###"); + else printf("#"); + + printf("TNode(id: %llu)\n", p_tnode->id); for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Value(type: %s): ", p_tnode->type); - if (!strcmp(p_tnode->type, "int")) { - printf("%d\n", *(int *)(p_tnode->value)); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %s): ", p_tnode->type); + if (!strcmp(p_tnode->type, "int")) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "double")) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "string")) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "pointer")) { + printf("%p\n", (char *)(p_tnode->value)); + } } - else if (!strcmp(p_tnode->type, "double")) { - printf("%a\n", *(double *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "string")) { - printf("%s\n", (char *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "pointer")) { - printf("%p\n", (char *)(p_tnode->value)); + else printf("NO\n"); + + if (p_tnode->child_num > 0) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Child number: %llu\n", p_tnode->child_num); } + return 0; } - else printf("NO\n"); - - if (p_tnode->child_num > 0) { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Child number: %llu\n", p_tnode->child_num); - } - return 0; + return -1; } int printTNodeWithHome(TNode *p_tnode,int priority) { - if (priority == 0) printf("###"); - else printf("#"); - printf("TNode(id: %llu)\n", p_tnode->id); - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("ifMalloc: "); - if (p_tnode->if_malloc) { - printf("YES\n"); + if (p_tnode != NULL) { + if (priority == 0) printf("###"); + else printf("#"); + printf("TNode(id: %llu)\n", p_tnode->id); for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Value(type: %s): ", p_tnode->type); - if (!strcmp(p_tnode->type, "int")) { - printf("%d\n", *(int *)(p_tnode->value)); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %s): ", p_tnode->type); + if (!strcmp(p_tnode->type, "int")) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "double")) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "string")) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "pointer")) { + printf("%p\n", (char *)(p_tnode->value)); + } } - else if (!strcmp(p_tnode->type, "double")) { - printf("%a\n", *(double *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "string")) { - printf("%s\n", (char *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "pointer")) { - printf("%p\n", (char *)(p_tnode->value)); - } - } - else printf("NO\n"); + else printf("NO\n"); - if (p_tnode->father != NULL) { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Father id: %llu\n", p_tnode->father->id); - } - else - { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Father: NO\n"); - } - - if (p_tnode->child_num > 0) { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Child(number: %llu):\n", p_tnode->child_num); - } + if (p_tnode->father != NULL) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Father id: %llu\n", p_tnode->father->id); + } + else + { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Father: NO\n"); + } - List *p_home = p_tnode->home; - Node *p_node = p_home->head; - while (p_node != NULL) { - printTNode((TNode *)p_node->value,priority+2); - p_node = p_node->next; + if (p_tnode->child_num > 0) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Child(number: %llu):\n", p_tnode->child_num); + } + + List *p_home = p_tnode->home; + Node *p_node = p_home->head; + while (p_node != NULL) { + printTNode((TNode *)p_node->value, priority + 2); + p_node = p_node->next; + } + return 0; } - return 0; + return -1; } int printTNodeWithFamily(TNode *p_tnode, int priority) { - for (int i = 0; i < priority ; i++) printf(" "); - if (priority == 0) printf("###"); - else printf("#"); - printf("TNode(id: %llu)\n", p_tnode->id); - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("ifMalloc: "); - if (p_tnode->if_malloc) { - printf("YES\n"); + if (p_tnode != NULL) { + for (int i = 0; i < priority; i++) printf(" "); + if (priority == 0) printf("###"); + else printf("#"); + printf("TNode(id: %llu)\n", p_tnode->id); for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Value(type: %s): ", p_tnode->type); - if (!strcmp(p_tnode->type, "int")) { - printf("%d\n", *(int *)(p_tnode->value)); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %s): ", p_tnode->type); + if (!strcmp(p_tnode->type, "int")) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "double")) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "string")) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (!strcmp(p_tnode->type, "pointer")) { + printf("%p\n", (char *)(p_tnode->value)); + } } - else if (!strcmp(p_tnode->type, "double")) { - printf("%a\n", *(double *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "string")) { - printf("%s\n", (char *)(p_tnode->value)); - } - else if (!strcmp(p_tnode->type, "pointer")) { - printf("%p\n", (char *)(p_tnode->value)); - } - } - else printf("NO\n"); + else printf("NO\n"); - if (p_tnode->father != NULL) { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Father id: %llu\n", p_tnode->father->id); - } - else - { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Father: NO\n"); - } + if (p_tnode->father != NULL) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Father id: %llu\n", p_tnode->father->id); + } + else + { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Father: NO\n"); + } - if (p_tnode->child_num > 0) { - for (int i = 0; i < priority + 1; i++) printf(" "); - printf("Child(number: %llu):\n", p_tnode->child_num); - } + if (p_tnode->child_num > 0) { + for (int i = 0; i < priority + 1; i++) printf(" "); + printf("Child(number: %llu):\n", p_tnode->child_num); + } - List *p_home = p_tnode->home; - Node *p_node = p_home->head; - while (p_node != NULL) { - printTNodeWithFamily((TNode *)p_node->value, priority + 2); - p_node = p_node->next; + List *p_home = p_tnode->home; + Node *p_node = p_home->head; + while (p_node != NULL) { + printTNodeWithFamily((TNode *)p_node->value, priority + 2); + p_node = p_node->next; + } + return 0; } - return 0; + return -1; } int printTree(Tree *p_tree) {