Below is a function "get Huffman Tree" that is supposed to create a huffman encoding tree from the given frequency array (stored in freq) and its associated character array (stored in sym). In particu- lar, symbol sym has frequency freq. The variable size is the number of frequencies/characters. The program is not supposed to leak any memory. Forest *getHuffmanTree( int freq[], char sym[], int size ) { int i; CharFreq cf; Forest *f createForest(); Queue *pq = createForest(); 1 2 3 4 5 6 7 8 cÍ.sym 9 10 for(i=0; i<size; i++) { = (char*) malloc( sizeof (char)*2); cf.freq = freq; cf.sym [0] sym; enqueue pq, cf, cf.freq); addNode(f, cf); } 11 12 13 14 15 16 while( !isEmpty( pq )){ CharFreq least1 = dequeue pq); CharFreq least2 = dequeue ( pq ); 17 18 19 cf. sym = (char*) malloc( sizeof (char)*100); cf->freq least1.freq + least2.freq; 20 21 22 for(i=0; least1.sym !='\0'; i++) cf. sym = least1.sym; 23 24 25 for(j=0; least2.sym !='\0'; j++) cf.sym[i+j] = least2.sym[j]; 26 27 28 sym[i+j] '\0'; 29 30 31 addNode( f, cf); setParent( f, cf, leasti, least2 ); enqueuef, cf); 32 33 } 34
34 36 return f; freeQueue pq); 37 } Find at least 5 errors in the code (syntax/runtime/logic/memory) and fill them in the provided table. Give a short description of the error. You can list up to 10 errors. If at least 5 of your answers are correct you will receive full credit. There is no additional credit for finding more errors. Continued on next page CS2124: Data Structures Sp22-v02 3 Definition of the CharFreq struct: typedef struct CharFreq { int freq; /* frequency of these symbol(s) +/ char *sym; /* a null-terminated string representing one or more symbols */ а } CharFreq; Reminder of the Priority Queue methods: 1/Define the type being stored in the priority queue typedef CharFreq queue Type; //Creates a new empty min priority queue and returns a pointer to it. Queue *createQueue(); //frees the given priority queue pointer. void freeQueue ( Queue *pq); //Dequeues and returns the first queueType in queue //If the priority queue is empty calls exit(-1) queueType dequeue Queue *pq );
typedef CharFreq queueType; 1/Creates a new empty min priority queue and returns a pointer to it. Queue *createQueue(); //frees the given priority queue pointer. void freeQueue Queue *pq ); //Dequeues and returns the first queue Type in queue //If the priority queue is empty calls exit(-1) queueType dequeue Queue *pq ); //Inserts the queue Type into given priority queue with priority p void enqueue Queue *pq, queueType qt, int p); 1/Returns TRUE if the given priority queue is empty and FALSE otherwise bool isEmpty( Queue *pq); Reminder of the Forest methods: 1/Define the type being stored in the tree typedef CharFreq treeType; 1/Creates a new empty Forest and returns a pointer to it. Forest #createForest(); //frees the given Forest. void freeForest( Forest *t); 1/Adds the treeType p as a new node to the Forest void addNode( Forest *t, treeType p ); 1/Makes node containing treeType p the parent of nodes containing leftChild/rightChild. //leftChild is p's left child. rightChild is p's right child. void setParent Forest *t, treeType p, treeType leftChild, treeType rightChild );
Below is a function "get Huffman Tree" that is supposed to create a huffman encoding tree from the given frequency array
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Below is a function "get Huffman Tree" that is supposed to create a huffman encoding tree from the given frequency array
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!