incorrect binary tree
while(count != 25) {
tail = head;
new_node = (binary_node*)malloc(sizeof(binary_node));
while(tail->next != N开发者_运维百科ULL)
tail = tail->next;
tail->next = new_node;
new_node->element.frequency = (p->element.frequency + q->element.frequency);
new_node->LSON = p;
new_node->LSON->RTAG = 0;
new_node->RSON = q;
new_node->RSON->RTAG = 1;
head = new_node;
n = n - 1;
head = q->next;
sort(n, head);
p = head;
q = p->next;
count++;
}
The code above should generate a huffman tree. However, the binary tree that is formed is incorrect. All the nodes that contains a letter should be a leaf or a node without a son but some alphabet nodes still have sons. What is wrong with the code?
malloc
returns you a memory area full of garbage.
Since you never set for the new_node that it's not an alphabet node sometimes you'll find garabage there saying that it is actually an alphabet node.
consequence for verification: you should find more alphabet nodes that you originally had.
精彩评论