Test 2 Problems - Algorithms Data Structure | CP SC 212, Exams of Algorithms and Programming

Material Type: Exam; Class: ALGS/DATA STRUCTURES; Subject: COMPUTER SCIENCE; University: Clemson University; Term: Summer 2 2004;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-h9t
koofers-user-h9t 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 212-1 Name: _____________________________
Test #2 July 21, 2004
Closed books. Closed Notes. Calculators OK. 105 points. 75 minutes. Weight of each
question in parentheses. Please use a pencil. If you need more space, use the back of the
sheet.
1. (10) Insert the following values into an initially empty AVL tree. Indicate the type of
rotations performed (“S” for single, “D” for double). The end should be the final tree:
5 8 9 10 21 16 7 4 23 19 17 25
2. (10) Your friend says that he can use an array, a[i], to represent a tree using the
following rules. (a) The root is stored in a[0]. (b) The left and right children of the node
in a[i] are in a[2i+1] and a[2i+2], respectively. (c) The parent of the node in a[i] is
stored in a[(i-1)/2]. Note that his scheme uses array element a[0] and is different
from the 1-array representation we learned in class.
Does his scheme work? If so, fill in the values in the array to represent the tree on the
below. If not, explain why it doesn’t work.
i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
a[i]: __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __
Tree: root: I
children of I: C J children of F: E G
children of C: B F children of L: K M
children of J: _ L children of E: D _
children of B: A _ children of G: _ H
3. (10) Insert the following values into a B-Tree with L=M=3 (i.e., max number of keys
in a leaf is 3, maximum num of keys in an interior node is 2). Then answer the questions
following.
17 15 33 24 61 36 48 4 12 9 18 27 35 43 50 64 38
_____ (a) How many leaves does the tree have?
_____ (b) What values are contained in the second leaf from the left.
_____ (c) What values are contained in the root?
_____ (d) How many children does the root have?
_____ (e) What values are contained in the rightmost child of the root?
4. (10) Insert the following values into a 17-element hash table using hash function:
h1(x) = x%17. Use quadratic probing to handle collisions.
5 0 29 34 51 67 17 1 13 22 52 35
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
pf3
pf4

Partial preview of the text

Download Test 2 Problems - Algorithms Data Structure | CP SC 212 and more Exams Algorithms and Programming in PDF only on Docsity!

CPSC 212-1 Name: _____________________________

Test #2 July 21, 2004

Closed books. Closed Notes. Calculators OK. 105 points. 75 minutes. Weight of each

question in parentheses. Please use a pencil. If you need more space, use the back of the

sheet.

1. (10) Insert the following values into an initially empty AVL tree. Indicate the type of

rotations performed (“S” for single, “D” for double). The end should be the final tree:

2. (10) Your friend says that he can use an array, a[i], to represent a tree using the

following rules. (a) The root is stored in a[0]. (b) The left and right children of the node

in a[i] are in a[2i+1] and a[2i+2], respectively. (c) The parent of the node in a[i] is

stored in a[(i-1)/2]. Note that his scheme uses array element a[0] and is different

from the 1-array representation we learned in class.

Does his scheme work? If so, fill in the values in the array to represent the tree on the

below. If not, explain why it doesn’t work.

i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 a[i]: __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ Tree: root: I children of I: C J children of F: E G children of C: B F children of L: K M children of J: _ L children of E: D _ children of B: A _ children of G: _ H

3. (10) Insert the following values into a B-Tree with L=M=3 (i.e., max number of keys

in a leaf is 3, maximum num of keys in an interior node is 2). Then answer the questions

following.

_____ (a) How many leaves does the tree have?

_____ (b) What values are contained in the second leaf from the left.

_____ (c) What values are contained in the root?

_____ (d) How many children does the root have?

_____ (e) What values are contained in the rightmost child of the root?

4. (10) Insert the following values into a 17-element hash table using hash function:

h 1 (x) = x%17. Use quadratic probing to handle collisions.

___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___

5. (10) Insert the values into a 17-element hash table using hash function: h 1 (x)=x

%17. Handle collisions using double hashing with hash function : h 2 (x)=11-(x

___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___

6. (10) Consider the binary tree shown below. Then answer the following questions.

_____ (a) What is the height of node H?

_____ (b) What is the depth of node B?

_____ (c) What is the height of the tree?

_____ (d) How many interior nodes does the tree have?

_____ (e) (T/F) The tree is an AVL tree.

Tree: root: L children of L: H N children of G: B _ children of H: G I children of I: J K children of N: M Q children of Q: _ R

7. (10) Consider the Binary Search Tree in Problem #2. Draw the tree after removing

nodes C, J, and D, in order.

8. (6) The time to access a sector of disk is the sum of three distinct times:

_______________________, ________________________,

_______________________.

(4) If a disk has 16 disk surfaces, and each surface has 64K tracks, and each track has 32

sectors, and each sector has 512 bytes, what is the capacity in bytes of the hard disk?

Express your answer in terms one of the following: K, M, G, T, P, E.

9. (6) Convert the following Boolean expression to quadruples:

x = a >= 0 || y != z && w > 1 || z <= x + y ;

10. (9) Consider the tree in Problem #6. Print the reverse postorder (RLN), reverse

inorder (RNL) and reverse preorder (NRL) traversals of the tree.

RLN :

RNL:

NRL:

11. (10) Write a recursive method: int countLeaves(BinaryNode t) that returns the

number of leaves in the binary tree rooted at node t. Assume that BinaryNode has

public variables t.left, t.right, and t.element.

public int countLeaves (BinaryNode t) {

= a 0 t != y z t w 1 t && t1 t2 t || t0 t3 t

  • x y t <= z t5 t || t4 t7 t = t7 x

RLN: R Q M N K J I B G H L

RNL: R Q N M L K I J H G B

NRL: L N Q R M H I K J G B

public int countLeaves (BinaryNode t) { if (t == null) return 0; if (t.left == null && t.right == null) return 1; return countLeaves(t.left) + countLeaves(t.right); } // countLeaves