Csce146practiceexam2 Notes
Csce146practiceexam2 Notes
1. Sort these Big O times in bounding order. IE One given can be bounded by the next one, then
the next one, etc.
O(nn) There really is no trick to this question, the best way to solve it is to just
memorize the proper order (in this case from least complex to most
O(lg(n)) complex). You can think about these times mathematically and know that as
n approaches infinity, the different Big O run times will grow at different
O(2n) rates.
2. For each algorithm what are its worst runtimes in Big O notation?
Binary search Like in the previous problem, there is no trick to this. You
will just have to memorize the specific Big O runtimes for
Merge Sort
each of the algorithms. Thankfully, you are only really
Quick Sort working with six Big O runtimes: O(lg(n)), O(nlg(n)), O(n2),
O(n), O(2n), and O(n!). If you remember from class JJ told
Insertion Sort us that if you are only going to remember one of the Big O
Bubble Sort runtimes, remember O(n2). As you can see, this run time is
shared by four of the sorting algorithms we learned. Don’t
Selection Sort be concerned with what the Tower of Hanoi and Travelling
Sales Person algorithms are. To my knowledge, we haven’t
Binary Search Tree Insertion
worked with them in class. Just know their runtimes.
Tower of Hanoi
***Recursive Method
public static boolean binarySearchRecursive(int[] a, int value, int minIndex, int maxIndex)
{
int midIndex = (maxIndex + minIndex)/2;
//System.out.println(midIndex);
if(minIndex > maxIndex)
{
return false;
}
if(a[midIndex] == value)
{
return true;
}
else
{
if(value > a[midIndex])
{
return binarySearchRecursive(a,value,midIndex+1,maxIndex);
}
else
{
return binarySearchRecursive(a,value,minIndex,midIndex-1);
}
}
}
This sample code uses recursion to perform binary search. First, we need to use the method’s
parameters to get the array of integers, the value we are searching for, the minimum index, and the
max index. This is shown at position 1. Then we determine the middle index by finding the average
between the minimum index and the maximum index shown in position 2. We then error check to
make sure that the minimum index is less than the max index in position 3. Next, we go to the index
at the middle of the array and if it is the value we are searching for then we return true at 4. In the
else statement at 5 we are telling the program that if the value is greater than the midIndex, then
recursively call the search again by setting minIndex to the midIndext + 1. Otherwise, we recursively
call the search again by setting the maxIndex to midIndex – 1.
Make sure you look at the slides for binary search. It can be helpful to think of binary search as
splitting the list in half and then looking through each piece.
Part 7 – Binary Search Trees
4. Remove 16 into this binary search tree. Show the end result. Showing your work is a good idea.
20
16 28
8 17 25 35
1 9 18 22 33 42
The answer to this question is straight forward. In deleting from a binary search tree, we first find the
value that we are trying to remove and then we remove the VALUE but not the NODE itself. We then go
to the right child of that node and follow the subtree down to is leftmost child. We then replace the
original node with the value in the leftmost child of the right subtree of the original node and delete that
child node.
In the case of this specific question, we are trying to delete 16. So, we find the node that contains that
value and we remove 16, but not the node it was stored in. We then travel down to the right child of the
node that contained 16 and try to find the leftmost child in that subtree. However, since there is no left
child in the subtree, we fill the node that contained 16 with the value 17. We are still not done. We must
delete a node, but we can’t delete the node that contained 17 because we can only delete nodes from
the bottom of the tree. Therefore, we run the deletion process again only this time we are looking to
delete 17. So, we find the node that contains 17, go to the right child of that node, and look for the
leftmost child of that subtree. We find 18, but there is no left child of 18. We replace 17 with the value
18 and delete the node that contained 18 because it is at the bottom of the tree.
5. What is the pre-order traversal for this tree?
20
16 28
8 17 25 35
1 9 18 22 33 42
Pre-order traversal is where you access the value of the node and traverse the left subtree and then the
right subtree, accessing each value as you go. Remember that pre-order traversal is not the only
traversal that you are required to know. You also need to know how to do in-order and post order
traversals.
In-order traversal is where you traverse each left subtree, THEN accessing the values, and then
traversing each right subtree. In-order traversal for this tree would look like: 1, 8, 9, 16, 17, 18, 20, 22,
25, 28, 33, 35, 42.
Post-order traversal is where you traverse the left subtree, then travers the right subtree, then access
the values. Post-order traversal for this tree would look like: 1, 9, 8, 18, 17, 16, 22, 25, 33, 42, 35, 28, 20.
6. Write the insert method for a linked version of a Binary Search Tree of integers. You may
assume that there is a Node object which has the attributes: data(int), leftChild(Node),
rightChild(Node). In the event of a tie, treat it as if it was greater than the value.
As with the first coding question on this practice exam, your ability to answer this question will come
from your knowledge of how BS Trees are built conceptually and how to implement that practically
using your knowledge of Linked Lists. Just remember how to set the root and then make comparisons
based on the root.
Part 8 – Heaps
7. Insert 17 into this max heap. Show the end result. Showing your work is a good idea.
20
16 6
8 11 4 5
1 2 3 10 - - - -
When inserting into a heap, remember that you are inserting the value into the first available child in
BREADTH ORDER. You are not inserting based on comparisons of the other nodes. Then you “bubble up”
using bubble sort to put the values in the proper positions. To insert 17 into the heap, we find the first
available child in breadth order. Then, we start using bubble sort to move the value into place. 17 is
greater than 4, so the values swap. 17 is greater than 6, so the values swap. 17 is not greater than 20, so
no swaps can be made and bubble sort ends.
8. Given this array representation of a max heap, write the array after the remove method is called
Index 0 1 2 3 4 5 6
Value 20 16 6 8 11 4 5
Index 0 1 2 3 4 5 6
Value
The answer is: 16, 11, 6, 8, 5, 4 because when you are removing from a heap, you always remove the
value at the root. From there, you take the last node via breadth order and place that value at the root.
Then, you “bubble down” by seeing which of the two children are larger, swapping the larger child, and
then continuing this bubble sort until no more swaps can be made. Just like with insert, you must
perform bubble sort every time you insert or remove a value from the heap. Remember that you can
read a heap into array format by traversing in breadth order starting at the root, left to right and top to
bottom.
9. Assuming this is a max heap, write a method that prints the heap sort. You may use methods
such as peek(), remove(), insert() as needed.
for(int i=size;i>0;i--)
System.out.print(tempHeap.remove()+" ");
This is basically just the code for the operation that we did above, removing and printing.
Part 9 – Graphs
10. Fill out the adjacency matrix for this graph. Row to Colum is From to To.
v1
v2 v3
v4 v5
v6 v7
V1 V2 V3 V4 V5 V6 V7
V1
V2
V3
V4
V5
V6
V7
I know that we have not discussed these graphs in class, but the concept is simple. The vertices listed in
the rows are the source and the vertices listed in the columns are the destination. Look at the direction
of the arrows (or edges) of the graph and see which vertices the original vertex leads to. If the source
directly connects to the destination, mark it as a 1 on the chart. Otherwise, mark it as a 0. For example,
V1 connects directly to V2, but does not connect directly to V5. Therefore, we put a 1 at (V2, V1) and a 0
at (V5, V1).
11. Give the DFS traversal of this graph starting from node v1.
v1
v2 v3
v4 v5
v6 v7
DFS stands for Depth First Search. How we perform this search is that we start from the specified node
(in this case v1) and then traverse through the graph until we cannot go any further. Since no
backtracking is not specified, the DFS traversal for this graph would be: v1, v2, v4, v3, v5, v6, v7.