diff --git a/list/list.c b/list/list.c index e434c5a..5807c18 100644 Binary files a/list/list.c and b/list/list.c differ diff --git a/list/list_expand.c b/list/list_expand.c index a5f8fb3..3a756bc 100644 --- a/list/list_expand.c +++ b/list/list_expand.c @@ -120,7 +120,7 @@ void printList(List *p_list) { printf("%s", (char *)(p_node->value)); } else if (!strcmp(p_node->type, "pointer")) { - printf("%s", (char *)(p_node->value)); + printf("%p", (char *)(p_node->value)); } else if (!strcmp(p_node->type, "list")) { printList((List *)p_node->value); diff --git a/test.c b/test.c index c21a0f5..14423f9 100644 --- a/test.c +++ b/test.c @@ -1,5 +1,6 @@ #include "test.h" + int list(void) { init_rand(); safeModeForNode(1); @@ -39,6 +40,15 @@ int list(void) { int tree(void) { safeModeForTree(1); Tree *t_tree = initTree(); + TNode *t_tnode = tnodeWithInt(1), *cr_tnode = tnodeWithInt(3),*cl_tnode = tnodeWithInt(2); + addChildInRight(t_tnode, cl_tnode); + addChildInRight(t_tnode, cr_tnode); + addChildInRight(cl_tnode, tnodeWithInt(4)); + addChildInRight(cl_tnode, tnodeWithInt(5)); + addChildInRight(cr_tnode, tnodeWithInt(6)); + addChildInRight(cr_tnode, tnodeWithInt(7)); + printTNodeWithFamily(t_tnode, 0); + //removeChildByIndex(t_tnode, 0); releaseAllForTree(); return 0; } diff --git a/test.h b/test.h index cba9876..3e171ce 100644 --- a/test.h +++ b/test.h @@ -6,6 +6,7 @@ #include "list/list_expand.h" #include "stack/stack_expand.h" #include "tree/tree.h" +#include "tree/tree_expand.h" int stack(void); int list(void); diff --git a/tree/tree.c b/tree/tree.c index 8d981e8..0d42a14 100644 --- a/tree/tree.c +++ b/tree/tree.c @@ -1,5 +1,9 @@ #include"tree.h" +unsigned long long target_id = 0; +TNode *target_value_id = NULL; +int if_safeModeForTree = 0; + int safeModeForTree(int ifon) { if (ifon == 1) { if (tnode_list == NULL && tree_list == NULL) { @@ -86,6 +90,7 @@ int addChildInLeft(TNode *f_tnode, TNode *c_tnode) { initMallocValueForNode(p_node, "pointer", c_tnode); insertInHead(f_tnode->home, p_node); c_tnode->father = f_tnode; + c_tnode->room = p_node; f_tnode->child_num++; return 0; } @@ -95,6 +100,7 @@ int addChildInRight(TNode *f_tnode, TNode *c_tnode) { initMallocValueForNode(p_node, "pointer", c_tnode); insertInTail(f_tnode->home, p_node); c_tnode->father = f_tnode; + c_tnode->room = p_node; f_tnode->child_num++; return 0; } @@ -246,7 +252,7 @@ TNode *getChildByIndex(TNode *p_tnode, unsigned long long index) { int m_index = 0; if (index < p_tnode->child_num - 1) { - while (p_node != NULL) { + while (p_node != NULL && m_index < index) { m_index++; p_node = p_node->next; } @@ -273,7 +279,7 @@ unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode) { int removeChildByIndex(TNode *p_tnode, unsigned long long index) { TNode *t_tnode = getChildByIndex(p_tnode, index); if (t_tnode != NULL) { - TNode *p_fnode = t_tnode->father; + TNode *p_fnode = t_tnode->father; p_fnode->child_num--; removeById(p_fnode->home, t_tnode->room->id); releaseOnlyNode(t_tnode->room); @@ -402,12 +408,14 @@ int releaseOnlyTree(Tree *p_tree) { int releaseOnlyTNode(TNode *p_tnode) { releaseList(p_tnode->home); - if (strcmp(p_tnode->type, "pointer")) { - if (!strcmp(p_tnode->type, "list")) { - releaseList((List *)p_tnode->value); - } - else { - free(p_tnode->value); + if (p_tnode->if_malloc) { + if (strcmp(p_tnode->type, "pointer")) { + if (!strcmp(p_tnode->type, "list")) { + releaseList((List *)p_tnode->value); + } + else { + free(p_tnode->value); + } } } p_tnode->value = NULL; @@ -425,14 +433,21 @@ int releaseAllForTree(void) { while (p_node != NULL) { TNode *p_tnode = (TNode *)p_node->value; releaseOnlyTNode(p_tnode); + p_node = p_node->next; } p_node = tree_list->head; while (p_node != NULL) { Tree *p_tree = (Tree *)p_node->value; releaseOnlyTree(p_tree); + p_node = p_node->next; } releaseList(tnode_list); releaseList(tree_list); } return 0; } + +int setRoot(Tree *p_tree, TNode *p_tnode) { + p_tree->root = p_tnode; + return 0; +} diff --git a/tree/tree.h b/tree/tree.h index 3f5a7ae..ac06ac7 100644 --- a/tree/tree.h +++ b/tree/tree.h @@ -20,8 +20,7 @@ typedef struct tree TNode *root; }Tree; -List *tree_list; -List *tnode_list; + int if_safeModeForTree; int safeModeForTree(int ifon); int releaseAllForTree(void); @@ -58,6 +57,8 @@ int releaseOnlyTree(Tree *p_tree); int releaseTNode(TNode *p_tnode); int releaseOnlyTNode(TNode *p_tnode); +int setRoot(Tree *p_tree, TNode *p_tnode); + char *target_type; void *target_value; TNode *target_value_value; @@ -65,5 +66,9 @@ TNode *target_value_value; unsigned long long target_id; TNode *target_value_id; +List *tree_list; +List *tnode_list; +int if_safeModeForTree; + #endif \ No newline at end of file diff --git a/tree/tree_expand.c b/tree/tree_expand.c new file mode 100644 index 0000000..478929a --- /dev/null +++ b/tree/tree_expand.c @@ -0,0 +1,197 @@ +#include "tree_expand.h" + +TNode *tnodeWithInt(int temp) { + TNode *p_tnode = initTNode(); + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + initMallocValueForTNode(p_tnode, "int", p_temp); + return p_tnode; +} + +TNode *tnodeWithDouble(double temp) { + TNode *p_tnode = initTNode(); + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + initMallocValueForTNode(p_tnode, "double", p_temp); + return p_tnode; +} +TNode *tnodeWithString(char *temp) { + TNode *p_tnode = initTNode(); + char *p_temp = (char *)malloc(sizeof(temp)); + strcpy_s(p_temp, sizeof(p_temp), temp); + initMallocValueForTNode(p_tnode, "double", p_temp); + return p_tnode; +} + +TNode *tnodeWithPointer(void *temp) { + TNode *p_tnode = initTNode(); + initMallocValueForTNode(p_tnode, "pointer", temp); + return p_tnode; +} + +int getValueByIntForTree(TNode *p_tnode) { + if (!strcmp(p_tnode->type, "int")) { + return *(int *)p_tnode->value; + } + return -1; +} + +double getValueByDoubleForTree(TNode *p_tnode) { + if (!strcmp(p_tnode->type, "double")) { + return *(double *)p_tnode->value; + } + return -1; +} + +char *getValueByStringForTree(TNode *p_tnode) { + if (!strcmp(p_tnode->type, "string")) { + return (char *)p_tnode->value; + } + return NULL; +} + +void *getValueByPointerForTree(TNode *p_tnode) { + if (!strcmp(p_tnode->type, "pointer")) { + return p_tnode->value; + } + return NULL; +} + +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"); + 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 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; +} + +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"); + 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 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); + } + + 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; +} + +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"); + 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 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); + } + + 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; +} + +int printTree(Tree *p_tree) { + printf("###"); + printf("Tree(id: %llu)",p_tree->id); + printTNodeWithFamily(p_tree->root,0); + return 0; +} \ No newline at end of file diff --git a/tree/tree_expand.h b/tree/tree_expand.h new file mode 100644 index 0000000..d8d882a --- /dev/null +++ b/tree/tree_expand.h @@ -0,0 +1,21 @@ +#ifndef TREE_EXPAND_H +#define TREE_EXPAND_H + +#include "tree.h" + +TNode *tnodeWithInt(int); +TNode *tnodeWithDouble(double); +TNode *tnodeWithString(char *); +TNode *tnodeWithPointer(void *); + +int getValueByIntForTree(TNode *); +double getValueByDoubleForTree(TNode *); +char *getValueByStringForTree(TNode *); +void *getValueByPointerForTree(TNode *); + +int printTree(Tree *p_tree); +int printTNodeWithHome(TNode *p_tnode, int priority); +int printTNodeWithFamily(TNode *p_tnode, int priority); +int printTNode(TNode *p_tnode, int priority); + +#endif