Sample solutions problems on the final exam study guide Don't look at these until you've tried the problems first. Given : class node { int head; node left, right; public node(int h, node l, node r) {head=h; left=l; right=r;} ... 1. Write a function that returns the smallest integer in a tree (not binary search tree). Since it's not a search tree, we have to use recursion to look in both the left and right branch. The answer is the smallest value among the head, the smallest on the left subtree and the smallest on the right subtree. public int smallest() { int answer = head; int l=answer; int r=answer; if (left!=null) l = left.smallest(); if (l { T head; node left, right; ... } size: find the number of nodes in a tree. Natural (never looks at head) depth: find the length of the longest branch. natural Natural (never looks at head) max : find the largest element in a general binary tree. Requires the meaning of ">" to be abstracted smax : find the largest element in binary SEARCH tree (think!). Natural. The largest element in a search tree is always on the extreme right node (analogous to the smallest function above). Therefore, there's again no need to compare with the head of any node. 9. A tree is called "normal" if every node is either null or has both non-null left and right branches. In other words, either both left and right are null or neither are null. Write a procedure that checks for this property. public boolean normal() { if (left==null && right==null) return true; if (left==null && right!=null) return false; if (left!==null && right==null) return false; return left.normal() && right.normal(); } Is this function naturally polymorphic? Yes. The head is clearly never examined. 10. The binary search algorithm works well with a sorted array, and with binary search trees. Explain why it doesn't work with sorted linked lists. A critical ability of binary search is to jump to the middle element immediately. In an array, this is just the middle index: A.length/2. In a roughly-balanced binary search tree, the root node can be used for the middle node. In a sorted list, we will have to traverse the tail pointers through half the list just to get to the middle node. This operation already takes time linearly proportional - O(n) - to the length of the list,and thus the binary search procedure cannot possbily be O(log n). (more may be added later ...) 11. Consider the polymorphic cell class class cell { A head; cell tail; cell(A h, cell t) {head=h; tail=t; } ... } 11a. Write code to use this class to construct a list 2 Integers. cell M = new cell(6,null)); 11b. Write a procedure within the class to find and return the middle cell of the list (this is naturally polymorphic because you never have to consider the head). That is, if there are 5 elements, return the 3rd element. If there are 8 elements, return the 5th element (the one slightly to the right of middle). Naturally you'll need to find the length first. public cell middle() { int len = 0; cell i = this; while (i!=null) {len++; i=i.tail;} // find length i = this; // reset to start for(int j=0;j