Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ObjectTree fromString Method and BagMBSTEntries Implementation, Assignments of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-mzo
koofers-user-mzo 🇺🇸

5

(1)

10 documents

1 / 14

Toggle sidebar

Related documents


Partial preview of the text

Download ObjectTree fromString Method and BagMBSTEntries Implementation and more Assignments Data Structures and Algorithms in PDF only on Docsity! CS230 Data Structures Handout # 19 Prof. Lyn Turbak Saturday, April 3, 2004 Wellesley College Problem Set 6 Due: 6pm Friday, April 9 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 PS6 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: 1. a team header sheet (see the end of this assignment for the header sheet) indicating the time that you (and your partner, if you are working with one) spent on the parts of the assignment; 2. For Problem 1, submit your final version of ObjectTreeParser.java. 3. For Problem 2, submit your final version of BagMBSTEntries.java and a testing transcript showing that your class works as expected 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/cs230 cp -R ps6 ~cs230/drop/ps6/username/ 1 Problem 1 [30]: Parsing Strings Into Trees Background It is easy to convert a binary tree into a string representation via the following toStringmethod: 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 (* #$?! *)) 2 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 • The skeleton for fromTokens can be found in the file ObjectTreeParser.java, which you should flesh out for this problem. • A contract for the ObjectTree class can be found in Appendix A. • In ObjectTreeParser.java, ObjectTree operations are accessible via the OT. prefix. • The following are helpful auxiliary methods with which you have been provided: public static void check (String expected, String actual) { if (! expected.equals(actual)) { throw new RuntimeException("ObjectTree.fromString: expected " + expected + " but got " + actual); } } 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. • This is definitely a problem in which spending time carefully working out your strategy on paper before jumping into coding will save you lots of time! The amount of code you have to write is rather small – fromTokens can be expressed in under 15 lines. However, you need to think carefully. The most important part of the solution strategy is wishful thinking – believing that when fromTokens is called recursively, it will correctly return the appropriate subtree after consuming all the tokens for that subtree. • You can test your code by invoking: 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. 5 Problem 2 [70]: Mutable BST of Bag Entries Implementation of Bags A bag is a collection of unordered elemements that may contain multiple occurrences of each element. In the CS230 collection hiearchy, the Bag interface describes mutable bags. You should study this interface (Appendix C) before proceeding with this problem. In this problem, you will implement a class BagMBSTBagEntries that represents bags as a mutable binary search tree of entries that pair elements in the bag with their number of occurrences. Each entry should be an instance of the following BagEntry class: public class BagEntry { public Object elt; public int num; public BagEntry (Object elt, int num) { this.elt = elt; this.num = num; } public String toString () { return "BagEntry[" + elt + "," + num + "]"; } } To improve the running time of the size() and count() bag operations, the values to be returned by these methods should be cached in instance variables of the BagMBSTEntries class. So instances of BagMBSTEntries should have the following instances variables: • comp: a comparator for determining the order of elements. • entries: a mutable binary search tree whose elements are instances of BagEntry. • size: the number of element occurrences currently in the bag (includes duplicates). • count: the number of distinct elements currently in the bag (does not include duplicates). For example, Fig. ?? shows one possible representation of an instance of BagMBSTEntries that contains two As, three Bs and one C. (The shape of the tree is determined by the order in which the elements were inserted.) 6 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. 7 Appendix A: ObjectTree Contract 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 * and a node N is represented by (L V R), where L is the string representation of the left subtree of N, V is the string representation of the value of N, and R is the string representation of the right subtree of N. For example, the tree created via: node(node(leaf(),"A",leaf()), "B", node(node(leaf(),"C",leaf()), "D", leaf())) has the following string representation: "((* A *) B ((* C *) D *))" 10 Appendix B: ObjectMTree Contract 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 * and a node N is represented by (L V R), where L is the string representation of the left subtree of N, V is the string representation of the value of N, and R is the string representation of the right subtree of N. For example, the tree created via: node(node(leaf(),"A",leaf()), "B", node(node(leaf(),"C",leaf()), "D", leaf())) has the following string representation: "((* A *) B ((* C *) D *))" 11 Appendix C: Bag Interface The Bag interface describes mutable collections of unordered elements that may contain multiple occurrences of each element. In mathematics, multiset is a synonym for bag. Each bag instance has a Comparator that is used to determine element equality (and may be used in bag implementations to order elements). Public Instance Methods Inherited from Collection Interface: public void insert (Object elt); Modifies this bag by inserting a new occurrence of elt. public Object deleteFirst (); Deletes and returns an arbitrary element of this bag. Throws a CollectionException if this bag is empty. The deleteFirst method is a synoynym for deleteOne. public Object first (); Returns an arbitrary element of this bag. Throws a CollectionException if this bag is empty. The first method is a synoynym for choose. public int size (); Returns the number of element occurrences in this bag. public boolean isEmpty (); Returns true if this bag has no elements, and false otherwise. public void clear (); Removes all elements from this bag. public Object clone (); Returns a ”shallow” copy of this bag – i.e. the copied bag structure is new, but elements themselves are shared with the old bag Operations on the copied bag do not affect the original bag and vice versa. Operations on mutable elements of the copied bag do affect elements of the original bag, and vice versa. public Enumeration elements (); Returns an enumeration of the element occurrences in this bag in an arbitrary order. public ObjectList toList (); Returns a list of the element occurrences in this bag in an arbitrary order. public String name (); Returns a name indicating the implementation of this bag. Public Instance Methods Inherited from ComparatorCollection Interface: public Comparator comparator (); Returns the comparator used by this bag to compare elements. public boolean delete (Object elt); Deletes one occurrence of elt from this bag. Returns true if elt was a member of the bag and false otherwise. 12
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved