From 71eb84f190a376496d4b58a6f518aa2b71982f40 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 12 Jun 2018 17:57:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8E=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/list.h | Bin 24428 -> 24778 bytes tree/tree.c | 2 +- tree/tree.h | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 1 deletion(-) diff --git a/list/list.h b/list/list.h index adc3808f6fc848355fea0904ee9f8fc24a2ad301..1e9faf6c9cc8545f3fb365f16e11ff94b73971a8 100644 GIT binary patch delta 534 zcmaE}kMYz&#tj9`>~0MC3`GonlQWsC#fupd8PXV18GM0aDL@(~r!THNS%4*gfIQda zX)H#(P(2V6HXmovW`$d!FRnOw9hbmn87?&zxZp!+(a8aP8$?m`!EE0Aldl=!#8RRC zc$`qWd6RGnGcE<2^~GaZP#rh9TiOurj6?(3$qxK1le2_`;9?J@9YwJQ$>e@**~urA zcqadnmYSR+!-qpvyrJyoc{1x5;Rd?OnZwy2e;<(Ln7lxOWAeHfp2^GPB4BcxzsoIW z;zkOP$qxgiCkyznOnxJW>^jNG_Y~t`nkIjj3!i+>muvGhr7Ik8L7=xbH)y|r2lwVg VgLVP9EHpwU%W&yzwsX{z0{~Kki);V@ delta 309 zcmX?gknznv#tj9`lMDDoCQo8c;0$I+WJqI3W$>N+kVA3u2TqR3Ml4=fME9_mZDQ48 zo$SIXH95h_W^*6cFXqV=@_dt*@NL*!z+b~WnL~kZGKcUgG`$x>I40M`a7?z7;G5jQ z%QbmHtj=aTkrHNNlu5i`o?N6RGP#GJYjTf{(BvJ4Vv{%UvrO)h5dv8~NttExJ{b)p zlm5#1Bk?vGNpG%`UB}3*$)GUVQCE6$fHFv5(`F8Z1x%B-Dd|BBNdme&VDkaxdmJDO WHXqV?!LoUWVS@n3tD6&?vg82LN@KSG diff --git a/tree/tree.c b/tree/tree.c index ca3e084..479d186 100644 --- a/tree/tree.c +++ b/tree/tree.c @@ -1,5 +1,5 @@ #include"tree.h" int tree(void) { - + return 0; } \ No newline at end of file diff --git a/tree/tree.h b/tree/tree.h index a46e524..6ceca66 100644 --- a/tree/tree.h +++ b/tree/tree.h @@ -20,6 +20,12 @@ typedef struct tree TNode *root; }Tree; +List *tree_list = NULL; +List *tnode_list = NULL; +int if_safeModeForTree = 0; +int safeModeForTree(int ifon); +int releaseAllForTree(void); + TNode *initTNode(void); Tree *initTree(void); int *initMallocValueForTNode(TNode *p_tnode, char *type, void *value); @@ -39,6 +45,36 @@ int removeChildByIndex(TNode *p_tnode, unsigned long long index); int removeChildByValue(TNode *p_tnode, char *type, void *value); int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); +int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); + +int releaseTree(Tree *p_tree); +int releaseOnlyTree(Tree *p_tree); +int releaseTNode(TNode *p_tnode); +int releaseOnlyTNode(Tree *p_tree); + +int safeModeForTree(int ifon) { + if (ifon == 1) { + if (tnode_list == NULL && tree_list == NULL) { + tnode_list = (List *)malloc(sizeof(List)); + tree_list = (List *)malloc(sizeof(List)); + + tree_list->head = NULL; + tree_list->length = 0; + tree_list->tail = NULL; + + tnode_list->head = NULL; + tnode_list->length = 0; + tnode_list->tail = NULL; + + if_safeModeForTree = 1; + } + else { + return -1; + } + } + + return ifon; +} TNode *initTNode(void) { TNode *p_tnode = (TNode *)malloc(sizeof(TNode)); @@ -50,6 +86,21 @@ TNode *initTNode(void) { p_tnode->type = NULL; p_tnode->home = initList(); p_tnode->room = NULL; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + Node *s_node = initNode(); + initMallocValueForTNode(s_node, "pointer", (void *)p_tnode); + insertInTail(tnode_list, s_node); + if_safeModeForNode = 1; + } + else + { + Node *s_node = initNode(); + initMallocValueForTNode(s_node, "pointer", (void *)p_tnode); + insertInTail(tnode_list, s_node); + } + } return p_tnode; } @@ -57,6 +108,21 @@ Tree *initTree(void) { Tree *p_tree = (Tree *)malloc(sizeof(Tree)); p_tree->id = getId(); p_tree->root = NULL; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + Node *s_node = initNode(); + initMallocValueForTNode(s_node, "pointer", (void *)p_tree); + if_safeModeForNode = 1; + insertInTail(tree_list, s_node); + } + else + { + Node *s_node = initNode(); + initMallocValueForTNode(s_node, "pointer", (void *)p_tree); + insertInTail(tree_list, s_node); + } + } return p_tree; } @@ -322,3 +388,104 @@ int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned return 0; } +int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + TNode *p_tnode = p_tree->root; + unsigned long long height = 0; + if (p_tnode != NULL) { + int func_back = func(p_tnode, height); + while (func_back > -2) { + if (func_back > -1) { + p_tnode = getChildByIndex(func_back,func_back); + func(p_tnode, height + 1); + } + else + { + p_tnode = p_tnode->father; + func(p_tnode, height - 1); + + } + } + } + return 0; +} +int releaseTNode(TNode *p_tnode) { + if(p_tnode->child_num == 0){ + releaseList(p_tnode->home); + if (p_tnode->father != NULL) { + removeChildById(p_tnode->father, p_tnode->id); + } + 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; + p_tnode->type = NULL; + p_tnode->id = 0; + p_tnode->if_malloc = 0; + free(p_tnode); + } + return 0; +} + +int _doreleaseTree(TNode *p_tnode, int height); + +int releaseTree(Tree *p_tree) { + TreeThroughUp(p_tree, _doreleaseTree); + p_tree->root = NULL; + p_tree->id = 0; + free(p_tree); +} + +int _doreleaseTree(TNode *p_tnode, int height) { + releaseTNode(p_tnode); + return 0; +} + + +int releaseOnlyTree(Tree *p_tree) { + p_tree->id = 0; + p_tree->root = NULL; + free(p_tree); + return 0; +} + +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); + } + } + p_tnode->value = NULL; + p_tnode->type = NULL; + p_tnode->id = 0; + p_tnode->if_malloc = 0; + free(p_tnode); + return 0; +} + +int releaseAllForTree(void) { + if (if_safeModeForTree) { + if_safeModeForTree = 0; + Node *p_node = tnode_list->head; + while (p_node != NULL) { + TNode *p_tnode = (TNode *)p_node->value; + releaseOnlyTNode(p_tnode); + } + Node *p_node = tree_list->head; + while (p_node != NULL) { + Tree *p_tree = (Tree *)p_node->value; + releaseOnlyTree(p_tree); + } + releaseList(tnode_list); + releaseList(tree_list); + } + return 0; +} \ No newline at end of file