aboutsummaryrefslogtreecommitdiff
path: root/src/1-2-binary-search-tree-visual.c
blob: 633dc7b5b2e5f20bf22c41f338ced53394aacc02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
 */