








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for implementing the fromstring method of the objecttree class, which converts a string representation of an object tree into an objecttree instance. Additionally, it outlines the methods that need to be implemented for the bagmbstentries class, which represents bags as a mutable binary search tree of entries. Helpful auxiliary methods and the contract for the objecttree and objectmtree classes.
Typology: Assignments
1 / 14
This page cannot be seen from the preview
Don't miss anything!









CS230 Data Structures Handout # 19 Prof. Lyn Turbak Saturday, April 3, 2004 Wellesley College
Exam 2 Notice: In class on Friday, April 9, the second take-home exam will be handed out. It will be due at midnight on Friday April 16. This is a hard deadline. No extensions will be given after this time. The exam will cover the material in lecture through Lecture 16 (Fri. April 2) and the material in problem sets through PS6, mainly focusing on material introduced since the last exam: binary trees and contracts and implementations of stacks, queues, priority queues, sets, bags, and tables. Because you should focus on the exam, it is strongly recommended that you submit PS on time (6pm Friday April 9), although you may use lateness coupons to turn in PS6 late. But since the exam is worth much more than the problem set, it might be best to cut your losses on PS6 if you are not done with it so that you can focus on the exam.
Overview: The purpose of this assignment is to give you practice with manipulating trees and implementing collections via trees.
Working Together: If you worked with a partner on a previous problem set and want to work with a partner on this assignment, you are encourage to choose a different partner. However, you may also work with someone you worked with in the first half of the semester.
Submission: Each team should turn in a single hardcopy submission packet for all problems by slipping it under Lyn’s office door by 6pm on the due date. The packet should include:
Keep in mind that your final project proposal is also due on Friday, April 9 (see Handout #20). This should be submitted separately. Each team should also submit a single softcopy (consisting of your final ps6 directory) to the drop directory ~cs230/drop/ps6/username, where username is the username of one of the team members (indicate which drop folder you used on your hardcopy header sheet). To do this, execute the following commands in Linux in the account of the team member being used to store the code.
cd /students/username/cs cp -R ps6 ~cs230/drop/ps6/username/
Problem 1 [30]: Parsing Strings Into Trees
Background It is easy to convert a binary tree into a string representation via the following toString method:
public String toString (ObjectTree t) { if (OT.isLeaf(t)) { return "*"; } else { return "(" + OT.left(t) + " " + OT.value(t) + " " + OT.right(t) + ")"; } }
Indeed, such a toString method is included in the contracts for ObjectTree (Appendix A) and ObjectMTree (Appendix B). In this problem you will define a fromString method with the following method header that inverts this process:
public static ObjectTree fromString (String s);
fromString converts a string representation s of an object tree into an ObjectTree instance t where every node has a string value and where t.toString() is equal to s (modulo possible differences in whitespace). This process is called ”parsing” the string into a tree. If s is a well-formed tree representation for an object tree, then fromString returns an appropriate instance of ObjectTree. However, if the string parameter to fromString is ill-formed, then fromString should throw an exception indicating this fact. For example, here are the results of fromString on some sample input strings. First are some examples of well-formed trees:
fromString("*") =
fromString("(* A )") = ( A *)
fromString("((* A ) B ( C ))") = (( A ) B ( C *))
fromString("(((* This ) is (( an ) example )) of ( an ( ObjectTree! )))") = ((( This ) is (( an ) example )) of ( an ( ObjectTree! *)))
Fig. ?? shows some examples involving strings that are not interpretable as trees. In these cases, the exceptions thrown by fromString have been caught by the testing program and an error message has been printed out. A parsing process is usually decomposed into two separate passes. The first pass, known as scanning or tokenizing, breaks the input string up into so-called tokens that represent the prim- itive units being parsed. In the case of tree parsing, the tokens are an open parenthesis "(", a close parenthesis ")", and any contiguous sequence of non-whitespace characters, such as "*", "253", "foo", and "$a-b_c!#?73". For example, the string
((* foo ) 230 ( #$?! *))
consists of the following tokens:
"(", "(", "", "foo", "", ")", "230", "(", "", "#$?!", "", ")", ")"
Whitespace is ignore in the tokenizing process, so "(* A *)" has exactly the same tokens as
"(* A
)" The second pass, known as parsing builds a tree from the token sequence according to rules for tree formation. In the case of ObjectTree, the rules are:
The first pass of splitting the string into tokens is tricky, so this process has already been encapsulated for you in the StringTreeTokens class. The class method invocation
new StringTreeTokens(string )
creates an enumeration whose elements are the tokens in string for the tree parsing process. If there are no more tokens, an instance of StringTreeTokens will respond to nextElement() by throwing a RuntimeException. The second pass is captured by a fromTokens method that takes a stream of tokens (represented as an instance of StringTreeTokens) and consumes all the tokens that it needs to to form a tree. It leaves behind any tokens not consumed by this process. In particular, when parsing an object tree:
Here is the definition of fromString based on this approach:
public static ObjectTree fromString (String s) { // Parses a string into an integer tree. // Either an ObjectTree, or throws an exception. Enumeration tokens = new StringTreeTokens(s); ObjectTree t = fromTokens(tokens); if (tokens.hasMoreElements()) { throw new RuntimeException("ObjectTree.fromString: superfluous token "
Your Task Your problem is to flesh out the fromTokens method:
public static ObjectTree fromTokens (Enumeration tokens); Parses the given enumeration of tokens into an object tree. Either returns an ObjectTree, or throws an exception indicating the tokens were not parsable into an ObjectTree.
Notes
public static void check (String expected, String actual) { if (! expected.equals(actual)) { throw new RuntimeException("ObjectTree.fromString: expected " + expected
public static String nextToken (Enumeration tokens) { try { return (String) tokens.nextElement(); } catch (RuntimeException e) { // no more tokens throw new RuntimeException("ObjectTree.fromString: too few tokens!"); } }
If you use the above auxiliary methods, then you should not have to explicitly throw any exception in your definition of fromString.
java ObjectTreeParser tree-strings.txt
This invokes fromString on each of the lines in the test file tree-strings.txt. Feel free to add additional test cases to tree-strings.txt.
BagMBSTEntries entries count 3 size 6 comp ... BagEntry elt B num 3
BagEntry elt A num 2
BagEntry elt C num 1
Figure 2: An example of a BagMBSTEntries instance.
To complete this problem, you need to flesh out the following methods of the bag implementation in BagMBSTEntries.java using the representation described above:
Constructor Methods
public BagMBSTEntries (Comparator c); public BagMBSTEntries ();
Instance Methods
public Object choose(); public void clear(); public Object clone(); public int count(); public boolean delete(Object x); public boolean deleteAll(Object x); public Object deleteOne(); public boolean isMember(Object x); public void insert(Object x); public ObjectList toList(); public int occurrences(Object x); public int size();
Note that many instance methods from the Bag interface in Appendix C are missing from the above list. This is because the BagMBSTEntries class inherits implementations of these other methods from its superclasses. Test your implementation by executing java BagMBSTEntries, which invokes the bag methods on various simple BagMBSTEntries instances. Study the output carefully to make sure that the methods behave as expected. You should turn in the transcript of this test for your final version of the code as part of your hardcopy submission.
Notes:
private FindEntryInfo findEntry (Object x); If a BagEntry for x exists in the entries tree, returns a FindEntryInfo fei where:
The ObjectMTree class models immutable trees whose nodes hold object values.
Public Class Methods:
public static ObjectTree leaf (); Returns a leaf – i.e., a distingushed non-value-bearing node that denotes an empty tree.
public static ObjectTree node (ObjectTree l, Object v, ObjectTree r); Returns a tree node whose left subtree is l, whose value is v, and whose right subtree is r.
public static boolean isLeaf (ObjectTree t); Returns true if t is a leaf and false otherwise (i.e., if t is a tree node).
public static Object value (ObjectTree t); If t is a node, returns the value it holds. If t is a leaf, throws a RuntimeException indicating that a leaf has no value.
public static ObjectTree left (ObjectTree t); If t is a node, returns its left subtree. If t is a leaf, throws a RuntimeException indicating that a leaf has no left subtree.
public static ObjectTree right (ObjectTree t); If t is a node, returns its right subtree. If t is a leaf, throws a RuntimeException indicating that a leaf has no right subtree.
Public Instance Methods:
public String toString (); Returns a string representation of this tree. In this representation, a leaf is represented by
The ObjectMTree class models mutable trees whose nodes hold object values.
Public Class Methods:
public static ObjectMTree leaf (); Returns a leaf – i.e., a distingushed non-value-bearing node that denotes an empty tree.
public static ObjectMTree node (ObjectMTree l, Object v, ObjectMTree r); Returns a tree node whose left subtree is l, whose value is v, and whose right subtree is r.
public static boolean isLeaf (ObjectMTree t); Returns true if t is a leaf and false otherwise (i.e., if t is a tree node).
public static Object value (ObjectMTree t); If t is a node, returns the value it holds. If t is a leaf, throws a RuntimeException indicating that a leaf has no value.
public static ObjectMTree left (ObjectMTree t); If t is a node, returns its left subtree. If t is a leaf, throws a RuntimeException indicating that a leaf has no left subtree.
public static ObjectMTree right (ObjectMTree t); If t is a node, returns its right subtree. If t is a leaf, throws a RuntimeException indicating that a leaf has no right subtree.
public static void setValue (ObjectMTree t, Object newValue); If t is a node, its value is changed to be newValue. If t is a leaf, throws a RuntimeException indicating that a leaf has no value.
public static void setLeft (ObjectMTree t, ObjectMTree newLeft); If t is a node, its left subtree is changed to be newLeft. If t is a leaf, throws a RuntimeException indicating that a leaf has no left subtree.
public static void setRight (ObjectMTree t, ObjectMTree newRight); If t is a node, its right subtree is changed to be newRight. If t is a leaf, throws a RuntimeException indicating that a leaf has no right subtree.
Public Instance Methods:
public String toString (); Returns a string representation of this tree. In this representation, a leaf is represented by
public boolean isMember (Object elt); Returns true if elt is a member of this bag and false otherwise.
public void union (Collection c); Modify this bag to contain the union of its elements with those of c. The union is the result of inserting each element of c into this bag. All comparison operations use the comparator of this bag.
public void intersection (Collection c); Modify this bag to contain the intersection of its elements with those of c. The intersection is the result of keeping in this bag only those elements that are also in c. The number of occurences of an element e in this bag after the intersection is the minimum of (1) the number of occurences of e in this bag before the intersection and (2) the number of occurences of e in c. All comparison operations use the comparator of this bag.
public void difference (Collection c); Modify this bag to contain the difference of its elements with those of c. The difference is the result of deleting each element of c from this bag. All comparison operations will use the comparator of this bag.
Public Instance Methods Inherited from UnorderedCollection Interface:
public Object choose (); Returns an arbitrary element of this bag. Throws a CollectionException if this bag is empty. The choose method is a synoynym for first.
public boolean deleteOne (); Deletes and returns an arbitrary element of this bag. Throws a CollectionException if this bag is empty. The deleteOne method is a synoynym for deleteFirst.
Public Instance Methods Added by Bag Interface:
public int count (); Returns the number of distinct elements in this bag (i.e., ignoring duplicates).
public boolean deleteAll (Object elt); Deletes all occurrences of elt in this bag. Returns true if this bag contained at least one occurrence of elt before deletion, and false otherwise.
public int occurrences (Object elt); Returns the number of occurrences of elt in this bag. Returns 0 if elt is not a member of this bag.
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you or your team spent on the parts of this problem set. Team members should be working closely together, so it will be assumed that the time reported is the time for each team member. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.