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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

typedef struct _BSTNode {
    int value;
    struct _BSTNode* left;
    struct _BSTNode* right;
} BSTNode;

int get_height(BSTNode const* root) {
    int height = -1;  // height of empty tree (i.e., NULL)
    if(root == NULL) {
        return -1;
    }
    else {
        int height_of_left  = get_height(root -> left);
        int height_of_right = get_height(root -> right);
        return 1 + (height_of_left > height_of_right ? height_of_left : height_of_right);
    }
}

int get_balance_score(BSTNode const* root) {
    if(root == NULL) {
        return 0;
    }
    else {
        return get_height(root -> left) - get_height(root -> right);
    }
}

void insert(int value, BSTNode** a_root) {
    if(*a_root == NULL) {
        *a_root = malloc(sizeof **a_root);
        **a_root = (BSTNode) { .value = value, .left = NULL, .right = NULL };
    }
    else if(value < (*a_root) -> value) {
        insert(value, &((*a_root) -> left));
        balance_after_insert_left(value, a_root);
    }
    else if(value > (*a_root) -> value) {
        insert(value, &((*a_root) -> right));
    }
}

void rotate_clockwise(BSTNode** a_root) { // rotate with left child
    BSTNode* old_root = *a_root;           // aka "y"
    BSTNode* new_root = (*a_root) -> left; // aka "x"
    old_root -> left = new_root -> right;  // "y" -> left  := "b"
    new_root -> right = old_root;          // "x" -> right := "y"
    *a_root = new_root;                    // root of the tree is now "x"
}

void balance_after_left(int value, BSTNode** a_root) {
    if(get_balance_score(*a_root) == 2) {
        if(value < (*a_root) -> left -> value) { // insert into root -> left -> left
            rotate_clockwise(a_root);
        }
        else if(value > (*a_root) -> left -> value) { // insert into root -> left -> right
            rotate_counterclockwise(&((*a_root) -> left));
            rotate_clockwise(a_root);
        }
    }
}

// To submit:  368submit le07 *.txt

void print_bst_nodes(BSTNode const* root) {
    if(root != NULL) {
        print_bst_nodes(root -> left);
        printf("[%d] ", root -> value);
        print_bst_nodes(root -> right);
    }
}

void print_bst(BSTNode const* root, char const* label) {
    printf("%s%s", label, "");
    print_bst_nodes(root);
    printf("\n");
}

int main(int argc, char* argv[]) {
    BSTNode* root = NULL;
    insert(7, &root);
    insert(6, &root);
    insert(5, &root);
    insert(4, &root);
    insert(3, &root);
    insert(2, &root);
    insert(1, &root);
    print_bst(root, "");
    printf("get_height(root) == %d\n", get_height(root));
    printf("get_balance_score(root) == %d\n", get_balance_score(root));
    printf("get_balance_score(root -> left) == %d\n", get_balance_score(root -> left));
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2023 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.