Question#4 [25 points]

advertisement
KING FAHD UNIVERSITY OF PETROLEUM & MINERALS
Information and Computer Science Department
ICS202: Data Structures
HOMEWORK 3 (Term 081)
Due Date: Monday, December 22nd, 2008
QUESTION#1 [35 points]
(a)
(6 points) Given the following General tree, T:
A
F
i.
ii.
B
C
I
L
G
D
J
E
K
H
(3 points) Show the result of the preorder depth first traversal of T.
(3 points) Show the result of the postorder depth first traversal of T.
(b)
(3 points) The breadth-first traversal of a full Binary Search tree gives its keys in the
following order: 20, 17, 32, 12, 18, 8, 15, 14, 16. Draw the tree.
(c)
(15 points) Consider the following array of 10 elements: 20, 14, 13, 12, 9, 7, 6, 5, 3, 1.
i.
(4 points) Convert the array into a MinHeap using the bottom-up approach. Make
sure you show your intermediate results.
ii.
(4 points) Convert the array into a MinHeap using the top-down approach. Make
sure you show your intermediate results.
iii. (3 points) When using binary heaps as priority queues, does it make a difference
which method is used to build the heap initially? If no, explain why, and if yes,
mention which one to use and explain why.
iv.
(2 points) Insert the node with key 8 to the MinHeap of Part i.
v.
(2 points) Show the tree after applying dequeueMin to the MinHeap of Part ii.
(d)
(11 points) Consider an empty AVL tree.
i.
(7 points) Insert the following nodes into the AVL tree: 20, 18, 30, 25, 12, 22, 35,
33, 31. Make sure you show the intermediate results and mention all the rotations
involved.
ii.
(4 points) Remove the key 12 from the AVL tree resulting from Part i. Make sure
you show the intermediate results and mention all the rotations involved.
QUESTION#2 [35 points]
(a)
(b)
(c)
(d)
(8 points) Answer Question 2(d) page 289 from the Textbook.
(10 points) Answer Question 3 page 289 from the Textbook.
(10 points) Answer Question 4 page 289 from the Textbook.
(7 points) Answer Question 5(d) page 289 from the Textbook.
QUESTION#3 [30 points]
(a) (10 points) For a complete binary tree, show that its height is O(log n) by approximating the
height of a complete binary tree with k levels, where nodes in all levels are filled.
(b) (20 points) Assume that binary search trees allow the insertion of duplicate keys into them,
call them modified binary search trees, denoted by MBST. Assume that they are modified
such that each node satisfies the MBST property: The value of the keys of the nodes in the left
subtree are strictly less than the key at the node, and the value of the keys of the nodes in the
right subtree are greater than or equal to the key at the node. Answer the following
questions:
i.
(5 points) The MBST property suggests that duplicate nodes of a certain node will
only exist in the right subtree of that node. Give a good reason not to allow
duplicate keys to exist in both subtrees of a certain node.
ii.
(15 points) Can we use both methods of deletion of the regular BST's in MBST's? If
yes, explain why. If no, mention which one to use or update the deletion method if
both methods do not work without applying major changes to their current
implementations.
Submission Instructions:
Since this homework does not include any programming assignment, make sure you
include the solution of this homework in a word document and submit it in both,
electronic form and in hard copy.
Download