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
// This is from office hours on 4/27/2022.  It is provided here for fairness.

// Okay to copy/adapt for ECE 26400 in Spring 2022, if you understand completely, at your own risk.

#include <stdlib.h>
#include <stdbool.h>

typedef unsigned char uchar;

static bool _is_cluster_with_frequency(TreeNode* node, size_t frequency) {
    return node -> left      != NULL
        && node -> right     != NULL
        && node -> frequency == frequency;
}
#define mu_check_cluster_node(frequency, node) _is_cluster_with_frequency(node, frequency) 

static bool _is_character_node_with_frequency(TreeNode* node, uchar ch, size_t frequency) {
    return node -> left      == NULL
        && node -> right     == NULL
        && node -> character == ch
        && node -> frequency == frequency;
}
#define mu_check_char_node(ch, frequency, node) _is_character_node_with_frequency(node, ch, frequency) 

static void _print_tree(TreeNode* root) {
    if(root != NULL) {
        assert((root -> left == NULL) == (root -> right == NULL));
        if(root -> left != NULL) {
            printf("[* x %zd] ", root -> frequency);
        }
        else if(root -> character >= ' ' && root -> character <= '~') {
            printf("['%c' x %zd] ", root -> character, root -> frequency);
        }
        else {
            printf("['%02x' x %zd] ", root -> character, root -> frequency);
        }
        fflush(stdout);
        _print_tree(root -> left);
        _print_tree(root -> right);
    }
}

static int _test______() {
    mu_start();
    //────────────────────
    mu_check_cluster_node(  24, root);
    mu_check_cluster_node(  10, root -> left);
    mu_check_char_node('f',  5, root -> left -> left);
    mu_check_cluster_node(   5, root -> left -> right);
    mu_check_cluster_node(   2, root -> left -> right -> left);
    mu_check_char_node('h',  1, root -> left -> right -> left -> left);
    mu_check_char_node('l',  1, root -> left -> right -> left -> right);
    mu_check_char_node(' ',  3, root -> left -> right -> right);
    //────────────────────
    mu_end();
}

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