测试与debug。

This commit is contained in:
Saturneric 2018-06-13 16:32:39 +08:00
parent 896e454c40
commit 880fa14d10
3 changed files with 133 additions and 115 deletions

16
test.c
View File

@ -37,6 +37,12 @@ int list(void) {
return 0; return 0;
} }
int _useTreeThroughDown(TNode *p_tnode, unsigned long long height) {
printTNode(p_tnode,0);
return 0;
}
int tree(void) { int tree(void) {
safeModeForTree(1); safeModeForTree(1);
Tree *t_tree = initTree(); Tree *t_tree = initTree();
@ -44,12 +50,14 @@ int tree(void) {
addChildInRight(t_tnode, cl_tnode); addChildInRight(t_tnode, cl_tnode);
addChildInRight(t_tnode, cr_tnode); addChildInRight(t_tnode, cr_tnode);
addChildInRight(cl_tnode, tnodeWithInt(4)); 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(6));
addChildInRight(cr_tnode, tnodeWithInt(7)); addChildInRight(cr_tnode, tnodeWithInt(7));
addChildInRight(gs_tnode, tnodeWithInt(8));
removeChildByIndex(cr_tnode, 1); setRoot(t_tree, t_tnode);
printTNodeWithFamily(t_tnode, 0); TreeThroughUp(t_tree, _useTreeThroughDown);
//printTNodeWithFamily(t_tnode, 0);
releaseAllForTree(); releaseAllForTree();
return 0; return 0;
} }

View File

@ -107,7 +107,7 @@ int addChildInRight(TNode *f_tnode, TNode *c_tnode) {
TNode *getBrotherInLeft(TNode *p_tnode) { TNode *getBrotherInLeft(TNode *p_tnode) {
List *p_home = p_tnode->father->home; 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); unsigned long long index = getIndexByNode(p_home, p_node);
if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value); if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value);
return NULL; 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)) { int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
TNode *p_tnode = p_tree->root; TNode *p_tnode = p_tree->root;
if (p_tnode != NULL) { if (p_tnode != NULL) {
func(p_tnode, 0);
if (p_tnode->child_num > 0) { if (p_tnode->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) { for (int i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) { 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; 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)) { int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
TNode *p_tnode = p_tree->root; TNode *p_tnode = p_tree->root;
if (p_tree->root != NULL) { if (p_tree->root != NULL) {
if (p_tree->root->child_num > 0) {
func(p_tnode, 0); func(p_tnode, 0);
if (p_tree->root->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) { for (int i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) {
break; break;

View File

@ -58,9 +58,11 @@ void *getValueByPointerForTree(TNode *p_tnode) {
} }
int printTNode(TNode *p_tnode, int priority) { int printTNode(TNode *p_tnode, int priority) {
if (p_tnode != NULL) {
for (int i = 0; i < priority; i++) printf(" "); for (int i = 0; i < priority; i++) printf(" ");
if (priority == 0) printf("###"); if (priority == 0) printf("###");
else printf("#"); else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id); printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" "); for (int i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: "); printf("ifMalloc: ");
@ -89,8 +91,11 @@ int printTNode(TNode *p_tnode, int priority) {
} }
return 0; return 0;
} }
return -1;
}
int printTNodeWithHome(TNode *p_tnode,int priority) { int printTNodeWithHome(TNode *p_tnode,int priority) {
if (p_tnode != NULL) {
if (priority == 0) printf("###"); if (priority == 0) printf("###");
else printf("#"); else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id); printf("TNode(id: %llu)\n", p_tnode->id);
@ -138,8 +143,11 @@ int printTNodeWithHome(TNode *p_tnode,int priority) {
} }
return 0; return 0;
} }
return -1;
}
int printTNodeWithFamily(TNode *p_tnode, int priority) { int printTNodeWithFamily(TNode *p_tnode, int priority) {
if (p_tnode != NULL) {
for (int i = 0; i < priority; i++) printf(" "); for (int i = 0; i < priority; i++) printf(" ");
if (priority == 0) printf("###"); if (priority == 0) printf("###");
else printf("#"); else printf("#");
@ -188,6 +196,8 @@ int printTNodeWithFamily(TNode *p_tnode, int priority) {
} }
return 0; return 0;
} }
return -1;
}
int printTree(Tree *p_tree) { int printTree(Tree *p_tree) {
printf("###"); printf("###");