12.07.2015 Views

Algorithms and Data Structures Written Exam Proposed Solution

Algorithms and Data Structures Written Exam Proposed Solution

Algorithms and Data Structures Written Exam Proposed Solution

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong><strong>Written</strong> <strong>Exam</strong><strong>Proposed</strong> <strong>Solution</strong>2005-10-25from 13:30 to 17:30• Allowed tools: A st<strong>and</strong>ard calculator.• Grading criteria: You can get at most 30 points.For an E, 12 points are required.For an D, 16 points are required.For an C, 20 points are required.For an B, 24 points are required.For an A, 28 points are required.• Responsible: Verónica Gaspes, telephone +46 +35 167380.• Read carefully! Some exercises might include explanations, hints <strong>and</strong>/orsome code. What you have to do in each exercise is marked with the pointsthat you can get for solving it (as (X pts.)).• Write clearly! Text that is difficult to read <strong>and</strong> code that is not wellstrucutred will not be read by the teacher marking your exam.• Motivate your answers!1


Good Luck!2


1. What follows is the code for insertion sortpublic static void sort(T[] a){for( int p = 1; p < a.length; p++ ){T tmp = a[ p ];int j = p;}}for( ; j > 0 && tmp.compareTo( a[ j - 1 ] )


to do what you want.static boolean sum2ToK (int [] a, int k){for(int i = 0; i


takes for x all the values in c one at a time. (It is usually read as for eachT x in c do something)For example, to print all elements in a list of integers, we can writevoid printAll(List li){for (Integer x : li)System.out.println(x);}for each integer x in li print x.For collections with an order (like TreeSet) the elements are retreived inorder.(a) (6 pts.) Write a methodvoid printReverse(Collection col)that prints the the elements of col in reverse order.Hint: You might need a datastructure to organize the elements of thecollection temporarily. void printReverse(java.util.Collection col){java.util.Stack st = new java.util.Stack();for(T e : col)st.push(e);while(!st.isEmpty())System.out.println(st.pop());}4. What follows is an implementation of Binary Nodes for elements that canbe compared:class BinaryNode


(a) (3 pts.) Add a methodBinaryNode findMin()that returns the binary node with least value in this binary node.BinaryNode findMin(){if(left==null && right==null)return this;else if(left == null){BinaryNode rm = right.findMin();if(rm.element.compareTo(element)>=0) return this;else return rm;}else if(right == null){BinaryNode lm = left.findMin();if(lm.element.compareTo(element)>=0) return this;else return lm;}else{BinaryNode rm = right.findMin();BinaryNode lm = left.findMin();BinaryNode m =(rm.element.compareTo(lm.element)>=0)?lm:rm;if(m.element.compareTo(element)>=0) return this;else return m;}}(b) (3 pts.) Write a methodBinaryNode findMinInSearchTreeNode()that returns the binary node with least value in this binary nodeassuming that it has the search property.Notice: A solution in which you do not use the assumption will notbe considered correct.Reminder: A binary node has the search property if all nodes in theleft child are smaller that the element in the root, all nodes in the rightchild are bigger than the element in the root <strong>and</strong> the left <strong>and</strong> rightchildren have the search property.BinaryNode findMinInSearchTreeNode(){if(left==null) return this;else return left.findMinInSearchTreeNode();}6


5. Below you find the implementation of graphs we used in the third laboration.(a) (6 pts.) Write a methodvoid printAdjacents(String vertexName)that prints the names of all vertices that are adjacent to the vertexwhose name is given as argument.Reminder: The method to access a Map for a given key is get, itexpects the key as argument <strong>and</strong> returns the value to what that keyis mapped to.void printAdjacents(String vertexName){Vertex v = vertexMap.get(vertexName);for(Edge e : v.adj)System.out.println(e.dest.name);}class Edge{public Vertex dest; // Second vertex in Edgepublic double cost; // Edge cost}public Edge( Vertex d, double c ){dest = d;cost = c;}class Vertex{public String name; // Vertex namepublic List adj; // Adjacent vertices}public Vertex( String nm ){ name = nm; adj = new LinkedList( );}public class Graph{// The graph is stored in a map mapping String to Vertex:7


private Map vertexMap = new HashMap( );// some auxiliary methods that help build the graph ...void printAdjacents(String vertexName){// implement this!}}8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!