Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CPSC 212-302 Test #1 Solutions, Exams of Algorithms and Programming

The solutions to test #1 of the cpsc 212-302 course. It includes the analysis of a pseudo-code segment, the execution of a code segment, the insertion of values into an avl tree, the traversals of a binary tree, the conversion of a binary tree to a k-ary tree, the completion of a java method, and explanations of radix sort and balanced/self-adjusting trees.

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-hpu-1
koofers-user-hpu-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 212-302 Name: _____________________________
Test #1B February 9, 2005
Closed books. Closed Notes. Calculators OK. 85 points. 60 minutes. Weight of each
question in parentheses. Please use a pencil. For more space, use the back of the sheet.
1. (10) Consider the following section of pseudo-code:
int i = 8*N;
while(i>0) {
for (int j=0; j<i-1; j++)
M[i] += A[i]*B[j]; // statement #1
i -= 8;
} // while
Analyze the code and develop an equation that specifies the total number of
multiplications performed in statement #1. Your answer should be expressed in terms
of N. Show how you arrived at your answer.
2. Consider the following code:
for (int i=0; i<10; i++) {
int x = (i%2==0)?((i>5)?(i*10):(i*5)):((i>6)?(i+15):(i-13));
System.out.println(i + ": " + x);
} // i
(5) What is displayed by the code segment? | (5) Rewrite the assignment statement using
| if-then-else statements
|
3. (10) Insert the following values into an initially empty AVL tree ignoring duplicates.
Show when, how, and what type (Single or Double) of rotations are performed. Draw
a box around the final tree.
14 21 46 1 7 16 5 8 15 27 6 12 10 9
4. (4) Give the reverse postorder (RLN) _______________________________and the
reverse inorder (RNL) ________________________traversals of the following tree.
root: F (6) Convert the binary tree to a k-ary tree.
children of F: E _
children of E: A B
children of B: C Q
children of A: H K
children of Q: G _
children of H: I J
pf3
pf4

Partial preview of the text

Download CPSC 212-302 Test #1 Solutions and more Exams Algorithms and Programming in PDF only on Docsity!

CPSC 212-302 Name: _____________________________

Test #1B February 9, 2005

Closed books. Closed Notes. Calculators OK. 85 points. 60 minutes. Weight of each

question in parentheses. Please use a pencil. For more space, use the back of the sheet.

1. (10) Consider the following section of pseudo-code:

int i = 8N; while(i>0) { for (int j=0; j<i-1; j++) M[i] += A[i]B[j]; // statement # i -= 8; } // while

Analyze the code and develop an equation that specifies the total number of

multiplications performed in statement #1. Your answer should be expressed in terms

of N. Show how you arrived at your answer.

2. Consider the following code:

for (int i=0; i<10; i++) { int x = (i%2==0)?((i>5)?(i10):(i5)):((i>6)?(i+15):(i-13)); System.out.println(i + ": " + x); } // i

(5) What is displayed by the code segment? | (5) Rewrite the assignment statement using

| if-then-else statements

3. (10) Insert the following values into an initially empty AVL tree ignoring duplicates.

Show when, how, and what type ( S ingle or D ouble) of rotations are performed. Draw

a box around the final tree.

4. (4) Give the reverse postorder (RLN) _______________________________and the

reverse inorder (RNL) ________________________traversals of the following tree.

root: F (6) Convert the binary tree to a k-ary tree.

children of F: E _ children of E: A B children of B: C Q children of A: H K children of Q: G _ children of H: I J

5. (10) Consider the following infix expression. Show the postfix expression and the

quadruples generated by the program you are developing for assignment #1.

infix: y = 2 / b * c ^ - 5 + d + - ( z * c ) ^ - 5 ; postfix: _____________________________________________________ quadruples:

6. (10) The following Java method boolean evalInput(String in) is intended to

return “true” if the String argument is balanced and “false” otherwise. Tokens consist

exclusively of “{“, “}”, “[“, “]”, “(“, and “)” and are separated by spaces. For example:

String returned boolean

[ ( { } { ( ) } ) { } ] { } true

[ ( ] ) false

[ { ( ) } false

] [ false

{ { { } } } [ ( ) ] true

Complete the method.

public boolean evalInput(String input) { StringTokenizer in = new StringTokenizer(input); } // evalInput

7. (a) In Section 3.2.6, Weiss provides three examples that use linked lists. One of the

examples is radix sort. He says that radix sort is also known as card sort.

(3) First briefly explain radix sort. (2) Then explain why it is also called card sort.

(b) At the end of section 4.3 (p. 118), Weiss argues that the potentially poor

performance of inserts into and deletes from unbalanced binary search trees led to the

development of balanced search trees and self-adjusting trees. (3) Give an example

of a balanced search tree. (2) Give an example of a self-adjusting tree.

8. (10) Freebie

~ 5 _ t ^ t5 t6 t ~ t7 _ t

  • t4 t8 t = t9 _ y
  1. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean evalInput(String input) { System.out.print(input + ": "); StringTokenizer in = new StringTokenizer(input); Stack s = new Stack(); while(in.hasMoreTokens()) { String oneToken = in.nextToken(); if (oneToken.equals("[") || oneToken.equals("(") || oneToken.equals("{")) s.push(oneToken); else if (s.isEmpty()) // one too many close tokens return false; else if (oneToken.equals("]") && !((String)s.peek()).equals("[") || oneToken.equals("}") && !((String)s.peek()).equals("{") || oneToken.equals(")") && !((String)s.peek()).equals("(")) return false; // mismatched open and close tokens else s.pop(); // so far so good } // if (!s.isEmpty()) // one too many open tokens return false; else // everything was OK return true; } // evalInput // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. Radix sort is a generalization of bucket sort. In each pass, keys are distributed among N buckets based on a single digit of the key, starting from the least significant digit to the most significant digit. It is sometimes called card sort because it was the method used to sort punched cards. (p. 66) balanced search tree: AVL (p. 118) self-adjusting tree: splay (p. 118)