finish tree view

This commit is contained in:
Dendy 2021-01-24 17:11:49 +01:00
parent 1408cca6b7
commit fd25215671
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
3 changed files with 123 additions and 53 deletions

View File

@ -2,32 +2,68 @@
#include "node.h" #include "node.h"
int main(int argc, char** argv){ int main(int argc, char* *argv){
NOD_Node* node = NOD_CreateNode("Main Node"); NOD_Node *distros = NOD_CreateNode("Distros");
NOD_Node* child1 = NOD_CreateNode("First child"); NOD_Node *linoox = NOD_CreateNode("GNU/Linux");
NOD_Node* child1child1 = NOD_CreateNode("Child of first child"); NOD_Node *debian = NOD_CreateNode("Debian");
NOD_Node* child2 = NOD_CreateNode("Second child"); NOD_Node *ubuntu = NOD_CreateNode("Ubuntu");
NOD_Node* child3 = NOD_CreateNode("Third child"); NOD_Node *mint = NOD_CreateNode("Linux Mint");
NOD_Node* child3child1 = NOD_CreateNode("First child of third child"); NOD_Node *devuan = NOD_CreateNode("Devuan");
NOD_Node* child3child2 = NOD_CreateNode("Second child of third child"); NOD_Node *kali = NOD_CreateNode("Kali Linux");
NOD_Node *voidlinux = NOD_CreateNode("Void Linux");
NOD_Node *arch = NOD_CreateNode("Arch");
NOD_Node *manjaro = NOD_CreateNode("Manjaro");
NOD_Node *parabola = NOD_CreateNode("Parabola");
NOD_Node *endeavouros = NOD_CreateNode("EndeavourOS");
NOD_Node *bsd = NOD_CreateNode("BSD");
NOD_Node *freebsd = NOD_CreateNode("FreeBSD");
NOD_Node *openbsd = NOD_CreateNode("OpenBSD");
NOD_Node *debianbsd = NOD_CreateNode("Debian GNU/kFreeBSD");
NOD_NodeAddChild(node, child1); NOD_NodeAddChild(distros, linoox);
NOD_NodeAddChild(child1, child1child1); NOD_NodeAddChild(distros, bsd);
NOD_NodeAddChild(node, child2);
NOD_NodeAddChild(node, child3);
NOD_NodeAddChild(child3, child3child1);
NOD_NodeAddChild(child3, child3child2);
NOD_NodeUnparent(child2); NOD_NodeAddChild(linoox, debian);
NOD_NodeAddChild(linoox, voidlinux);
NOD_NodeAddChild(linoox, arch);
NOD_PrintNodeTree(node); NOD_NodeAddChild(debian, ubuntu);
NOD_NodeAddChild(debian, devuan);
NOD_NodeAddChild(debian, kali);
NOD_DestroyNode(node); NOD_NodeAddChild(ubuntu, mint);
NOD_DestroyNode(child1);
NOD_DestroyNode(child1child1); NOD_NodeAddChild(arch, manjaro);
NOD_DestroyNode(child2); NOD_NodeAddChild(arch, parabola);
NOD_DestroyNode(child3); NOD_NodeAddChild(arch, endeavouros);
NOD_DestroyNode(child3child1);
NOD_DestroyNode(child3child2); NOD_NodeAddChild(bsd, freebsd);
NOD_NodeAddChild(bsd, openbsd);
NOD_NodeAddChild(bsd, debianbsd);
//NOD_NodeUnparent(child2);
NOD_PrintNodeTree(distros);
NOD_PrintNodeTree(arch);
NOD_PrintNodeTree(linoox);
NOD_PrintNodeTree(debian);
NOD_PrintNodeTree(freebsd);
NOD_DestroyNode(distros);
NOD_DestroyNode(linoox);
NOD_DestroyNode(debian);
NOD_DestroyNode(ubuntu);
NOD_DestroyNode(mint);
NOD_DestroyNode(devuan);
NOD_DestroyNode(kali);
NOD_DestroyNode(voidlinux);
NOD_DestroyNode(arch);
NOD_DestroyNode(manjaro);
NOD_DestroyNode(parabola);
NOD_DestroyNode(endeavouros);
NOD_DestroyNode(bsd);
NOD_DestroyNode(freebsd);
NOD_DestroyNode(openbsd);
NOD_DestroyNode(debianbsd);
return 0; return 0;
} }

View File

@ -2,9 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
NOD_Node* NOD_CreateNode(char* name){ NOD_Node *NOD_CreateNode(char *name){
NOD_Node* node = malloc(sizeof(NOD_Node)); NOD_Node *node = malloc(sizeof(NOD_Node));
if(node != NULL){ if(node != NULL){
node->name = name; node->name = name;
@ -17,20 +18,20 @@ NOD_Node* NOD_CreateNode(char* name){
return node; return node;
} }
void NOD_DestroyNode(NOD_Node* node){ void NOD_DestroyNode(NOD_Node *node){
if(node != NULL){ if(node != NULL){
free(node); free(node);
} }
} }
void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child){ void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child){
if(parent != NULL && child != NULL){ if(parent != NULL && child != NULL){
/** Prevent recursion */ /** Prevent recursion */
if(parent == child){ if(parent == child){
fprintf(stderr, "NOD_Warning: A node cannot be added to itself.\n"); fprintf(stderr, "NOD_Warning: A node cannot be added to itself.\n");
return; return;
} }
NOD_Node* tmp_parent = parent->parent; NOD_Node *tmp_parent = parent->parent;
while(tmp_parent != NULL){ while(tmp_parent != NULL){
if(tmp_parent == child){ if(tmp_parent == child){
fprintf(stderr, "NOD_Warning: Can't add a parent of a node as its child.\n"); fprintf(stderr, "NOD_Warning: Can't add a parent of a node as its child.\n");
@ -69,7 +70,7 @@ void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child){
has already been tested */ has already been tested */
else if(parent->childc > 0){ else if(parent->childc > 0){
parent->childv = realloc(parent->childv, parent->childv = realloc(parent->childv,
sizeof(child) * (parent->childc+1)); sizeof(child) *(parent->childc+1));
if(parent->childv != NULL){ if(parent->childv != NULL){
parent->childv[parent->childc] = child; parent->childv[parent->childc] = child;
@ -87,9 +88,9 @@ void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child){
} }
} }
void NOD_NodeUnparent(NOD_Node* node){ void NOD_NodeUnparent(NOD_Node *node){
if(node != NULL && node->parent != NULL){ if(node != NULL && node->parent != NULL){
NOD_Node* parent = node->parent; NOD_Node *parent = node->parent;
for(int i = 0; i < parent->childc; i ++){ for(int i = 0; i < parent->childc; i ++){
if(parent->childv[i] == node){ if(parent->childv[i] == node){
@ -99,7 +100,7 @@ void NOD_NodeUnparent(NOD_Node* node){
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,32 +110,65 @@ void NOD_NodeUnparent(NOD_Node* node){
} }
} }
void nodetree_print_branch(NOD_Node* node, int level, int pos){ int get_child_pos(NOD_Node *node){
int count = 0;
if(node != NULL && node->parent != NULL){
for(int i = 0; i < node->parent->childc; i++){
if(node->parent->childv[i] == node) return count;
count++;
}
return 0;
}
return -1;
}
void nodetree_print_branch(NOD_Node *node, char* level){
if(node != NULL){ if(node != NULL){
if(pos+1 != node->parent->childc) printf("├── "); for(int i = 0; level[i] != '\0'; i++){
else printf("└── "); if(level[i] == 'y') printf("");
printf("\"%s\"\n", node->name); else printf(" ");
}
if(node->parent != NULL){
if(get_child_pos(node)+1 != node->parent->childc){
printf("├─ ");
strcat(level, "y");
}
else{
printf("└─ ");
strcat(level, "n");
}
}
printf("%s\n", node->name);
/** Recurse */
for(int i = 0; i < node->childc; i++){ for(int i = 0; i < node->childc; i++){
if(pos+1 != node->parent->childc) printf(""); nodetree_print_branch(node->childv[i], level);
else printf(" "); }
nodetree_print_branch(node->childv[i], level+1, i);
if(node->parent != NULL){
level = (char *)realloc(level, strlen(level) * sizeof(char));
level[strlen(level)-1] = '\0';
} }
} }
} }
void NOD_PrintNodeTree(NOD_Node* node){ void NOD_PrintNodeTree(NOD_Node *node){
if(node != NULL){ if(node != NULL){
printf("\"%s\"\n", node->name); char* level = strdup("");
printf("%s\n", node->name);
for(int i = 0; i < node->childc; i++){ for(int i = 0; i < node->childc; i++){
nodetree_print_branch(node->childv[i], 0, i); nodetree_print_branch(node->childv[i], level);
} }
free(level);
} }
} }
int NOD_NodeChildCount(NOD_Node* node){ int NOD_NodeChildCount(NOD_Node *node){
unsigned int count = 0; int count = 0;
for(int i = 0; i < node->childc; i++){ for(int i = 0; i < node->childc; i++){
if(node->childv[i] != NULL) count++; if(node->childv[i] != NULL) count++;
} }

View File

@ -2,24 +2,24 @@
#define __NODE_H_ #define __NODE_H_
typedef struct node{ typedef struct node{
char* name; char *name;
/** childc keeps track of allocated space, /** childc keeps track of allocated space,
not amount of children. For that use not amount of children. For that use
NOD_NodeChildCount */ NOD_NodeChildCount */
int childc; int childc;
struct node** childv; struct node* *childv;
struct node* parent; struct node *parent;
} NOD_Node; } NOD_Node;
NOD_Node* NOD_CreateNode(char* name); NOD_Node *NOD_CreateNode(char *name);
void NOD_DestroyNode(NOD_Node* node); void NOD_DestroyNode(NOD_Node *node);
void NOD_NodeAddChild(NOD_Node* parent, NOD_Node* child); void NOD_NodeAddChild(NOD_Node *parent, NOD_Node *child);
void NOD_NodeUnparent(NOD_Node* child); void NOD_NodeUnparent(NOD_Node *child);
void NOD_PrintNodeTree(NOD_Node* node); void NOD_PrintNodeTree(NOD_Node *node);
int NOD_NodeChildCount(NOD_Node* node); int NOD_NodeChildCount(NOD_Node *node);
#endif // __NODE_H_ #endif // __NODE_H_