Skip to content

Instantly share code, notes, and snippets.

@geekskick
Created December 2, 2017 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekskick/592452e19d4442a5a25d362f93ed52b1 to your computer and use it in GitHub Desktop.
Save geekskick/592452e19d4442a5a25d362f93ed52b1 to your computer and use it in GitHub Desktop.
//
// main.c
// Playground
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEXT 100
typedef struct f_node node;
struct f_node{
char name[MAX_TEXT]; //dynamic
char text[MAX_TEXT]; // this would be dynamic for you
node *optn_a;
node *optn_b;
};
node* dfs_search(node*root, const char * const target);
node* create_node(const char * const file_name, node* const a, node* const b);
void dfs_cb_free(node *n);
void dfs_cb_print(node *n);
void dfs_traverse(node* root, void (*fn_cb)(node* n));
/* This searches though the tree for the filename original, then if it's found, creates a node for the left with the filename a, and one for the rigt with filename b */
node* insert_options(node* root, const char * const original, const char* const a, const char* const b){
node* result = dfs_search(root, original);
if(!result){ return NULL; }
// This means I can pass NULL to the function instead of having addLeft and addRight
if(a){
result->optn_a = create_node(a, NULL, NULL);
}
if(b){
result->optn_b = create_node(b, NULL, NULL);
}
return result;
}
node* create_node(const char * const file_name, node* const a, node* const b){
node* rc = calloc(sizeof(node), 1);
strncpy(rc->name, file_name, MAX_TEXT);
strncpy(rc->text, file_name, MAX_TEXT); // this is where you would read it's contents
rc->optn_a = a;
rc->optn_b = b;
return rc;
}
void dfs_cb_free(node *n){
if(n){
printf("[%p] - Freeing %s\n", n, n->name);
free(n);
}
}
void dfs_cb_print(node *n){
printf("\t[%p]\t%s\n", n, n->text);
}
node* dfs_search(node*root, const char * const target){
if(!root){ return NULL; }
if(0==strncmp(root->name, target, MAX_TEXT)){ return root; }
if(root->optn_a){
printf("[Search: %p]\t%s A([%p]-%s)\n", root, root->name, root->optn_a, root->optn_a->name);
node* result = dfs_search(root->optn_a, target);
if(result){ return result; }
}
if(root->optn_b){
printf("[Search: %p]\t%s B([%p]-%s)\n", root, root->name, root->optn_b, root->optn_b->name);
node* result = dfs_search(root->optn_b, target);
if(result){ return result; }
}
//end of story as both nodes are null
return NULL;
}
void dfs_traverse(node* root, void (*fn_cb)(node* n)){
if(!root){ return; }
if(root->optn_a){
printf("[Traverse: %p]\t%s A([%p]-%s)\n", root, root->name, root->optn_a, root->optn_a->name);
dfs_traverse(root->optn_a, fn_cb);
}
if(root->optn_b){
printf("[Traverse: %p]\t%s B([%p]-%s)\n", root, root->name, root->optn_b, root->optn_b->name);
dfs_traverse(root->optn_b, fn_cb);
}
fn_cb(root);
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
node* head = create_node("Start", NULL, NULL);
if(!insert_options(head, "Start", "Part1", "Part2")){ fprintf(stderr, "Error finding %s\n", "Start"); }
if(!insert_options(head, "Part1", "Part4", "Part5")){ fprintf(stderr, "Error finding %s\n", "Part1"); }
if(!insert_options(head, "Part2", NULL, NULL)){ fprintf(stderr, "Error finding %s\n", "Part2"); }
if(!insert_options(head, "Part4", NULL, NULL)){ fprintf(stderr, "Error finding %s\n", "Part4"); }
if(!insert_options(head, "Part5", "Part6", NULL)){ fprintf(stderr, "Error finding %s\n", "Part5"); }
if(!insert_options(head, "Part6", NULL, NULL)){ fprintf(stderr, "Error finding %s\n", "Part6"); }
dfs_traverse(head, dfs_cb_print);
dfs_traverse(head, dfs_cb_free);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment