Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 240 Computer Science II Exam 3 Page 1 April 4, 2012 Name_________Key___________ 1. . For each data structure in the table, give the complexity, O(1), O(log n), or O(n) for each of the operations [16 pts] Find last Search for an Traverse the Insert an element arbitrary list element element O(n) O(n) O(n) O(1) Linked List (unsorted) Unsorted array O(1) O(n) O(n) O(1) Sorted array O(1) O(log n) O(n) O(n) Binary Search Tree (inorder) O(log n) O(log n) O(n) O(log n) 2. Fill in the blanks to implement a recursive algorithm to find the longest of the strings in a linked list. The public helper function that calls the recursive function is given: [10 pts] public __String_ getLongest(){ return getLongest(head); } private __String__ getLongest(LLNode cur){ if( _cur___ == __null_ ) {//base case: end of list or empty return ””; } String long = ___getLongest__ (____cur__ .getNext()); if(long._compareTo_____(____cur___.getData() > 0){ return _____long____________; } else { return _cur.getData()____; } } 3. Convert the following normal infix expression to prefix and postfix. Account for proper operator precedence. Be sure the operands in the expressions (a,b,c,d,e) are in the same original order. [12 pts] Draw the expression tree for a + b / c * d - e : Prefix (pre-order traversal):___- + a / b * c d e________ Postfix (post-order traversal):_____a b c / d * + e -__________ CS 240 Computer Science II Exam 3 Page 2 4. Show the output of the following ancestor traversal routine against the data to the right with a call to Ancs(BobHowe). Note carefully the order of the recursive calls and note mothers and fathers are not consistently left or right. [8 pts] public static void Ancs(Relative inx) { if(inx==Unknown){ return; } else { System.out.println(inx); Ancs(father[inx]); Ancs(mother[inx]); } } BobHowe TimHowe KaySmith JimSmith MelSmith BevOlsen NanBlack 5. Trees. Answer the questions based on the tree to the right. [12 pts] a) Assume the root is at level 0. What is the level of node I? _3__ b) Which node is the root of the largest subtree that is a binary tree? __B__ c) If each node were limited to two children, how many nodes total could be stored in such a binary tree without adding any more levels? __31__ d) How many non-terminals are there in this tree? __7__ e) List the ancestors of node H _____A B D________ f) List the descendants of node F. ___I J K L M N O______ A / \ B C / \ \ D E F / \ /|\ G H I J K / \ |\ L M N O g) Draw the conversion of this general tree as a binary tree (left child as first child of the general tree and right child as a sibling in the general tree) CS 240 Computer Science II Exam 3 Page 3 6. Create a binary search tree from the following 10 characters entered into a BST left to right. [4 pts] LINKPAUSED List the data of your tree above from a inorder traversal. [4 pts] ADEIKLNPSU 8. Below is the method for pop in an array implementation of an Object stack. Fill in the code to complete the implementation. An instance variable top is part of the stack class and is initialized to -1. [10 pts] public __Object___ pop(){ if ( ____top == -1______ ) _throw__ new StackUnderflowException("Stack is empty"); _Object_ temp = stack [___top_____]; __top___ = ___top____ _-__ 1 ; return __temp___; } 9. More Unix concepts. Give the command to carry out the tasks described. [10 pts] Get a word and line count of all your java source files. _____wc *.java___________ Capture a long, detailed listing of your files stored in a file called directory __ls –l > directory_____ Search your java source files for the string LLNode ___grep LLNode *.java____________ You want to create an alias for a complex command. You store the alias command in the file__.bashrc__ To activate the changes in the file above, __source .bashrc_______ To run the java program bigoutput and control the output page by page __java bigoutput | more______ CS 240 Computer Science II Exam 3 Page 4 10. Answer the following questions about the refactoring of classes for image and sound media [14 pts] True/false: __T___ An object can be refered to by an LLNode variable. __T___ DrawableNode is a concrete class. __T___ CollectableNode contains methods common to SoundBranch and SoundNode and ScaleBranch. __T __ Branch extends its superclass to a tree structure. _F___ A SoundNode object’s call to getNext() would return a SoundNode object. getNext() is inherited from the class LLNode. __F___ This class hierarchy prevents us to mix sounds and images into a common linked list. Assume we three basic images s,t,u that we want to put into a scene graph: We want three instances of s drawn horizontally, 2 instances of t drawn vertically, one instance u and 2 more instances of s drawn vertically. Draw a tree of these sounds using the appropriate classes.