diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/1-1-binary-search-tree.c | 33 | ||||
| -rw-r--r-- | src/1-2-binary-search-tree-visual.c | 162 |
2 files changed, 195 insertions, 0 deletions
diff --git a/src/1-1-binary-search-tree.c b/src/1-1-binary-search-tree.c new file mode 100644 index 0000000..cb5efc1 --- /dev/null +++ b/src/1-1-binary-search-tree.c @@ -0,0 +1,33 @@ +#include <stdbool.h> +#include <stdio.h> +#include <algolib.c> +#include <vectorlib.c> +#include <time.h> + +typedef struct TREE{ + BS_Node *root; +} TREE; + +int main(int argc, char **argv){ + srand(time(NULL)); + TREE tree = {NULL}; + + for(int i=0; i <40; i++){ + BSAddValue(&tree.root,(int)RandomFloat(0,100)); + } + + BSPrintTree(&tree.root); + BS_Node *found_node = BSSearchTree(&tree.root,53); + if(found_node != NULL) { + printf("53 found %d\n",found_node->value); + } else { + printf("53 not found\n"); + } + BSFreeTree(&tree.root); + return 0; +} + +/* Challanges + * + * balance the tree + */
\ No newline at end of file diff --git a/src/1-2-binary-search-tree-visual.c b/src/1-2-binary-search-tree-visual.c new file mode 100644 index 0000000..633dc7b --- /dev/null +++ b/src/1-2-binary-search-tree-visual.c @@ -0,0 +1,162 @@ +#include <stdbool.h> +#include <stdio.h> +#include <vectorlib.c> +#include <algolib.c> + +#include <SDL2/SDL_render.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_video.h> +#include <SDL2/SDL2_gfxPrimitives.h> +#include <SDL2/SDL_ttf.h> + +#define PROJECT_NAME "algorithms-binary search tree visual" +#define obj_n 1 + +typedef struct OBJECT_INER { + BS_Node *root; + BS_Node *found_node; +} OBJECT_INER; + +typedef struct OBJECT { + OBJECT_INER *objs[obj_n]; +} OBJECT; + +#include <init.h> +#include <gobjects.c> + +int node_rows[200]; + +void count_rows(BS_Node **node){ + static int depth=1; + depth++; + if((*node)->left != NULL){ + node_rows[depth]++; + count_rows(&(*node)->left); + } + if((*node)->right != NULL){ + node_rows[depth]++; + count_rows(&(*node)->right); + } + depth--; +} + +int node_row_index[200]; + +void set_pos(BS_Node **node, STUFFS *stuff){ + static int depth=1; + depth++; + if((*node)->left != NULL){ + node_row_index[depth]++; + (*node)->left->pos.x = (((float)stuff->width / 2) + ((float)node_row_index[depth] * 30)) - (15 + (float)node_rows[depth] * 15); + (*node)->left->pos.y = (*node)->pos.y + 30; + set_pos(&(*node)->left, stuff); + } + if((*node)->right != NULL){ + node_row_index[depth]++; + (*node)->right->pos.x = (((float)stuff->width / 2) + ((float)node_row_index[depth] * 30)) - (15 + (float)node_rows[depth] * 15); + (*node)->right->pos.y = (*node)->pos.y + 30; + set_pos(&(*node)->right, stuff); + } + (*node)->text = NULL; + depth--; +} + +void add_text(BS_Node **node, STUFFS *stuff){ + GS stuff2={stuff->width,stuff->height,stuff->renderer,stuff->font}; + char *text=(char*)malloc(sizeof(char)*20); + sprintf(text,"%d",(*node)->value); + (*node)->text=text_constructor(&stuff2,text); + free(text); + (*node)->text->textRect.x=(*node)->pos.x-5; + (*node)->text->textRect.y=(*node)->pos.y-5; + if((*node)->left != NULL){ + add_text(&(*node)->left, stuff); + } + if((*node)->right != NULL){ + add_text(&(*node)->right, stuff); + } +} + +void init_stuffs(int w, int h, OBJECT_INER *objs,STUFFS *stuff){ + OBJECT_INER *tree = &objs[0]; + for(int i=0; i <100; i++){ + BSAddValue(&tree->root,(int)RandomFloat(0,100)); + } + tree->root->pos.x = (float)stuff->width / 2; + tree->root->pos.y = 16; + tree->root->text = NULL; + + BSPrintTree(&tree->root); + BS_Node *found_node = BSSearchTree(&tree->root,53); + tree->found_node=found_node; + if(found_node != NULL) { + printf("53 found %d\n",found_node->value); + } else { + printf("53 not found\n"); + } + + count_rows(&tree->root); + set_pos(&tree->root, stuff); + add_text(&tree->root,stuff); +} + + +void draw_node(BS_Node **node, STUFFS *stuff){ + if((*node)->left != NULL){ + draw_node(&(*node)->left, stuff); + SDL_RenderDrawLine(stuff->renderer,(*node)->pos.x,(*node)->pos.y,(*node)->left->pos.x,(*node)->left->pos.y); + } + SDL_RenderCopy(stuff->renderer,(*node)->text->texture,NULL, &(*node)->text->textRect); + circleRGBA(stuff->renderer,(*node)->pos.x,(*node)->pos.y,10,255,0,255,255); + if(stuff->obj.objs[0]->found_node != NULL && stuff->obj.objs[0]->found_node == *node){ + circleRGBA(stuff->renderer,(*node)->pos.x,(*node)->pos.y,10,0,255,255,255); + SDL_SetRenderDrawColor(stuff->renderer,255,0,255,255); + } + if((*node)->right != NULL){ + draw_node(&(*node)->right, stuff); + SDL_RenderDrawLine(stuff->renderer,(*node)->pos.x,(*node)->pos.y,(*node)->right->pos.x,(*node)->right->pos.y); + } +} + + +void draw(SDL_Window* window, STUFFS *stuff){ + SDL_SetRenderDrawColor(stuff->renderer,51,51,51,255); + //SDL_RenderClear(renderer); + SDL_RenderFillRect(stuff->renderer, &(SDL_Rect){0,0,stuff->width,stuff->height}); + //for (int i=0; i<obj_n; i++){ + // show(renderer,stuff,stuff->obj.objs[i]); + //} + + SDL_SetRenderDrawColor(stuff->renderer,255,0,255,255); + draw_node(&stuff->obj.objs[0]->root,stuff); + SDL_RenderPresent(stuff->renderer); +} + +void BSFreeText(BS_Node **node){ + if((*node)->left != NULL){ + BSFreeText(&(*node)->left); + } + if((*node)->right != NULL){ + BSFreeText(&(*node)->right); + } + + SDL_DestroyTexture((*node)->text->texture); + free((*node)->text); +} + +void on_end(STUFFS *stuff){ + OBJECT_INER *tree = stuff->obj.objs[0]; + BSFreeText(&tree->root); + BSFreeTree(&tree->root); +} + +#include <init.c> + +/* Challanges + * + * make a prettier visualization + * + * - animation of a search + * + * - set node position closer to its parent node + */ |
