aboutsummaryrefslogtreecommitdiff
path: root/lib/algolib.c
blob: 43a1f2e90b8ec7dacd489b1f323f81fc9ce72f35 (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
#ifndef algolib
#define algolib

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <vectorlib.c>
#include <gobjects.c>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

#define flaot float

//Binary Search Tree

typedef struct BS_Node {
    int value;
    struct BS_Node *left;
    struct BS_Node *right;
    Point pos;
    TEXT *text;
} BS_Node;

void BSAddNode(BS_Node **self, BS_Node **node){
    if ((*node)->value < (*self)->value){
        if((*self)->left == NULL){
            (*self)->left = (*node);
        } else {
            BSAddNode(&(*self)->left,node);
        }
    } else if ((*node)->value > (*self)->value){
        if((*self)->right == NULL){
            (*self)->right = (*node);
        } else {
            BSAddNode(&(*self)->right,node);
        }
    } else {
        free(*node);
    }
}

void BSAddValue(BS_Node **root, int value){
    BS_Node *node = (BS_Node*)malloc(sizeof(BS_Node));
    node->value=value;
    node->left = NULL;
    node->right = NULL;

    if ((*root) == NULL){
        (*root) = node;
    } else {
        BSAddNode(root, &node);
    }
}

void BSPrintTree(BS_Node **node){
    if((*node)->left != NULL){
        BSPrintTree(&(*node)->left);
    }
    printf("%d\n",(*node)->value);
    if((*node)->right!= NULL){
        BSPrintTree(&(*node)->right);
    }
}

BS_Node *BSSearchTree(BS_Node **node, int val){
    if((*node)->value == val) {
        return *node;
    } else if (val < (*node)->value && (*node)->left != NULL){
        return BSSearchTree(&(*node)->left,val);
    } else if (val > (*node)->value && (*node)->right!= NULL){
        return BSSearchTree(&(*node)->right,val);
    }
    return NULL;
}

void BSFreeTree(BS_Node **node){
    if((*node)->left != NULL){
        BSFreeTree(&(*node)->left);
    }
    if((*node)->right != NULL){
        BSFreeTree(&(*node)->right);
    }
    free(*node);
}


//Breadth-First Search

typedef struct BFS_Node{
    char *value;
    struct BFS_Node **edges;
    bool searched;
    struct BFS_Node *parent;
} BFS_Node;

typedef struct BFS_Graph{
    BFS_Node **nodes;
    BFS_Node **graph; //make hashmap
} BFS_Graph;

BFS_Node *BFS_Constructor(char *value){
    BFS_Node *node=(BFS_Node*)malloc(sizeof(BFS_Node));
    node->value=value;
    node->edges=(BFS_Node**)malloc(sizeof(BFS_Node*)*2048);
    return node;
}

#endif