



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS230 Data Structures Handout # 26 Prof. Lyn Turbak Sunday, December 15, 2002 Wellesley College
Below are a number of purely optional extra credit problems. Each problem is marked with a number of points that will be awarded for a complete and correct solution. Partial credit will be awarded for problems that are partially complete and/or partially correct. In most problems, you are expected to write all code, including any testing code, from scratch. If you are interested in a problem but are having difficulty any details of its âfrom scratchâ nature (e.g., you donât know exactly how to start or how to get your code working with other code weâve been using in class) please contact Lyn. Solutions to any of these problems will be accepted through Monday, Dec. 23. To submit a problem, send Lyn an email message that contains (1) a writeup of the problem (i.e., the âhard- copyâ) and (2) indicates where the code for the problem lives (i.e., the âsoftcopyâ). Note that you can access all the departments Linux machines remotely, and so may work on these problems at home if you wish (and have the capability to work remotely).
Extra Credit 1 [25]: Cyclic Lists You saw in Problem 3 of Problem Set 6 that mutable lists can be used to create cyclic lists â lists in which later nodes point back to earlier nodes. In this problem, you will explore ways to create cyclic lists and tests if lists are cyclic. Assume that all methods are defined in a class named Cyclic.
a. [5]: Cyclify Write the following class method: public static void cyclify (ObjectMList L); Given a non-empty, non-cyclic mutable list L, modifies L so that the tail of the last node in L points to the first node in L. Test your method to show that it works as expected. (This is a bit tricky. A cyclic list is effectively infinite, so you canât just print out all the elements!)
b. [10]: isCyclic Write the following class method:
public static boolean isCyclic (ObjectMList L); Returns true if L is a cyclic list â i.e., if L contains some node whose tail points to itself or to a node earlier in the list. Otherwise, returns false.
Notes:
c. [10]: isCyclicTwoFinger A disadvantage of the isCyclic algorithm mentioned above is that it requires Î(n) space to maintain the collection of already-visited nodes. There is a constant-space means of testing if a given mutable list is cyclic known as the two-finger algorithm: imagine that two fingers traversing a list from the beginning, but one finger travels twice as fast as the other (i.e., one finger takes two tails for every tail taken by the other). A list is cylic if and only if the two fingers meet at the same node after the first step. Implement and test this idea via a method named isCyclicTwoFinger.
Extra Credit 2 [25]: Node Counting Consider the following size method for counting the number of nodes in an integer tree:
public static int size (IntTree t) { if (IT.isLeaf(t)) { return 0; } else { return 1 + (size (IT.left(T)))
d. [5] Due to possible sharing involving tree nodes, the size method can return a number that is larger than the actual number of distinct nodes in the tree. For example, consider the tree t constructed by the following code: IntTree t3 = IT.node(3, IT.leaf(), IT.leaf()); IntTree t2 = IT.node(2, IT.leaf(), t3); IntTree t1 = IT.node(1, t2, t3); The tree t1 contains only three distinct nodes. But what is the result returned by size(t1)?
e. [5] Design integer trees t5 and t7 that contain only three distinct nodes, but for which size(t5) returns 5 and size(t7) returns 7. Draw pictures of the trees and write the Java code that constructs them.
f. [15] Write a Java class method countNodes that takes an integer tree as an input and returns the number of distinct nodes in the tree. For example, countNodes should return 3 for the trees t1, tb, and tc described above. Hints: Ignore the values in the heads of the nodes. Instead concentrate on the nodes themselves. You can test the equality of two list nodes via ==.
Extra Credit 3 [25]: Breadth-First Numbering Write the following method:
public static IntTree bfnum (IntTree t); Returns an integer tree that has the same âshapeâ as t, but in which every node is numbered by the order in which it would be visited in a breadth-first traversal of t.
public static boolean isComplete (IntTree t); Returns true if t is complete and false otherwise.
Extra Credit 5 [20]: In-place Heap Sort At the bottom of p. 10 of Handout #24 is a brief section entitled Heapsort Revisited that describes an in-place version of heap sort for vectors â i.e., one that does not use any additional vector (or any other Î(n)-size) storage other than the vector supplied as an argument to the heap sort method. Based on this description and the other information on complete heaps in Handout #24, define a HeapSort class with the following single public method:
public static void heapSort (Comparator c, Vector v); Uses an in-place heap sort algorithm to sorts the elements of v from low to high according to the order specfied by c.
Notes:
Extra Credit 6 [40]: Lefist Heaps Handout #24 gives pictures and words explaining how to implement operations on leftist heaps, but does not give any Java code. In this problem, you will remedy this lack by defin- ing a MaxPQLeftistHeap class that inherits from CollectionImpl, implements Collection, and represents a max priority queue as a leftist heap.
Notes:
public static MaxPQLeftistHeap fromVector (Comparator c, Vector v); Uses the linear-time algorithm described on the last page of Handout #24 to build an instance of MaxPQLeftistHeap from the elements of vector v, using the comparator c to compare elements.
Extra Credit 7 [40]: 2-3 Trees Handout #28 gives words an pictures describing how to implement 2-3 trees, but does not give any Java code. In this problem, you will remedy this lack by defining an Int23Tree class that implements immutable 2-3-trees with integers as values. Your class should have the following public class methods:
public static Int23Tree leaf (); Returns an empty 2-3 tree.
public static Int23Tree insert (int x, Int23Tree t); Returns the 2-3 tree that results from inserting the integer x into t.
public static Int23Tree delete (int x, Int23Tree t); Returns the 2-3 tree that results from deleting the integer x from t. If x does not exist in t, returns a tree equivalent to t.
public static boolean isLeaf (Int23Tree t); Returns true if t is a leaf and false otherwise.
public static boolean isTwoNode (Int23Tree t); Returns true if t is a 2-node and false otherwise.
public static boolean value2 (Int23Tree t); If t is a 2-node, returns its value. Otherwise, throws an exception.
public static boolean left2 (Int23Tree t); If t is a 2-node, returns its left subtree. Otherwise, throws an exception.
public static boolean right2 (Int23Tree t); If t is a 2-node, returns its right subtree. Otherwise, throws an exception.
public static boolean isThreeNode (Int23Tree t); Returns true if t is a 3-node and false otherwise.
public static boolean leftValue3 (Int23Tree t); If t is a 3-node, returns its left value. Otherwise, throws an exception.
public static boolean rightValue3 (Int23Tree t); If t is a 3-node, returns its right value. Otherwise, throws an exception.
public static boolean left3 (Int23Tree t); If t is a 3-node, returns its left subtree. Otherwise, throws an exception.
public static boolean middle3 (Int23Tree t); If t is a 3-node, returns its middle subtree. Otherwise, throws an exception.
public static boolean right3 (Int23Tree t); If t is a 3-node, returns its right subtree. Otherwise, throws an exception.
public static int height (Int23Tree t); Returns the height of t, which is the length of all paths from the root of the tree to a leaf.
public static int size (Int23Tree t); Returns the number of values in t.