- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
Latest commit
100 lines (88 loc) · 1.79 KB
/
Copy pathnode.c
File metadata and controls
100 lines (88 loc) · 1.79 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
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
99
100
#include"node.h"
#include"node_extnd.h"
#include<stdio.h>
#include<stdlib.h>
//
// Creates a Node
//
// symbol: symbol of node
// frequency: frequency of node
//
Node*node_create(uint8_tsymbol, uint64_tfrequency) {
Node*n= (Node*) malloc(sizeof(Node));
if (n) {
n->symbol=symbol;
n->frequency=frequency;
n->right=NULL;
n->left=NULL;
}
returnn;
}
// Helper Functions from node_extnd.h //
//
// Returns Node frequency
//
// n: an adress to a Node
//
uint64_tnode_frequency(Node*n) {
returnn ? n->frequency : 0;
}
//
// Returns Node symbol
//
// n: an address to a node
//
uint8_tnode_symbol(Node*n) {
returnn ? n->symbol : 0;
}
//
// Returns left node
//
// n: an address to a node
//
Node*node_left(Node*n) {
returnn&&n->left ? n->left : NULL;
}
//
// Returns right node
//
// n: an address to a node
//
Node*node_right(Node*n) {
returnn&&n->right ? n->right : NULL;
}
///////////////////////////////////////
//
// Delete a Node
//
// n: an adress to an address of a Node
//
voidnode_delete(Node**n) {
free(*n);
*n=NULL;
}
//
// Join two nodes together
//
// left: an address of a Node to be stored to the left
// right: an address of a Node to be stored toe the right
//
Node*node_join(Node*left, Node*right) {
Node*n=node_create((uint8_t) '$', right->frequency+left->frequency);
n->left=left;
n->right=right;
returnn;
}
//
// Prints a node for debugging
//
// n: an address of a node to be stored
//
voidnode_print(Node*n) {
printf("%c:%ld\n", n->symbol, n->frequency);
if (n->left!=NULL&&n->right!=NULL) {
printf("left:[%c:%ld] right:[%c:%ld]\n", n->left->symbol, n->left->frequency,
n->right->symbol, n->right->frequency);
}
return;
}