You are on page 1of 5

COMSATS University, Islamabad

Islamabad Campus
Department of Computer Science

Read before Attempt


Assignment No. 4: NON-LINEAR DATA STRUCTURE-1
Course code and Title: CSC211, Data Structure and Algorithm
Instructor: Tanveer Ahmed
Assigned Date: November 27, 2019 Due Date: December 10, 2019
Total Marks: --
CLO-4: Apply non-linear data structures on simple problems.
Instructions:
1. This is an individual assignment. You will submit your work individually through your logins
(course portal)
2. Try to get the concepts, consolidate your concepts and ideas from these questions
3. You should concern recommended books for clarify your concepts as handouts are not
sufficient.
4. Try to make solution by yourself and protect your work from other students. If I found
the solution files of some students are same, then I will reward zero marks to all those
students.
5. Deadline for this assignment is December 2, 2019. This deadline will not be extended.

FALL 2019 Page 1


Question # 1
a) Reconstruct a Binary Tree from the following in-order and pre-order traversal
In-order Traversal: 4,7,2,8,5,1,6,9,3
Pre-order Traversal: 1,2,4,7,5,8,3,6,9
b) Reconstruct a Binary Tree from the following in-order and post-order traversal
In-order Traversal: 1,2,3,4,5
Post-order Traversal: 5,4,3,2,1
Question # 2
a) Consider the following data items in an array.

i.Draw Binary Search Tree (BST)


ii.Perform in order traversal on BST created in part i
iii.Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last item in
the list as pivot element.
iv. Draw Binary Search Tree for the pivots you choose (at each recursive call) in above tracing
of Quick sort.
v. Describe similarity and difference between BST-Sort( step i and ii) and quick sort( iii and iv)
b) Consider the following data items in an array.

i.Draw Binary Search Tree (BST)


ii.Perform in order traversal on BST created in part i
iii.Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last item in
the list as pivot element.
iv. Draw Binary Search Tree for the pivots you choose (at each recursive call) in above tracing
of Quick sort.
v. Describe similarity and difference between BST-Sort( step i and ii) and quick sort( step iii
and iv)
c) Consider the following data items in an array.

i. Draw Binary Search Tree (BST)


ii. Perform in order traversal on BST created in part i
iii. Illustrate how Quick Sort works on above array. Assume that Quick sort uses the last item in
the list as pivot element.
iv. Draw Binary Search Tree for the pivots you choose (at each recursive call) in above tracing
of Quick sort.

FALL 2019 Page 2


Question # 2
a) Place the words
Old programmers never die
They just lose their memories
In the order in which they appear, in a Binary Search Tree. Show your work step by step
b) List the order in which the nodes are processed using pre-order, in- order, and post- order traversal of BST
constructed in part a
c) Delete the node representing the word “old” from BST constructed in part a). Show your work step by step
d) Insert new word “young” in BST resulting in part c)
e) A binary tree T is balanced if every node v in T, the height of the left and right subtrees of v differ by at most
1. (Here the height of an empty tree is defined to be -1).
i. Find the Balanced factor of each node
ii. Is constructed tree balanced? If not, then make it balance (AVL tree) by performing appropriate
rotation. Show your work step by step
f) List the order in which the nodes are processed using pre-order, in- order, and post- order traversal of balanced
BST(AVL) constructed in part (e

Question # 3
a) Place the words
Old programmers never die
They just lose their memories
In the order in which they appear, in a Max heap and Min Heap. Show your work step by step
b) List the order in which the nodes are processed using pre-order, in- order, and post- order traversal of Max
heap and Min heap constructed in part (a
c) Delete the node representing the word “old” from Max heap and Min heap constructed in part a)
d) Insert new word “young” in Max heap and Min heap resulting in part c). Show your work step by step
e) Sort the Max heap and Min heap using heap sort. Show your work step by step.

Question # 4
a) Create a Huffman tree to determine the binary codes for each character in the following text:
Old programmers never die
They just lose their memories
Character Frequency Huffman Code

b) Fill the codes into the table above.


c) Encode the given text using prefix code.
d) How much %age the size of text reduces using these codes instead of ASCII code.

FALL 2019 Page 3


When applying Huffman encoding technique on an Image, the source symbols can be either pixel intensities
of the Image. Suppose the following 5x5 raster image. compressing a raster image using Huffman coding.

i. Write Huffman code of each colour


ii. Draw Huffman tree

A. We use Huffman's algorithm to obtain an encoding of alphabet {a, b, c} with frequencies fa , fb , fc.
In each of the following cases, either give an example of frequencies (fa , fb , fc) that would yield the
specified code, or explain why the code cannot possibly be obtained (no matter what the frequencies
are).
(a) Code: {0, 10, 11}
(b) Code :{ 0, 1, 00}
(c) Code: {10, 01, 00}
B. Under a Huffman encoding of n symbols with frequencies f1; f2; ……… ; fn, what is the longest a codeword
could possibly be? Give an example set of frequencies that would produce this case.
C. Consider the following 21 character message that consists of 3 a’s, 7 c’s, 6 t’s, and 5 g’s:
aaccccacttgggttttccgg
Are the following 43 bits a possible Huffman encoding of the message above?
0000001111000101010010010010101010111001001
Justify your answer as concisely and rigorously as possible.(No need to draw the Huffman tree)

D. A code for a, b, c, d, e is given by

character code
a 00
b 01
c 101
d x10
e yz1

Where x, y, z are in {0,1}. Determine x, y and z so that the given code is a prefix code

E. Write a frequency list that the Huffman code of this frequency would deterministically create the
following structure.

FALL 2019 Page 4


Question # 5
a) Represents the following expressions as a binary tree and write the prefix and postfix forms of the expression.
i. (A*B+C*D) –(A/B- (D+E))
ii. (((A+B)*C+D)*E) – ((A+B)*C-D)
iii. (A*b-C/D+E) + (A-B-C-D*D)/(A+B+C)
b) Represents the following postfix expression as binary tree and write the prefix form, the usual infix form, and
the fully parenthesized infix form of the expression
i) ABCD +*/E-
ii) ABC**CDE+/-
iii) AB+CD*EF/ - - A*
c) Evaluate Expression trees that you construct in part b using following Algorithm. Show your work step by
step
Where A = 5 B=7 C=3 D=2 E=4 F=8

FALL 2019 Page 5

You might also like