Docsity

CMSC 132 Quiz 3 Worksheet: Huffman Coding and Heaps, Quizzes of Computer Science

Toggle sidebar
bg1
CMSC 132 Quiz 3 Worksheet
The third quiz for the course will be on Wednesday, Mar 7, during your lab session. The
following list provides more information about the quiz:
• The quiz will be a written quiz (no computer).
• Closed book, closed notes quiz.
• Answers must be neat and legible. We recommend that you use pencil and eraser.
The following exercises cover the material to be included in this quiz. Solutions to these
exercises will not be provided, but you are welcome to discuss your solutions with the TA or
instructor during office hours. We strongly recommend you do not use Eclipse to write
the code associated with these exercises. Try to answer the exercises in a piece of
paper and then use Eclipse to verify your solutions. This approach will better prepare you for
the quiz.
A. Huffman
1. Consider the following Huffman tree.
i. Decode the sequence “10010110”
ii. Encode the string “CED”
2. Create a Huffman tree for the symbols A, B, C, and D which have the following
frequencies:
A : 9 B : 4 C : 2 D : 8
3. Create a Huffman tree for the symbols A, B, C, D, and E which have the
following frequencies:
A : 9 B : 2 C : 2 D : 8 E:8
4.
In the previous problem, how many Huffman tress are possible assuming we always
assign 0 to the right subtree and 1 to the left subtree?
A
D
1
1
0
1
0
0
0
E
C
B
1
pf2

Partial preview of the text

Download CMSC 132 Quiz 3 Worksheet: Huffman Coding and Heaps and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132 Quiz 3 Worksheet

The third quiz for the course will be on Wednesday, Mar 7, during your lab session. The following list provides more information about the quiz:

  • The quiz will be a written quiz (no computer).
  • Closed book, closed notes quiz.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.

The following exercises cover the material to be included in this quiz. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with the TA or instructor during office hours. We strongly recommend you do not use Eclipse to write the code associated with these exercises. Try to answer the exercises in a piece of paper and then use Eclipse to verify your solutions. This approach will better prepare you for the quiz.

A. Huffman

1. Consider the following Huffman tree.

i. Decode the sequence “10010110”

ii. Encode the string “CED”

2. Create a Huffman tree for the symbols A , B , C , and D which have the following

frequencies:

A : 9 B : 4 C : 2 D : 8

3. Create a Huffman tree for the symbols A , B , C, D, and E which have the

following frequencies:

A : 9 B : 2 C : 2 D : 8 E:

4. In the previous problem, how many Huffman tress are possible assuming we always

assign 0 to the right subtree and 1 to the left subtree?

A

D

1

1

0

0 1

0

0

C E

B

1

B. Heaps

  1. Draw the heaps (in tree representation) created after completing each of the following operations. Assume the heap has the smallest element at top.

Heap after inserting 9 Heap after inserting 40 in the previous heap Heap after inserting 2 in the previous heap Heap after inserting 7 in the previous heap Heap after inserting 10 in the previous heap Heap after inserting 30 in the previous heap Heap after deleting 2 from the previous heap

  1. MaxHeap

The MaxHeap class represents a max-heap. In a max-heap every element has a key value greater than or equal to either of its children. The class definition associated with the MaxHeap class is:

public class MaxHeap<E extends Comparable> { ArrayList data = new ArrayList(); }

Complete the implementation of the MaxHeap class, by providing the following methods:

a. insert( ) – Inserts an element in the heap. b. removeMax( ) – Removes the largest element from heap and returns that element. c. size( ) – Returns the size of the heap.

C. Binary Trees

The following Java class definition for a binary tree will be used to answer the questions that

follow. Unlike project 3, the following tree is not polymorphic.

public class BinarySearchTree <E extends Comparable> { private class Node { E data; Node left, right; } Node root; }

1. Define a constructor that creates an empty tree.

2. Define a method add(E val) that adds a node to the proper location in the tree.

3. Define a method remove(E val) that removes a node from the tree.

4. Define a recursive method numLeaves( ) that returns the number of leaves in the tree.

5. Define a recursive method height( ) that returns the height of the tree.

6. Define a recursive method isFull( ) that returns true if every interior node in the tree

has both a left and right subtree.