Tail Recursive, Non Tail Recursive and Iterative Version - Assignment | CS 220, Assignments of Computer Science

Material Type: Assignment; Professor: Carver; Class: Programming w/Data Structures; Subject: Computer Science; University: Southern Illinois University Carbondale; Term: Fall 2009;

Typology: Assignments

Pre 2010

Uploaded on 02/24/2010

koofers-user-5f7
koofers-user-5f7 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 220 Programming with Data Structures Fall 2009
Solutions to Homework #3
1. Text Chapter 10, Exercise No. 11:
(A) Tail recursive version:
public int countNodes(Node<T> start, int count)
{
if (start == null)
//Base case:
return count;
else
//Recursive case:
return countNodes(start.next, count + 1);
}
(B) Non-tail recursive version:
public int countNodes(Node<T> start)
{
if (start == null)
//Base case:
return 0;
else
//Recursive case:
return 1 + countNodes(start.next);
(C) Iterative version:
public int countNodes(Node<T> start)
{
int count = 0;
Node<T> currNode = start;
while (currNode != null) {
count++;
currNode = currNode.next; }
return count;
}
2. Text Chapter 10, Exercise No. 16:
public static int minimum(int[] data)
{
return minimumHelper(data, 0, data.length - 1);
}
pf3
pf4
pf5

Partial preview of the text

Download Tail Recursive, Non Tail Recursive and Iterative Version - Assignment | CS 220 and more Assignments Computer Science in PDF only on Docsity!

CS 220 – Programming with Data Structures – Fall 2009

Solutions to Homework

  1. Text Chapter 10, Exercise No. 11: (A) Tail recursive version:

public int countNodes(Node start, int count) { if (start == null) //Base case: return count; else //Recursive case: return countNodes(start.next, count + 1); }

(B) Non-tail recursive version:

public int countNodes(Node start) { if (start == null) //Base case: return 0; else //Recursive case: return 1 + countNodes(start.next);

(C) Iterative version:

public int countNodes(Node start) { int count = 0; Node currNode = start; while (currNode != null) { count++; currNode = currNode.next; } return count; }

  1. Text Chapter 10, Exercise No. 16:

public static int minimum(int[] data) { return minimumHelper(data, 0, data.length - 1); }

public static int minimumHelper(int[] data, int first, int last) { if (first == last) //Base case, single element arrray: return data[first]; else { //Recursive case, array with more than one element: //(return minimum in each half) int middle = (first + last) / 2; return Math.min(minimum(data, first, middle),minimum(data, middle + 1, last)); } }

  1. Text Chapter 10, Exercise No. 17:

(a) (b) (c) f(4): return f(2) + f(1); f(8): f(8): return f(4) + f(2); return f(4) + f(2); f(16): f(16): f(16): return f(8) + f(4); return f(8) + f(4); return f(8) + f(4);

(d) (e) (f) f(2): f(1): return 1; return 1; f(4): f(4): f(4): return f(2) + f(1); return 1 + f(1); return 1 + f(1); f(8): f(8): f(8): return f(4) + f(2); return f(4) + f(2); return f(4) + f(2); f(16): f(16): f(16): return f(8) + f(4); return f(8) + f(4); return f(8) + f(4);

(g) (h) (i) f(4): f(2): return 1 + 1; return 1; f(8): f(8): f(8): return f(4) + f(2); return 2 + f(2); return 2 + f(2); f(16): f(16): f(16): return f(8) + f(4); return f(8) + f(4); return f(8) + f(4);

  1. Text Chapter 21, Exercise No. 11. Iterative version:

public void printStack(LinkedStack stack) LinkedStack extraStack = new LinkedStack(); while (!stack.isEmpty()) { T item = stack.pop(); extraStack.push(item); } while (!extraStack.isEmpty()) { T item = extraStack.pop(); System.out.println(item); stack.push(item); }

Recursive version:x

public LinkedStack printStack(LinkedStack stack) { if(stack.isEmpty()) { //Base case: return stack; } else { //Recursive case: T item = stack.pop(); printStack(stack); System.out.println(item); stack.push(item); return stack; } }

  1. Text Chapter 22, Exercise No. 2: Have each node keep a reference to the previous node and either keep a reference to the tail of the list or link the nodes in a circular fashion. If we use a tail reference, push, pop and peek are implemented, as follows: - To push a node, use the tail reference to find the end of the chain and then link in the new node. Afterwards, move the tail reference. - To peek, use the tail reference to find the last node. Return that nodes data value, since it is the stacks top. - To pop, use the tail reference to find the last node. Using the previous reference in that node, remove the node from the chain, get its data value (the top), update the tail reference, and return the top.

For a circularly linked implementation, the previous reference in the rst node will serve as the tail reference, so we can implement the operations as just described.

  1. Text Chapter 23, Exercise No. 3: Initially, myQueue is empty. The following statements affect the queue as shown:

myQueue.enqueue("Jane"); Jane myQueue.enqueue("Jess"); Jane Jess myQueue.enqueue("Jill"); Jane Jess Jill myQueue.enqueue(myQueue.dequeue()); Jess Jill Jane myQueue.enqueue(myQueue.getFront()); Jess Jill Jane Jess myQueue.enqueue("Jim"); Jess Jill Jane Jess Jim String name = myQueue.dequeue(); Jill Jane Jess Jim myQueue.enqueue(myQueue.getFront()); Jill Jane Jess Jim Jill

  1. Text Chapter 23, Exercise No. 4: Initially, myDeque is empty. The following statements affect it as shown:

myDeque.addToFront("Jim"); Jim myDeque.addToFront("Jess"); Jess Jim myDeque.addToBack("Jill"); Jess Jim Jill myDeque.addToBack("Jane"); Jess Jim Jill Jane String name = myDeque.removeFront(); Jim Jill Jane myDeque.addToBack(name); Jim Jill Jane Jess myDeque.addToBack(myDeque.getFront()); Jim Jill Jane Jess Jim myDeque.addToFront(myDeque.removeBack()); Jim Jim Jill Jane Jess myDeque.addToFront(myDeque.getBack()); Jess Jim Jim Jill Jane Jess

  1. Text Chapter 23, Exercise No. 5: Initially, myPriorityQueue is empty. The following statements affect the priority queue as shown, assuming that priority is interpreted as coming rst in a lexicographical ordering (dictionary order):

myPriorityQueue.add("Jim"); Jim myPriorityQueue.add("Jess"); Jess Jim myPriorityQueue.add("Jill"); Jess Jill Jim myPriorityQueue.add("Jane"); Jane Jess Jill Jim String name = myPriorityQueue.remove(); Jess Jill Jim myPriorityQueue.add(name); Jane Jess Jill Jim myPriorityQueue.add(myPriorityQueue.peek()); Jane Jane Jess Jill Jim myPriorityQueue.add("Jim"); Jane Jane Jess Jill Jim Jim myPriorityQueue.remove(); Jane Jess Jill Jim Jim

  1. Text Chapter 24, Exercise No. 4:

public void splice(ArrayQueue anotherQueue) { int anotherQueueCapacity = anotherQueue.queue.length; int count = (anotherQueue.backIndex + anotherQueue.queue.length - anotherQueue.frontIndex + 1) % anotherQueueCapacity; for (int i = 0; i < count; i++) { int location = (anotherQueue.frontIndex + i) % anotherQueueCapacity; T item = anotherQueue.queue[location]; enqueue(item); } }

  1. Text Chapter 24, Exercise No. 5:

public void splice(LinkedQueue anotherQueue) { Node scout = anotherQueue.firstNode; while (scout != null) { T item = scout.getData(); enqueue(item); scout = scout.getNextNode(); } }

  1. Text Chapter 25, Exercise No. 6:

(a) Preorder traversal: 6 4 2 1 3 5 8 7 9 10 11 (b) Postorder traversal: 1 3 2 5 4 9 7 11 10 8 6 (c) Inorder traversal: 1 2 3 4 5 6 7 9 8 10 11 (d) Level order traversal: 6 4 8 2 5 7 10 1 3 9 11