back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/rb_tree_main.c
blob: 66ba68d99ad6a92ad2ca02a1a74a8168f31a28ad (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
#include "rb_tree.h"
#include <stdio.h>
#include <stdlib.h>

#define PRINT_MENU \
    printf("\n1 - add numbers to tree\n"); \
    printf("2 - remove node if it exists\n"); \
    printf("3 - print tree\n"); \
    printf("4 - clear tree\n"); \
    printf("5 - quit\n");

#define CLEAR_BUFFER \
    while ( (res = getchar()) != '\n' && res != EOF ) { }

int main()
{
    enum { true = 1 };
    enum state { add = 1, remove, print, clear, quit };
    enum state answer;
    rb_tree *new_tree = NULL;
    int res, num;
    char ch;
    while(true) {
        PRINT_MENU
        if((res = getchar())== EOF)
            break;
        ch = (char)res;
        res = strtol(&ch, NULL, 10);
        if(!res) {
            CLEAR_BUFFER
            printf("\e[1;1H\e[2J");     /* clear screen */
            continue;
        }
        answer = (enum state) res;
        CLEAR_BUFFER
        printf("\e[1;1H\e[2J");
        switch(answer) {
            case add:
                if(!new_tree) {
                    new_tree = malloc(sizeof(rb_tree));
                    rb_tree_init(new_tree);
                }
                printf("enter some numbers (ctrl-d - quit)\n");
                while((res = scanf("%d", &num)) != EOF) {
                    if(!res) {
                        fprintf(stderr, "invalid input\n");
                        return 1;
                    }
                    node *new_node = rb_tree_create_node(new_tree, num);
                    if (new_node)
                        rb_tree_insert_node(new_tree, new_node);
                }
                clearerr(stdin);
                break;
            case remove:
                if(!new_tree) {
                    printf("tree wasn't created\n");
                    break;
                }
                printf("enter a number to delete\n");
                res = scanf("%d", &num);
                if(!res) {
                    fprintf(stderr, "invalid input\n");
                    return 1;
                }
                node *del_node = rb_tree_search(new_tree, new_tree->root, num);
                if(del_node != new_tree->nil) {
                    rb_tree_delete_node(new_tree, del_node);
                    printf("node with this number has been removed!\n");
                }
                else
                    printf("the number wasn't finded\n");
                CLEAR_BUFFER
                break;
            case print:
                /* clearing terminal history */
                printf("\033c");
                if(new_tree)
                    rb_tree_print(new_tree, new_tree->root);
                break;
            case clear:
                if(new_tree) {
                    rb_tree_clear(new_tree, new_tree->root);
                    free(new_tree);
                    new_tree = NULL;
                    printf("tree has been cleared\n");
                }
                break;
            case quit:
                if(new_tree)
                    free(new_tree);
                return 0;
        }
    }
    return 0;
}