Nt1330 Unit 1 Assignment 1

632 Words3 Pages
#include #include #include #include #include #define _MAX 100 #define _SIZE 26 int id=0; struct node { char data; unsigned int freq; struct node *next; }*input,*input1; struct hfnode { char info; unsigned int prob; struct hfnode *l, *r; }; struct min_tree { unsigned int length; unsigned int hfm_cp; struct hfnode **nodes; }; void min_tree_construct(struct min_tree* min_tree, int node_id) { int minimum = node_id,l,r; struct hfnode* t; l = 2 * node_id + 1; r = 2 * node_id + 2; if (l < min_tree->length && min_tree->nodes[l]->prob < min_tree->nodes[minimum]->prob) minimum = l; if (r < min_tree->length && min_tree->nodes[r]->prob < min_tree->nodes[minimum]->prob)…show more content…
minimum = r; if (minimum != node_id) { t = min_tree->nodes[minimum]; min_tree->nodes[minimum]=min_tree->nodes[node_id]; min_tree->nodes[node_id]=t; min_tree_construct(min_tree, minimum); } } struct hfnode* min_take(struct min_tree* min_tree) { struct hfnode* tmp = min_tree->nodes[0]; min_tree->nodes[0] = min_tree->nodes[min_tree->length - 1]; --min_tree->length; min_tree_construct(min_tree, 0); return tmp; } void insertmin_tree(struct min_tree* min_tree, struct hfnode* hfnode) { ++min_tree->length; int a = min_tree->length - 1; while (a && hfnode->prob < min_tree->nodes[(a - 1)/2]->prob) { min_tree->nodes[a] = min_tree->nodes[(a -…show more content…
(struct hfnode**)malloc(min_tree->hfm_cp * sizeof(struct hfnode*)); int i; struct node *temp2; temp2 = input; i=0; while(temp2!=NULL) { info[i]=temp2->data; freq[i]=temp2->freq; temp2=temp2->next; i++; } for(i=0;inodes[i] = new_node(info[i],freq[i]); min_tree->length = length; int sz,count; sz = min_tree->length - 1; for (count = (sz - 1) / 2; count >= 0; --count) min_tree_construct(min_tree, count); while (!(min_tree->length == 1)) { l1 = min_take(min_tree); r1 = min_take(min_tree); tmp = new_node(' ', l1->prob + r1->prob); tmp->l = l1; tmp->r = r1; insertmin_tree(min_tree, tmp); } rt = min_take(min_tree); codes_hfm(rt, arr, t); } void display() { struct node *traverse; traverse=input; if(traverse==NULL) { printf(" "); } else { while(traverse!=NULL) { printf(" %c: %d\t ",traverse->data,traverse->freq); traverse=traverse->next; } } } void insert(char ch,int freq) { struct node* temp,*temp_2; temp=input; temp_2=input; input1=(struct node*)malloc(sizeof (struct

More about Nt1330 Unit 1 Assignment 1

Open Document