Name:
CS 360 Exam 3 Fall 2014
October 22, 2014
1. (20 points) Suppose that you are given a 0-1 Knapsack problem with n items and a capacity C
knapsack. Each item, k, has a value vk and a size sk. Let the table T for solving the problem have
C rows and N c
CS 360 Exam 2
Summer 2014
Name _
1. Complete the pseudo-code method RightRotate of class RedBlackTree below so that it
will correctly perform a right rotation at the arbitrary specified node X in O(1) time.
[10 points]
P
X
V
U
P
V
Y
W
class Node cfw_
Elem
CS 360 Exam 1
Summer 2014
Solution
1. Hash tables. [15 points]
Given an initially empty hash table with capacity 11 and hash function H (x) = x % 11.
Keys 0, 1, 8, 9 are already in the table. Insert keys 52, 44, 56, 53, 61, 64 (in that order).
a. Linear p
CS 360 Exam 2
Summer 2014
Solution
1. Complete the pseudo-code method RightRotate of class RedBlackTree below so that it
will correctly perform a right rotation at any arbitrary specified node X in O(1) time.
[10 points]
P
X
V
U
P
V
Y
W
U
X
W
class Node c
CS 360 Exam 2
Summer 2014
Study guide
Be prepared to answer these kinds of questions:
Trace a greedy algorithm or dynamic programming algorithm, or some operations on an
red-black tree or (a,b)-tree. Determine the output or draw the data structure.
Determ
CS 360 Exam 1
Summer 2014
Study guide
Be prepared to answer these kinds of questions:
Short-answer questions about math concepts, stacks, queues, linked lists, trees, and the
other data structures we have discussed.
Trace given pseudo-code and determine t
CS 360: Data Structures and Algorithms
Spring 2013
(a,b) trees: generalization of (2,3) trees, for b 2a1
Non-binary trees
Each node except root is c-node where a c b
Root node is c-node where 2 c b
Each c-node holds c1 keys and c child links
Each node als
CS 360: Data Structures and Algorithms
Spring 2013
(2,3) trees:
Non-binary trees
Each node is either a 2-node or a 3-node
2-node holds 1 key and 2 child links
3-node holds 2 keys (key1 < key2) and 3 child
links
Each node also has parent link and degree (
CS 360: Data Structures and Algorithms
Spring 2013
Hash tables:
Another data structure for the dictionary ADT
Can be used instead of search trees
Dictionary ADT:
boolean find (ElementType x)
void insert (ElementType x)
void remove (ElementType x)
Alternat
CS 360: Data Structures and Algorithms
Spring 2013
Splay trees:
Splay tree = Binary search tree + Rotations
(No height information)
class Node cfw_
ElementType data;
Node parent, left, right;
Worst-case height of splay tree is (n)
Worst-case running tim
CS 360: Data Structures and Algorithms
Spring 2013
Height of a tree:
height (T) = height (T.root)
height(p) = 1 + max cfw_height(c) : c is child of p
height(p) = 1 + max cfw_height(p.left), height(p.right) if binary tree
height (leaf) = 0
height (null) =
CS 360: Data Structures and Algorithms
Spring 2013
Non-Binary Trees:
class Node cfw_
ElementType data;
Node parent, leftmostChild, rightSibling;
/ singly-linked list of each nodes children
class Tree cfw_
Node root;
Node:
A
B
E
C
F
B
E
null null
root
D
CS 360: Data Structures and Algorithms
Spring 2013
Binary Trees:
class Node cfw_
ElementType data;
Node parent, leftChild, rightChild;
class BinaryTree cfw_
Node root;
root
A
B
D
C
E
H
Node:
data parent
leftC rightC
F
A null
G
B
J
D
null null
C
E
H
null
CS 360: Data Structures and Algorithms
Spring 2013
Analysis of a program or algorithm
Suppose a program or algorithm runs for
T(n) = 6n3 + 7n2 + 8n + 9 time units
(nanoseconds or instruction cycles or steps)
Exact bound:
T(n) is (n3)
Upper bound:
T(n) is
CS 360 Exam 1
Summer 2014
Name _
1. Hash tables. [15 points]
Given an initially empty hash table with capacity 11 and hash function H (x) = x % 11.
Keys 0, 1, 8, 9 are already in the table. Insert keys 52, 44, 56, 53, 61, 64 (in that order).
a. Linear pro
CS 360 Exam 3
Summer 2014
Study guide
Be prepared to answer these kinds of questions:
Short-answer questions about graphs.
Trace some algorithms or operations on graphs, binomial heaps, Fibonacci heaps, or
disjoint sets. Determine the output or draw the d
Non-Binary Trees:
-disk read and disk write
(2,3)-tress:
Non-binary trees
Each node is either a 2-node or a 3-node
2- node holds 1 key and 2 child links
3-node holds 2 keys (key 1 < key2) and 3 child links
Each node also has parent link and degree (
CS 360 Quiz 5
Spring 2013
Solution
Suppose a binary tree is defined exactly as shown.
class Node cfw_
ElementType data;
Node parent, leftChild, rightChild;
class BinaryTree cfw_
Node root;
a. Using recursion, write an O(n)-time function CountNodes (Bina
CS 360 Quiz 4
Spring 2013
Solution
Given two doubly-linked lists L1 and L2 that each have the same size n, write an O(n)-time
pseudo-code function Interleave(L1, L2) that builds and returns a new doubly-linked list that
has size 2n and that consists of al
CS 360 Quiz 3
Spring 2013
Solution
Suppose classes Queue and Stack are declared exactly as shown below. Note that Stack is a
client of Queue because Stack has one field Q of type Queue. Do not add any additional fields
to class Stack. Assume that the meth
CS 360 Quiz 2
Spring 2013
Solution
Write the running time of each pseudo-code fragment below as a simplest function of n.
for (a=n*n*n; a>=0; a-=n)
z+;
(n2)
for (a=1; a<=n; a+=a)
z+;
(lg n)
for (a=2; a<=n; a*=a)
z+;
(lg lg n)
for (a=2*n; a<=4*n; a+)
for (
CS 360 Quiz 1
Spring 2013
Solution
In the pseudo-code below, recall that class X (Y) means that X is a subclass of Y. Determine the
values that are returned when each receiver object cfw_x[0], x[1], x[2], x[3], x[4] invokes each method
cfw_p, q, r, s, t a