Skip to content

Commit 4bf9a8a

Browse files
Gathrosleios
authored andcommitted
Fixed huffman edge case failure in C. (algorithm-archivists#661)
1 parent 5227e00 commit 4bf9a8a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

contents/huffman_encoding/code/c/huffman.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ struct tree* generate_tree(const char* str) {
134134
}
135135
}
136136

137+
if (heap.length == 1) {
138+
struct tree* leaf = heap_pop(&heap);
139+
struct tree* root = calloc(0, sizeof(struct tree));
140+
root->left = leaf;
141+
root->count = leaf->count;
142+
heap_free(&heap);
143+
return root;
144+
}
145+
137146
while (heap.length > 1) {
138147
struct tree* left = heap_pop(&heap);
139148
struct tree* right = heap_pop(&heap);

0 commit comments

Comments
 (0)