Implement node tree printing

This commit is contained in:
Dendy 2021-01-24 12:41:12 +01:00
parent 1559a0d51c
commit 1408cca6b7
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
2 changed files with 38 additions and 34 deletions

View File

@ -3,40 +3,31 @@
#include "node.h" #include "node.h"
int main(int argc, char** argv){ int main(int argc, char** argv){
NOD_Node* node = NOD_CreateNode("test node"); NOD_Node* node = NOD_CreateNode("Main Node");
NOD_Node* testchild = NOD_CreateNode("I'm just a child"); NOD_Node* child1 = NOD_CreateNode("First child");
NOD_Node* otherchild = NOD_CreateNode("Another child"); NOD_Node* child1child1 = NOD_CreateNode("Child of first child");
NOD_Node* justtotest = NOD_CreateNode("This one is a test"); NOD_Node* child2 = NOD_CreateNode("Second child");
NOD_Node* child3 = NOD_CreateNode("Third child");
NOD_Node* child3child1 = NOD_CreateNode("First child of third child");
NOD_Node* child3child2 = NOD_CreateNode("Second child of third child");
NOD_NodeAddChild(node, testchild); NOD_NodeAddChild(node, child1);
NOD_NodeAddChild(node, otherchild); NOD_NodeAddChild(child1, child1child1);
NOD_NodeAddChild(node, justtotest); NOD_NodeAddChild(node, child2);
NOD_NodeAddChild(node, justtotest); NOD_NodeAddChild(node, child3);
NOD_NodeAddChild(node, justtotest); NOD_NodeAddChild(child3, child3child1);
NOD_NodeAddChild(node, node); NOD_NodeAddChild(child3, child3child2);
NOD_NodeAddChild(testchild, node);
NOD_NodeUnparent(justtotest); NOD_NodeUnparent(child2);
printf("\nThe name of the node is \"%s\"\n", node->name);
for(int i = 0; i < NOD_NodeChildCount(node); i++){
printf("Childnode: \"%s\"\n", node->childv[i]->name);
}
printf("\nThe name of the node is \"%s\"\n", node->name);
for(int i = 0; i < NOD_NodeChildCount(node); i++){
printf("Childnode: \"%s\"\n", node->childv[i]->name);
}
printf("\nThe amount of children is %d, the real size is %d\n",
NOD_NodeChildCount(node),
(int)sizeof(testchild)
);
printf("\n");
NOD_PrintNodeTree(node); NOD_PrintNodeTree(node);
NOD_DestroyNode(node); NOD_DestroyNode(node);
NOD_DestroyNode(testchild); NOD_DestroyNode(child1);
NOD_DestroyNode(child1child1);
NOD_DestroyNode(child2);
NOD_DestroyNode(child3);
NOD_DestroyNode(child3child1);
NOD_DestroyNode(child3child2);
return 0; return 0;
} }

View File

@ -97,8 +97,8 @@ void NOD_NodeUnparent(NOD_Node* node){
/* Free allocated memory if last child */ /* Free allocated memory if last child */
if(i + 1 == parent->childc){ if(i + 1 == parent->childc){
fprintf(stderr, //fprintf(stderr,
"NOD_Debug: Freeing last child of vector...\n"); //"NOD_Debug: Freeing last child of vector...\n");
parent->childv = realloc(parent->childv, sizeof(node) * parent->childc); parent->childv = realloc(parent->childv, sizeof(node) * parent->childc);
parent->childc--; parent->childc--;
} }
@ -109,13 +109,26 @@ void NOD_NodeUnparent(NOD_Node* node){
} }
} }
void nodetree_print_branch(NOD_Node* node, int level, int pos){
if(node != NULL){
if(pos+1 != node->parent->childc) printf("├── ");
else printf("└── ");
printf("\"%s\"\n", node->name);
for(int i = 0; i < node->childc; i++){
if(pos+1 != node->parent->childc) printf("");
else printf(" ");
nodetree_print_branch(node->childv[i], level+1, i);
}
}
}
void NOD_PrintNodeTree(NOD_Node* node){ void NOD_PrintNodeTree(NOD_Node* node){
if(node != NULL){ if(node != NULL){
printf("\"%s\"\n", node->name); printf("\"%s\"\n", node->name);
for(int i = 0; i < node->childc; i++){ for(int i = 0; i < node->childc; i++){
if(i+1 != node->childc) printf("├── "); nodetree_print_branch(node->childv[i], 0, i);
else printf("└── ");
printf("\"%s\"\n", node->childv[i]->name);
} }
} }
} }