From 8ab4d740422d2d45083bbad1055bad3652cfa060 Mon Sep 17 00:00:00 2001 From: ergz Date: Thu, 13 Jul 2023 23:16:00 -0700 Subject: [PATCH] trying to implement binary tree walk --- c/binary-search.c | 55 ------------------ c/binary-tree.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 55 deletions(-) delete mode 100644 c/binary-search.c create mode 100644 c/binary-tree.c diff --git a/c/binary-search.c b/c/binary-search.c deleted file mode 100644 index 1c94b5b..0000000 --- a/c/binary-search.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include - -typedef struct ArrayList { - int capacity; - int index; - int data[]; -} ArrayList; - -ArrayList* new_arraylist(int cap) { - ArrayList* arr = malloc(sizeof(ArrayList) + cap * sizeof(int)); - arr->capacity = cap; - arr->index = 0; - for (int i = 0; i < cap; i++) { - arr->data[i] = 0; - } - - return arr; -} - -void push_to_array(ArrayList* s, int v) { - if (s->index == s->capacity) { - printf("you attempted to insert %d, but array is at capacity cannot add mode values\n", v); - } else { - s->data[s->index] = v; - s->index++; - } -} - -void pop_from_array(ArrayList* s) { - if (s->index == 0) { - printf("there is nothing to remove!\n"); - } else { - s->index--; - } -} - -typedef struct BinaryNode { - int value; - struct BinaryNode* left; - struct BinaryNode* right; -} BinaryNode; - -BinaryNode* new_binary_node(int value) { - BinaryNode* node = malloc(sizeof(BinaryNode)); - node->value = value; - - return (node); -} - - - -int main() { - return (0); -} \ No newline at end of file diff --git a/c/binary-tree.c b/c/binary-tree.c new file mode 100644 index 0000000..e4d1648 --- /dev/null +++ b/c/binary-tree.c @@ -0,0 +1,145 @@ +/* +implementation of binary tree, in the course he just jumps into +search algorithms but uses built in javascript data structures +to jump a bunch of steps, here I write everything from scratch + */ + +#include +#include + +typedef struct Node { + int value; + struct Node* prev; +} Node; + +typedef struct Stack { + int len; + Node* head; +} Stack; + +Stack* new_stack() { + Stack* s = malloc(sizeof(Stack)); + s->head = NULL; + s->head = 0; + return (s); +} + +Node* new_node(int value) { + Node* n = malloc(sizeof(Node)); + n->prev = NULL; + n->value = value; + + return (n); +} + +void push(Stack* stack, Node* node) { + if (stack->len == 0) { + stack->head = node; + stack->len++; + } else { + node->prev = stack->head; + stack->head = node; + stack->len++; + } +} + +void pop(Stack* stack) { + if (stack->len > 0) { + Node* node = stack->head; + stack->head = node->prev; + free(node); + } +} + +typedef struct IntBinaryNode { + int value; + struct IntBinaryNode* left; + struct IntBinaryNode* right; +} IntBinaryNode; + +IntBinaryNode* new_int_binary_node(int value) { + IntBinaryNode* n = malloc(sizeof(IntBinaryNode)); + n->value = value; + + return (n); +} + +typedef struct BinaryTree { + IntBinaryNode* root; +} BinaryTree; + +BinaryTree* new_binary_tree(IntBinaryNode* n) { + BinaryTree* b = malloc(sizeof(BinaryTree)); + b->root = n; + + return (b); +} + +void add_child_node(IntBinaryNode* parent, IntBinaryNode* child, char position) { + if (position != 'l' && position != 'r') { + printf("ERROR: position must be either (l)eft or (r)ight\n"); + exit(1); + } + + if (position == 'l') { + if (parent->left != NULL) { + printf("ERROR: left position of parent is already occupied"); + exit(1); + } else { + parent->left = child; + } + } else if (position == 'r') { + if (parent->right != NULL) { + printf("ERROR: right position of parent is already occupied"); + exit(1); + } else { + parent->right = child; + } + } +} + +// prints the bottom row of a tree +void print_binary_tree(IntBinaryNode* node) { + if (node->left == NULL && node->right == NULL) { + printf("<%d>\n", node->value); + } else { + print_binary_tree(node->left); + print_binary_tree(node->right); + } +} + +void walk_tree(IntBinaryNode* current_node, Stack* stack) { + if (current_node->left != NULL && current_node->right != NULL) { + printf("looking at value: %d\n", current_node->value); + push(stack, new_node(current_node->value)); + + walk_tree(current_node->left, stack); + walk_tree(current_node->right, stack); + } +} + +int main() { + /* lets create the following tree + 12 + 4 6 + 10 11 44 77 + + */ + IntBinaryNode* root_node = new_int_binary_node(12); + BinaryTree* tree = new_binary_tree(root_node); + + add_child_node(root_node, new_int_binary_node(4), 'l'); + add_child_node(root_node, new_int_binary_node(6), 'r'); + + add_child_node(root_node->left, new_int_binary_node(10), 'l'); + add_child_node(root_node->left, new_int_binary_node(11), 'r'); + add_child_node(root_node->right, new_int_binary_node(44), 'l'); + add_child_node(root_node->right, new_int_binary_node(77), 'r'); + + Stack* stack = new_stack(); + walk_tree(root_node, stack); + + printf("the len of the stack is %d\n", stack->len); + + return (0); +}