Docsity

26 Problems on Recursion and Binary Trees - Exam 3 | C SC 227, Exams of Computer Science

Toggle sidebar
bg1
C Sc 227 at the University of Arizona, Fall 2007
Answers to 26 Recursion and BinaryTeee Exercises
By Rick Mercer
1.
0
X<<<2
X<<4
X<6
2. 1 mystery(0) 4 mystery(1) 7 mystery(2) 10 mystery(3) 13 mystery(4)
3. stars(1) *
stars(2) *
**
stars(3) *
**
***
4. public void goingUp(int start, int end) {
if (start <= end) {
System.out.print(start + " ");
goingUp(start + 1, end);
}
}
5. public int power(int x, int n) {
if (n == 0)
return 1;
else
return x * power(x, n - 1);
}
6. private int f(int n) {
if (n <= 2)
return 1;
else
return f(n - 1) + f(n - 2);
}
7. private double addReciprocals(int n) {
if (n == 1)
return 1.0;
else
return 1.0 / n + addReciprocals(n - 1);
}
8. public int sum(int[] y) {
return sum(y, 0);
}
private int sum(int[] array, int index) {
if(index >= array.length)
return 0;
else
return array[index] + sum(array, index+1);
}
9. public void printArray(int[] x) {
printArray(x,0);
}
private void printArray(int[] x, int index) {
if(index < x.length)
{
print(x[index] + " ");
printArray(x,index+1);
1
pf3
pf4

Partial preview of the text

Download 26 Problems on Recursion and Binary Trees - Exam 3 | C SC 227 and more Exams Computer Science in PDF only on Docsity!

C Sc 227 at the University of Arizona, Fall 2007

Answers to 26 Recursion and BinaryTeee Exercises

By Rick Mercer

X<<<

X<<

X<

2.^1 mystery(0)^^4 mystery(1)^^7 mystery(2)^^10 mystery(3)^^13 mystery(4)

3. stars(1)^ *

stars(2) ***


stars(3) *** **


4. public^ void^ goingUp( int^ start,^ int^ end) {

if (start <= end) { System. out .print(start + " "); goingUp(start + 1, end); } }

5. public^ int^ power(int^ x,^ int^ n)^ {

if (n == 0) return 1; else return x * power(x, n - 1); }

6. private^ int^ f(int^ n)^ {

if (n <= 2) return 1; else return f(n - 1) + f(n - 2); }

7. private^ double^ addReciprocals(int n) {

if (n == 1) return 1.0; else return 1.0 / n + addReciprocals(n - 1); }

8. public^ int^ sum(int[]^ y)^ {

return sum(y, 0); } private int sum(int[] array, int index) { if(index >= array.length) return 0; else return array[index] + sum(array, index+1); }

9. public^ void^ printArray(int[]^ x)^ {

printArray(x,0); } private void printArray(int[] x, int index) { if(index < x.length) { print(x[index] + " "); printArray(x,index+1);

10.. public^ void^ reverse(int[]^ x)^ { reverse(x, 0, x.length-1); } private void reverse(int[] x, int left, int right) { if (left < right) { int temp = x[left]; x[left] = x[right]; x[right] = temp; reverse(x, left + 1, right - 1); } }

  1. public^ boolean^ isPalindrome(String s) { if(s.length() <= 1) //empty string is a palindrome return true; else { if( s.charAt[0] != s.charAt(s.length()-1)) return false; else return isPalindrome(s.substring(1,s.length()-1)); } }
  2. public^ int^ sumLinked()^ { return sum(first); } private int sum(Node n) { if (n == null) return 0; else return n.data + sum(n.next); }
  3. public^ void^ testOccurencesOf()^ { IntNode front = new IntNode(1); front = new IntNode(3, front); front = new IntNode(1, front); front = new IntNode(7, front); front = new IntNode(1, front); assertEquals (3, occurencesOf(front, 1)); }

private int occurencesOf(IntNode ref, int search) { if (ref == null ) return 0; else if (ref.data == search) return 1 + occurencesOf(ref.next, search); else return 0 + occurencesOf(ref.next, search); } ------or------- private int occurencesOf(IntNode ref, int search) { if (ref == null ) return 0; else { int found = 0; if (ref.data == search) found++; return found + occurencesOf(ref.next, search); } }

  1. F

/ \

A E

/ / \

return 1 + Math.max(height(t.left), height(t.right)); }

public int size() { return size(root); } private int size(TreeNode t) { if (t == null ) return 0; else return 1 + size(t.left) + size(t.right); }

  1. At depth answer not posted since it is a programming project problem.

You test it with your binarySearchTree

  1. No. There are two "G"s
  2. No (unless you reverse the ordering scheme)
  3. The main operations, insert, find, remove are O(log n). An unsorted array has O(n) runtimes for all three An sorted array has O(n) runtimes for two because find could be O(log n) if you sort the elements
  4. Matrix /
    Antz Shaft \ /
    Bean Scream Titanic