From fe6ba5fa8bb43ed469297b8845fa3216c81bc5ea Mon Sep 17 00:00:00 2001 From: ergz Date: Sat, 15 Jul 2023 11:14:33 -0700 Subject: [PATCH] set up the ds needed to get the bfs implemented --- c/binary-tree.c | 26 ++++++++++-- c/breadth-first-search.c | 87 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 c/breadth-first-search.c diff --git a/c/binary-tree.c b/c/binary-tree.c index 142dece..b461d8c 100644 --- a/c/binary-tree.c +++ b/c/binary-tree.c @@ -167,6 +167,22 @@ void print_stack(Stack* stack) { } } +void print_stack_v2(Stack* stack) { + if (stack->len == 0) { + printf("ERROR: empty stack\n"); + } else { + printf("[ "); + int counter = 0; + Node* curr = stack->head; + while (counter < stack->len) { + printf("%d ", curr->value); + curr = curr->prev; + counter++; + } + printf(" ]\n"); + } +} + int main() { /* lets create the following tree 12 @@ -192,10 +208,12 @@ int main() { walk_tree_post_order(root_node, post_stack); walk_tree_in_order(root_node, in_order_stack); - printf("the len of the stack is %d\n", stack->len); - print_stack(stack); - print_stack(post_stack); - print_stack(in_order_stack); + printf("pre order: ", stack->len); + print_stack_v2(stack); + printf("in order: ", stack->len); + print_stack_v2(post_stack); + printf("post order: ", stack->len); + print_stack_v2(in_order_stack); free(stack); free(post_stack); free(in_order_stack); diff --git a/c/breadth-first-search.c b/c/breadth-first-search.c new file mode 100644 index 0000000..5aa7acd --- /dev/null +++ b/c/breadth-first-search.c @@ -0,0 +1,87 @@ +#include +#include + +/* Set up data structures to be able to support the breadth first search of a tree */ +typedef struct QNode { + int value; + struct QNode* next; + struct QNode* prev; +} QNode; + +typedef struct Q { + QNode* head; + QNode* tail; +} Q; + +typedef struct TreeNode { + int value; + struct TreeNode* left; + struct TreeNode* right; +} TreeNode; + +typedef struct Tree { + TreeNode* root; +} Tree; + +TreeNode* new_tree_node(int value) { + TreeNode* node = malloc(sizeof(TreeNode)); + node->left = NULL; + node->right = NULL; + node->value = value; + + return (node); +} + +QNode* new_qnode(int value) { + QNode* node = malloc(sizeof(QNode)); + node->next = NULL; + node->prev = NULL; + node->value = value; + + return (node); +} + +Tree* new_tree(TreeNode* root) { + Tree* tree = malloc(sizeof(Tree)); + tree->root = root; + return (tree); +} + +void add_child_left(TreeNode* parent, TreeNode* node) { + if (parent->left != NULL) { + printf("ERROR: left child is non-empty\n"); + exit(1); + } else { + parent->left = node; + } +} + +void add_child_right(TreeNode* parent, TreeNode* node) { + if (parent->right != NULL) { + printf("ERROR: right child is non-empty\n"); + exit(1); + } else { + parent->right = node; + } +} + +/* + 10 + 5 7 + 12 8 88 14 + + */ + +int main() { + TreeNode* root = new_tree_node(10); + Tree* tree = new_tree(root); + + add_child_left(root, new_tree_node(5)); + add_child_right(root, new_tree_node(7)); + + add_child_left(root->left, new_tree_node(12)); + add_child_right(root->left, new_tree_node(8)); + + add_child_left(root->right, new_tree_node(88)); + add_child_right(root->right, new_tree_node(14)); +} \ No newline at end of file