CMPT 225
Final Review
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-1
Time and Place
• Exam is at 7:00-10:00pm on April 10
• Room: HCC 1315
• Exception... for the 3 students with a
conflict in Burnaby, the room will be
AQ4120
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-2
Topics Covered (before midterm)
• Java review
• Software Engineering
• Recursion
– (and a little algorithm efficiency)
•
•
•
•
Abstract Data Types
Linked Lists
Stacks
Queues
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-3
Topics Covered (after midterm)
•
•
•
•
•
•
Java Interfaces and Generics
Trees
Tables and Priority Queues
Hashing
Graphs
Sorting
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-4
Topics Covered
• Roughly: chapters 1-14
• Minus chapter 6... though we did some of it
in class
• We didn’t read the second half of ch. 7, but
we covered it in class
• Didn’t read first half of ch. 10, but we
covered the material in class
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-5
Java Review
• You should be able to read Java code
– You don’t need to memorize a bunch of
java.util classes
• You should be able to write Java code
– Defining your own classes, interfaces, methods,
etc.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-6
Software Engineering
• What is a good solution to a problem?
• Object oriented design
– Read a UML diagram
– Write a UML diagram
• Modularity, modifiability, readability, ease
of use, fail-safe programming…
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-7
Recursion
• Write recurrence relations
– e.g. rabbits(n) = rabbits(n-1) + rabbits(n-2)
• Write recursive (Java) methods
• Solve problems with simple recursive
solutions
• Backtracking
• Relation with induction
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-8
Algorithm Efficiency
• Big-O notation:
– What does O(n) mean?
– What about O(n2)?
• Count the steps of an algorithm, translate to
big-O
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-9
Abstract Data Types
• Difference:
– ADT vs. data structure
• How do you specify ADTs?
• Designing the right ADT for a problem
• ADT List and SortedList
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-10
Linked Lists
• Object references
• Using nodes and references
• Programming with linked lists
– inserting, deleting, traversing
• Variations
– tail references, circular lists, doubly linked
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-11
Stacks
• ADT Stack
– LIFO
• Implementations
• Problem solving with Stacks
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-12
Queues
• ADT Queue
– FIFO
• Implementations
• Problem solving with Queues
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-13
Java Generics
• What is a generic class?
• Why do you use them?
• How do you define one?
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-14
Trees
•
•
•
•
•
•
General trees
Binary trees
Binary search trees
Traversals (in-order, pre-order, post-order)
Implementations
Efficiency of search
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-15
Tables
• Basic implementations
– array, reference
• Faster implementations
–
–
–
–
binary search tree
2-3 trees
red-black trees
hash functions
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-16
Hashing
• What is a good has function?
• What is a collision?
• Collision resolution
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-17
Graphs
• Definitions and basic properties
• Implementations
– adjacency list
– adjacency matrix
• Traversals and spanning trees
– depth-first
– breadth-first
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-18
Sorting
• Quadratic sorts:
– Selection, insertion, bubble
• Log linear sorts:
– merge, quick
• Special case: radix
• Should be able to explain/sketch/understand
different sorts
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-19
Example 1
• What can go wrong with this method?
How can you protect yourself?
public static double computation(double x)
{
return java.lang.Math.sqrt(x) /
java.lang.Math.cos(x);
}
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-20
Example 1 - Solution
Two hazards:
1) the argument x cannot be a negative number
because computation() calls the sqrt() method
with x as the argument.
2) since zero is in the range of the method
cos(x), the result of this computation must be
tested prior to using it as a divisor in order to
avoid a "Divide by Zero error".
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-21
Example 1 – Solution
double computation( double x )
{
double denominator = Math.cos(x);
double result = 0;
if ( x < 0 )
{ System.out.println("Cannot take square root of negative number");
result = -1;
}
if ( denominator == 0 )
{ System.out.println("Cannot divide by zero");
result = -1;
}
if ( result != -1 )
result = Math.sqrt(x)/denominator;
return result;
}
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-22
Example 2
• Write the loops invariant for this:
void printN(int n)
{
int i = 20;
while(i>1)
i--;
}
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-23
Example 2 - Solution
• 20 >= i >= 1
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-24
Example 3
• Write a recursive method that will compute
the sum of the first n integers in an array of
at least n integers.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-25
Example 3 - Solution
int computeSum(final int a[], int n)
{ // base case
if(n <= 0)
return 0;
else // reduce the problem size
return a[n - 1] + computeSum(a, n - 1);
} // end computeSum
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-26
Example 4
• Write a recursive Java method that writes
the digits of a positive decimal integer in
reverse order.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-27
Example 4 - Solution
void reverseDigits(int number){
if(number >= 0) // check for input bounds
{ // base case
if(number < 10)
System.out.print(number);
else
{ // print out rightmost digit
System.out.print(number % 10);
// pass remainder of digits to next call
reverseDigits(number / 10);
} // end if
} // end if
} // end reverseDigits
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-28
Example 5
• Implement a method “swap” for lists... use
the ADT list, and handle the exception if the
positions being swapped do not exist.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-29
Example 5 - Solution
void swap(List aList, int i, int j)
{
Object first, second;
try{
first = aList.get(i);
second = aList.get(j);
}
catch(Exception e){
System.out.println(e);
}
aList.remove(i);
aList.add(i, second);
aList.remove(j);
aList.add(j, first);
} // end Swap
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-30
Example 6
• Trace the selection sort as it sorts the
following array:
• 7, 12, 24, 4, 19, 32
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-31
Example 6 - Solution
•
•
•
•
•
•
7
4
4
4
4
4
12
12
7
7
7
7
24
24
19
12
12
12
4
7
12
19
19
19
19
19
24
24
24
24
© 2006 Pearson Addison-Wesley. All rights reserved
32
32
32
32
32
32
8 A-32
Example 7
Draw the complete binary tree obtained by
inserting the following values in the given
order:
4, 13, 5, 3, 7, 30
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-33
Example 7 - Solution
4
/ \
13 5
/ \ /
3 7 30
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-34
Example 8
Draw the binary search tree obtained by
inserting the following values in the given
order:
4, 13, 5, 3, 7, 30
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-35
Example 7 - Solution
4
/ \
3 13
/ \
5 30
\
7
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-36
Example 9
• A general tree can be implemented by
having an array of child references.
• 1) What are the advantages and
disadvantages of this implementation?
• 2) Write a recursive preorder traversal
method for a general tree.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-37
Example 9 - Solution
• Advantages: Access to the children is easy; direct
access to each child is available.
• Disadvantages: Limits the number of children to a
fixed maximum. If the maximum number of
children possible is higher than the average
number of children, then memory is wasted. If the
children are kept in order, insertions and deletions
may require shifting pointers in the array.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-38
Example 9 - Solution
public void traverse(TreeNode tNode);
{ if (tNode != null)
{
queue.enqueue(tNode .getItem());
for (int i=0; i<tNode .numChildren; ++i)
traverse(tNode.child[i]);
} // end if
} // end traverse
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-39
Example 10
• What are the advantages of implementing
ADT Table with a 2-3 tree instead of a
binary search tree?
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-40
Example 10 - Solution
The height of a binary search tree depends on the order in
which the items are inserted.
Advantage: In a tree with N items, the height can range
between N (equivalent to a linked list) and log2 ( N+1) (a
fully balanced tree). A 2-3 tree is always balanced and thus
has height proportional to log N. So, a 2-3 tree is a more
efficient ADT table implementation than a binary search
tree.
Advantage: Maintaining a balanced binary search tree may
become very expensive in the face of frequent inserts and
deletions as the entire tree must be rebuilt in the worst
case.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-41
Example 11
• Write pseudocode for the
tableDelete operation when the
implementation uses a hash-table
with seperate chaining.
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-42
Example 11 - Solution
tableDelete(Object item)
hashval = h(item.searchKey)
let LL = the linked list at table[hashval]
LL.delete(item)
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-43
Example 12
• Briefly explain/define these terms:
– 2-3 tree
– hash function
– adjacency list
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-44
Example 12
• Briefly explain/define these terms:
– 2-3 tree
• tree where each node contains either 1 or 2 data items. nodes
containing 1 data item have 2 children, nodes containing 2 data
items have 3.
– hash function
• a function that maps a search key to a position in an array, to
implement an ADT table with efficient search/retrieval
– adjacency list
• a way to implement the ADT graph in which the nodes
adjacent to each node are stored in a linked list
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-45
Example 13
• Given the following running times for input
n, what is the big-O measure of efficiency
– n + 6000
– n (log n) + n(n2 + 6000)
– log n + 6000n
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-46
Example 13 - Solution
• Given the following running times for input
n, what is the big-O measure of efficiency
– n + 6000
----------------------- O(n)
– n (log n) + n(n2 + 6000) ------------ O(n3)
– log n + 6000n
-------------------- O(n)
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-47
Example 14
• Which ADT is appropriate for each type of
data:
– A pile of shoeboxes in a warehouse
– The World Wide Web
– A collection of employees at a company
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-48
Example 14 - Solution
• Which ADT is appropriate for each type of
data:
– STACK
– DIRECTED GRAPH
– TABLE
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-49
Some more notes
• The final will look a lot like the midterm....
same kinds of questions
• Some people on the midterm seemed to
have trouble with recursion... it will be back
• There will be no cheat sheet of ADT
operations this time
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-50
More types of examples
• Displaying contents of a Queue or Stack
• Writing data structure for an ADT... say the
Bag from earlier... need to decide data
members
• Inserting nodes in doubly linked/circular
linked lists
© 2006 Pearson Addison-Wesley. All rights reserved
8 A-51