diff --git a/src/main.c b/src/main.c index 44ab31c..f81fe96 100644 --- a/src/main.c +++ b/src/main.c @@ -3,40 +3,31 @@ #include "node.h" int main(int argc, char** argv){ - NOD_Node* node = NOD_CreateNode("test node"); - NOD_Node* testchild = NOD_CreateNode("I'm just a child"); - NOD_Node* otherchild = NOD_CreateNode("Another child"); - NOD_Node* justtotest = NOD_CreateNode("This one is a test"); + NOD_Node* node = NOD_CreateNode("Main Node"); + NOD_Node* child1 = NOD_CreateNode("First child"); + NOD_Node* child1child1 = NOD_CreateNode("Child of first child"); + 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, otherchild); - NOD_NodeAddChild(node, justtotest); - NOD_NodeAddChild(node, justtotest); - NOD_NodeAddChild(node, justtotest); - NOD_NodeAddChild(node, node); - NOD_NodeAddChild(testchild, node); + NOD_NodeAddChild(node, child1); + NOD_NodeAddChild(child1, child1child1); + NOD_NodeAddChild(node, child2); + NOD_NodeAddChild(node, child3); + NOD_NodeAddChild(child3, child3child1); + NOD_NodeAddChild(child3, child3child2); - 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_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; } diff --git a/src/node.c b/src/node.c index 39f4909..838964a 100644 --- a/src/node.c +++ b/src/node.c @@ -97,8 +97,8 @@ void NOD_NodeUnparent(NOD_Node* node){ /* Free allocated memory if last child */ if(i + 1 == parent->childc){ - fprintf(stderr, - "NOD_Debug: Freeing last child of vector...\n"); + //fprintf(stderr, + //"NOD_Debug: Freeing last child of vector...\n"); parent->childv = realloc(parent->childv, sizeof(node) * 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){ if(node != NULL){ printf("\"%s\"\n", node->name); + for(int i = 0; i < node->childc; i++){ - if(i+1 != node->childc) printf("├── "); - else printf("└── "); - printf("\"%s\"\n", node->childv[i]->name); + nodetree_print_branch(node->childv[i], 0, i); } } }