-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
57 lines (50 loc) · 1.15 KB
/
node.c
File metadata and controls
57 lines (50 loc) · 1.15 KB
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
#include "node.h"
#include <stdint.h>
#include <stdlib.h>
// Description:
// Initializes a node with a specified symbol and frequency.
//
// Parameters:
// uint8_t symbol - The symbol of the node.
// uint64_t frequency - The frequency of the node.
//
// Returns:
// Node * - A pointer to the newly initialized node.
Node *node_create( uint8_t symbol, uint64_t frequency ) {
Node *n = ( Node * ) malloc( sizeof( Node ) );
if ( n ) {
n->left = n->right = NULL;
n->symbol = symbol;
n->frequency = frequency;
}
return n;
}
// Description:
// Frees the memory given to a node.
//
// Parameters:
// Node **s - A pointer to a pointer to the node to free the memory of.
//
// Returns:
// Nothing.
void node_delete( Node **n ) {
if ( *n ) {
free( *n );
*n = NULL;
}
}
// Description:
// Joins two child nodes and returns a pointer to a created parent node.
//
// Parameters:
// Node *left - The left child node.
// Node *right - The right child node.
//
// Returns:
// Node * - The created parent node.
Node *node_join( Node *left, Node *right ) {
Node *n = node_create( '$', left->frequency + right->frequency );
n->left = left;
n->right = right;
return n;
}