-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.c
45 lines (39 loc) · 1.03 KB
/
node.c
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
#include "node.h"
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Node *node_create(uint8_t symbol, uint64_t frequency) {
Node *n = (Node *) malloc(sizeof(Node));
assert(n);
n->left = NULL;
n->right = NULL;
n->symbol = symbol;
n->frequency = frequency;
return n;
}
void node_delete(Node **n) {
if (*n) {
// Free Memory
free(*n);
*n = NULL;
}
return;
}
Node *node_join(Node *left, Node *right) {
Node *n = node_create('$', left->frequency + right->frequency);
n->left = left;
n->right = right;
return n;
}
void node_print(Node *n) {
printf("Parent Node Frequency: %" PRId64 " Parent node symbol: %d\n", n->frequency, n->symbol);
if (n->left && n->right) {
printf("Left child frequency '%c': %" PRId64 " Right child frequency '%c': %" PRId64 "\n",
n->left->symbol, n->left->frequency, n->right->symbol, n->right->frequency);
}
return;
}