Difference between revisions of "CSC212 Homework 6 2014"

From dftwiki3
Jump to: navigation, search
(Problem 3)
(Problem 3)
 
(11 intermediate revisions by the same user not shown)
Line 85: Line 85:
 
=Problem 3=
 
=Problem 3=
 
<br />
 
<br />
# Create a new class called StringBST.java.  This will be the program you submit.
+
<font color="magenta">(Updated 11/3/14 7:59 p.m.)</font>
# Instead of keeping track of a tree of ints, StringBST.java keeps track of a binary-search tree of Strings.  You may want to copy the contents of IntBST.java into StringBST.java and modify all the operations on the key to be string operations.
+
* Create a new class called StringBST.java.  This will be the program you submit.
:* Note 1:  when you compare two strings for equality, you must use the .equals() method.
+
* Instead of keeping track of a tree of ints, StringBST.java will maintain a binary-search tree of '''Strings'''.  You may want to copy the contents of IntBST.java into StringBST.java and modify all the operations on the keys to be '''string operations'''.
:* Note 2:  when you compare two strings for whether one is less than the other, use the String .compareTo() method.
+
:* '''Note 1''':  when you compare two strings for equality, you must use the '''.equals()''' method.
::
+
:* '''Note 2''':  when you compare two strings for whether one is less than the other, use the String '''.compareTo()''' method.
 +
 
 
<center>
 
<center>
{|  class="wikitable" width="100%"
+
{|  class="wikitable" width="60%"
 
! with ints
 
! with ints
 
! with Strings
 
! with Strings
Line 112: Line 113:
 
</source>
 
</source>
 
|-
 
|-
 +
|
 
<source lang="java">
 
<source lang="java">
 
int key, el;
 
int key, el;
Line 130: Line 132:
 
</center>
 
</center>
 
<br />
 
<br />
 +
* Your StringBST.java code must support several public methods that will be used by the test program:
 +
::* <font color="magenta">inOrderVisitDuplicates(): will '''print''' a list of all the words contained in the tree, one per line, along with the amount of replication for each word.</font>
 +
::* length(): will return '''an int''' that is the number of unique words in the tree.
 +
::* insertWithDuplicates(): this function works just like the '''insert()''' method, but it allows one to insert an element that is already in the tree.  You have to figure out how to keep track of duplicate keys.  We talked briefly about this in class, but because all we are interested in is the number of duplicated strings (see findMostFrequent() method below), there may be a much simpler solution than what we discussed in class...
 +
::* findMostFrequent(): will return the list (ArrayList<String>) of word(s) that was/were inserted by insertDuplicate() most often.
 +
<br />
 +
==Submission==
 +
<br />
 +
Submit your '''StringBST.java''' tree to Moodle, in the Homework 6 Problem 3 section.
 
<br />
 
<br />
 +
==Example Test Program==
 
<br />
 
<br />
 +
* Here is a test program called Hw6_3Test.java you can use to test your StringBST.java code with.  All you need to do is create a new class with Eclipse, in the same 212 project where StringBST.java resides, and the Hw6_3Test program will automatically have access to your StringBST.java.
 
<br />
 
<br />
 +
::<source lang="java">
 +
import java.io.File;
 +
import java.io.FileNotFoundException;
 +
import java.util.ArrayList;
 +
import java.util.Iterator;
 +
import java.util.Scanner;
 +
 +
 +
public class Hw6_3Test {
 +
 +
public static void main(String[] args) {
 +
// use a string with a good number of words...
 +
String text = "ULYSSES\n"
 +
  +"by James Joyce\n"
 +
  +"-- I --\n"
 +
  +"Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of\n"
 +
  +"lather on which a mirror and a razor lay crossed. A yellow dressinggown,\n"
 +
  +"ungirdled, was sustained gently behind him on the mild morning air. He\n"
 +
  +"held the bowl aloft and intoned:\n"
 +
  +"--_Introibo ad altare Dei_.\n"
 +
  +"Halted, he peered down the dark winding stairs and called out coarsely:\n"
 +
  +"--Come up, Kinch! Come up, you fearful jesuit!\n"
 +
  +"Solemnly he came forward and mounted the round gunrest. He faced about\n"
 +
  +"and blessed gravely thrice the tower, the surrounding land and the\n"
 +
  +"awaking mountains. Then, catching sight of Stephen Dedalus, he bent\n"
 +
  +"towards him and made rapid crosses in the air, gurgling in his throat\n"
 +
  +"and shaking his head. Stephen Dedalus, displeased and sleepy, leaned\n"
 +
  +"his arms on the top of the staircase and looked coldly at the shaking\n"
 +
  +"gurgling face that blessed him, equine in its length, and at the light\n"
 +
  +"untonsured hair, grained and hued like pale oak.\n"
 +
  +"Buck Mulligan peeped an instant under the mirror and then covered the\n"
 +
  +"bowl smartly.\n"
 +
  +"--Back to barracks! he said sternly.\n"
 +
  +"He added in a preacher's tone:\n"
 +
  +"--For this, O dearly beloved, is the genuine Christine: body and soul\n"
 +
  +"and blood and ouns. Slow music, please. Shut your eyes, gents. One\n"
 +
  +"moment. A little trouble about those white corpuscles. Silence, all.\n"
 +
  +"He peered sideways up and gave a long slow whistle of call, then paused\n"
 +
  +"awhile in rapt attention, his even white teeth glistening here and there\n"
 +
  +"with gold points. Chrysostomos. Two strong shrill whistles answered\n"
 +
  +"through the calm.\n"
 +
  +"--Thanks, old chap, he cried briskly. That will do nicely. Switch off\n"
 +
  +"the current, will you?\n"
 +
  +"He skipped off the gunrest and looked gravely at his watcher, gathering\n"
 +
  +"about his legs the loose folds of his gown. The plump shadowed face and\n"
 +
  +"sullen oval jowl recalled a prelate, patron of arts in the middle ages.\n"
 +
  +"A pleasant smile broke quietly over his lips.\n"
 +
  +"toto toto toto toto toto toto toto toto toto toto toto \n"
 +
  +"toto toto toto toto toto toto toto toto toto toto toto \n";
 +
 +
// split the string into lower case words, and strip all punctuation marks and white space.
 +
// This recipe is taken from StackOverflow
 +
// http://stackoverflow.com/questions/23332146/remove-punctuation-preserve-letters-and-white-space-java-regex
 +
String[] words = text.replaceAll("\\s+", " ").replaceAll("[^a-zA-Z\\s]", "").toLowerCase().split("\\s+");
 +
 +
// create a new tree
 +
StringBST tree = new StringBST();
 +
 +
// insert words and allow duplicates. 
 +
// int wordCounter = 0;
 +
for (int i = 0; i < words.length; i++) {
 +
tree.insertWithDuplicates( words[i] );
 +
// wordCounter++;
 +
}
 +
 +
// display the tree (for debugging purposes)
 +
tree.inOrderVisitDuplicates();
 +
//System.out.println( wordCounter + " words inserted" );
 +
 +
// display unique words
 +
System.out.println( tree.length() );
 +
 +
// display list of most frequent word(s)
 +
ArrayList<String> mostFreq = tree.findMostFrequent();
 +
if ( mostFreq.isEmpty() )
 +
System.out.println( "empty tree" );
 +
else {
 +
Iterator<String> it = mostFreq.iterator();
 +
while ( it.hasNext() )
 +
    System.out.print( it.next() + " " );
 +
System.out.println();
 +
}
 +
 +
}
 +
 +
}
 +
 +
</source>
 
<br />
 
<br />
 +
 +
==Output==
 
<br />
 
<br />
 +
 +
<source lang="text">
 +
a (9)
 +
about (3)
 +
ad (1)
 +
added (1)
 +
ages (1)
 +
air (2)
 +
all (1)
 +
aloft (1)
 +
altare (1)
 +
an (1)
 +
and (20)
 +
answered (1)
 +
arms (1)
 +
arts (1)
 +
at (3)
 +
attention (1)
 +
awaking (1)
 +
awhile (1)
 +
back (1)
 +
barracks (1)
 +
bearing (1)
 +
behind (1)
 +
beloved (1)
 +
bent (1)
 +
blessed (2)
 +
blood (1)
 +
body (1)
 +
bowl (3)
 +
briskly (1)
 +
broke (1)
 +
buck (2)
 +
by (1)
 +
call (1)
 +
called (1)
 +
calm (1)
 +
came (2)
 +
catching (1)
 +
chap (1)
 +
christine (1)
 +
chrysostomos (1)
 +
coarsely (1)
 +
coldly (1)
 +
come (2)
 +
corpuscles (1)
 +
covered (1)
 +
cried (1)
 +
crossed (1)
 +
crosses (1)
 +
current (1)
 +
dark (1)
 +
dearly (1)
 +
dedalus (2)
 +
dei (1)
 +
displeased (1)
 +
do (1)
 +
down (1)
 +
dressinggown (1)
 +
equine (1)
 +
even (1)
 +
eyes (1)
 +
face (2)
 +
faced (1)
 +
fearful (1)
 +
folds (1)
 +
for (1)
 +
forward (1)
 +
from (1)
 +
gathering (1)
 +
gave (1)
 +
gently (1)
 +
gents (1)
 +
genuine (1)
 +
glistening (1)
 +
gold (1)
 +
gown (1)
 +
grained (1)
 +
gravely (2)
 +
gunrest (2)
 +
gurgling (2)
 +
hair (1)
 +
halted (1)
 +
he (10)
 +
head (1)
 +
held (1)
 +
here (1)
 +
him (3)
 +
his (8)
 +
hued (1)
 +
i (1)
 +
in (6)
 +
instant (1)
 +
intoned (1)
 +
introibo (1)
 +
is (1)
 +
its (1)
 +
james (1)
 +
jesuit (1)
 +
jowl (1)
 +
joyce (1)
 +
kinch (1)
 +
land (1)
 +
lather (1)
 +
lay (1)
 +
leaned (1)
 +
legs (1)
 +
length (1)
 +
light (1)
 +
like (1)
 +
lips (1)
 +
little (1)
 +
long (1)
 +
looked (2)
 +
loose (1)
 +
made (1)
 +
middle (1)
 +
mild (1)
 +
mirror (2)
 +
moment (1)
 +
morning (1)
 +
mountains (1)
 +
mounted (1)
 +
mulligan (2)
 +
music (1)
 +
nicely (1)
 +
o (1)
 +
oak (1)
 +
of (6)
 +
off (2)
 +
old (1)
 +
on (3)
 +
one (1)
 +
ouns (1)
 +
out (1)
 +
oval (1)
 +
over (1)
 +
pale (1)
 +
patron (1)
 +
paused (1)
 +
peeped (1)
 +
peered (2)
 +
pleasant (1)
 +
please (1)
 +
plump (2)
 +
points (1)
 +
preachers (1)
 +
prelate (1)
 +
quietly (1)
 +
rapid (1)
 +
rapt (1)
 +
razor (1)
 +
recalled (1)
 +
round (1)
 +
said (1)
 +
shadowed (1)
 +
shaking (2)
 +
shrill (1)
 +
shut (1)
 +
sideways (1)
 +
sight (1)
 +
silence (1)
 +
skipped (1)
 +
sleepy (1)
 +
slow (2)
 +
smartly (1)
 +
smile (1)
 +
solemnly (1)
 +
soul (1)
 +
staircase (1)
 +
stairhead (1)
 +
stairs (1)
 +
stately (1)
 +
stephen (2)
 +
sternly (1)
 +
strong (1)
 +
sullen (1)
 +
surrounding (1)
 +
sustained (1)
 +
switch (1)
 +
teeth (1)
 +
thanks (1)
 +
that (2)
 +
the (22)
 +
then (3)
 +
there (1)
 +
this (1)
 +
those (1)
 +
thrice (1)
 +
throat (1)
 +
through (1)
 +
to (1)
 +
tone (1)
 +
top (1)
 +
toto (22)
 +
towards (1)
 +
tower (1)
 +
trouble (1)
 +
two (1)
 +
ulysses (1)
 +
under (1)
 +
ungirdled (1)
 +
untonsured (1)
 +
up (3)
 +
was (1)
 +
watcher (1)
 +
which (1)
 +
whistle (1)
 +
whistles (1)
 +
white (2)
 +
will (2)
 +
winding (1)
 +
with (1)
 +
yellow (1)
 +
you (2)
 +
your (1)
 +
 +
214
 +
the toto
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 19:51, 3 November 2014

--D. Thiebaut (talk) 12:17, 30 October 2014 (EDT)







This assignment is due Friday Nov 7, 2014, at 11:55 p.m.



Problem #1


  • Add a new method to your BST program that will output the DOT version of the tree it contains. Refer to Lab 10 for how we generate the DOT version of a tree.
  • Your function must be called generateDot()
  • It must be public so that my test program can access and use it
  • It doesn't need any parameters passed to it.
  • It outputs your name as a label.
  • Here is an example of the type of output it will generate for a given tree, for User Mickey Mouse:


digraph T {
label = "Mickey Mouse";
8 -> 3;
8 -> 10;
3 -> 1;
3 -> 6;
6 -> 4;
6 -> 7;
10 -> 14;
14 -> 13;
}


Submission to Moodle


  • Submit IntBST.java to Moodle, Problem 1 of Homework 6.


Problem #2


  • The tree generated in the first problem is not balanced well when the number of children is 1.
  • Modify your generateDot() function so that it outputs "invisible nodes" for null children.
  • Here is an example of the type of output to expect from your function for the same tree:


digraph T {
label = "Mickey Mouse";
8 -> 3;
8 -> 10;
3 -> 1;
3 -> 6;
null1left [label="",width=.1,style=invis];
1 -> null1left [style=invis];
null1right [label="",width=.1,style=invis];
1 -> null1right [style=invis];
6 -> 4;
6 -> 7;
null4left [label="",width=.1,style=invis];
4 -> null4left [style=invis];
null4right [label="",width=.1,style=invis];
4 -> null4right [style=invis];
null7left [label="",width=.1,style=invis];
7 -> null7left [style=invis];
null7right [label="",width=.1,style=invis];
7 -> null7right [style=invis];
null10left [label="",width=.1,style=invis];
10 -> null10left [style=invis];
10 -> 14;
14 -> 13;
null14right [label="",width=.1,style=invis];
14 -> null14right [style=invis];
null13left [label="",width=.1,style=invis];
13 -> null13left [style=invis];
null13right [label="",width=.1,style=invis];
13 -> null13right [style=invis];
}


Submission


  • Submit your IntBST.java program to Moodle, Problem 2 of Homework 6 section.


Problem 3


(Updated 11/3/14 7:59 p.m.)

  • Create a new class called StringBST.java. This will be the program you submit.
  • Instead of keeping track of a tree of ints, StringBST.java will maintain a binary-search tree of Strings. You may want to copy the contents of IntBST.java into StringBST.java and modify all the operations on the keys to be string operations.
  • Note 1: when you compare two strings for equality, you must use the .equals() method.
  • Note 2: when you compare two strings for whether one is less than the other, use the String .compareTo() method.
with ints with Strings
int key, el;
...
if ( el == key ){
   ...
}
String key, el;
...
if ( el.equals( key ) ){
   ...
}
int key, el;
...
if ( el < key ){
   ...
}
String key, el;
...
if ( el.compareTo( key ) < 0 ){
   ...
}


  • Your StringBST.java code must support several public methods that will be used by the test program:
  • inOrderVisitDuplicates(): will print a list of all the words contained in the tree, one per line, along with the amount of replication for each word.
  • length(): will return an int that is the number of unique words in the tree.
  • insertWithDuplicates(): this function works just like the insert() method, but it allows one to insert an element that is already in the tree. You have to figure out how to keep track of duplicate keys. We talked briefly about this in class, but because all we are interested in is the number of duplicated strings (see findMostFrequent() method below), there may be a much simpler solution than what we discussed in class...
  • findMostFrequent(): will return the list (ArrayList<String>) of word(s) that was/were inserted by insertDuplicate() most often.


Submission


Submit your StringBST.java tree to Moodle, in the Homework 6 Problem 3 section.

Example Test Program


  • Here is a test program called Hw6_3Test.java you can use to test your StringBST.java code with. All you need to do is create a new class with Eclipse, in the same 212 project where StringBST.java resides, and the Hw6_3Test program will automatically have access to your StringBST.java.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;


public class Hw6_3Test {

	public static void main(String[] args) {
		// use a string with a good number of words...
		String text = "ULYSSES\n"
				   +"by James Joyce\n"
				   +"-- I --\n"
				   +"Stately, plump Buck Mulligan came from the stairhead, bearing a bowl of\n"
				   +"lather on which a mirror and a razor lay crossed. A yellow dressinggown,\n"
				   +"ungirdled, was sustained gently behind him on the mild morning air. He\n"
				   +"held the bowl aloft and intoned:\n"
				   +"--_Introibo ad altare Dei_.\n"
				   +"Halted, he peered down the dark winding stairs and called out coarsely:\n"
				   +"--Come up, Kinch! Come up, you fearful jesuit!\n"
				   +"Solemnly he came forward and mounted the round gunrest. He faced about\n"
				   +"and blessed gravely thrice the tower, the surrounding land and the\n"
				   +"awaking mountains. Then, catching sight of Stephen Dedalus, he bent\n"
				   +"towards him and made rapid crosses in the air, gurgling in his throat\n"
				   +"and shaking his head. Stephen Dedalus, displeased and sleepy, leaned\n"
				   +"his arms on the top of the staircase and looked coldly at the shaking\n"
				   +"gurgling face that blessed him, equine in its length, and at the light\n"
				   +"untonsured hair, grained and hued like pale oak.\n"
				   +"Buck Mulligan peeped an instant under the mirror and then covered the\n"
				   +"bowl smartly.\n"
				   +"--Back to barracks! he said sternly.\n"
				   +"He added in a preacher's tone:\n"
				   +"--For this, O dearly beloved, is the genuine Christine: body and soul\n"
				   +"and blood and ouns. Slow music, please. Shut your eyes, gents. One\n"
				   +"moment. A little trouble about those white corpuscles. Silence, all.\n"
				   +"He peered sideways up and gave a long slow whistle of call, then paused\n"
				   +"awhile in rapt attention, his even white teeth glistening here and there\n"
				   +"with gold points. Chrysostomos. Two strong shrill whistles answered\n"
				   +"through the calm.\n"
				   +"--Thanks, old chap, he cried briskly. That will do nicely. Switch off\n"
				   +"the current, will you?\n"
				   +"He skipped off the gunrest and looked gravely at his watcher, gathering\n"
				   +"about his legs the loose folds of his gown. The plump shadowed face and\n"
				   +"sullen oval jowl recalled a prelate, patron of arts in the middle ages.\n"
				   +"A pleasant smile broke quietly over his lips.\n"
				   +"toto toto toto toto toto toto toto toto toto toto toto \n"
				   +"toto toto toto toto toto toto toto toto toto toto toto \n";
		
		// split the string into lower case words, and strip all punctuation marks and white space.
		// This recipe is taken from StackOverflow
		// http://stackoverflow.com/questions/23332146/remove-punctuation-preserve-letters-and-white-space-java-regex
		String[] words = text.replaceAll("\\s+", " ").replaceAll("[^a-zA-Z\\s]", "").toLowerCase().split("\\s+");
		
		// create a new tree
		StringBST tree = new StringBST();
		
		// insert words and allow duplicates.  
		// int wordCounter = 0;
		for (int i = 0; i < words.length; i++) {
			tree.insertWithDuplicates( words[i] );
			// wordCounter++;
		}
		
		// display the tree (for debugging purposes)
		tree.inOrderVisitDuplicates();
		//System.out.println( wordCounter + " words inserted" );
		
		// display unique words
		System.out.println( tree.length() );
		
		// display list of most frequent word(s)
		ArrayList<String> mostFreq = tree.findMostFrequent();
		if ( mostFreq.isEmpty() )
			System.out.println( "empty tree" );
		else {
			Iterator<String> it = mostFreq.iterator();
			while ( it.hasNext() ) 
			    System.out.print( it.next() + " " );
			System.out.println();
		}
	
	}

}


Output


a (9) 
about (3) 
ad (1) 
added (1) 
ages (1) 
air (2) 
all (1) 
aloft (1) 
altare (1) 
an (1) 
and (20) 
answered (1) 
arms (1) 
arts (1) 
at (3) 
attention (1) 
awaking (1) 
awhile (1) 
back (1) 
barracks (1) 
bearing (1) 
behind (1) 
beloved (1) 
bent (1) 
blessed (2) 
blood (1) 
body (1) 
bowl (3) 
briskly (1) 
broke (1) 
buck (2) 
by (1) 
call (1) 
called (1) 
calm (1) 
came (2) 
catching (1) 
chap (1) 
christine (1) 
chrysostomos (1) 
coarsely (1) 
coldly (1) 
come (2) 
corpuscles (1) 
covered (1) 
cried (1) 
crossed (1) 
crosses (1) 
current (1) 
dark (1) 
dearly (1) 
dedalus (2) 
dei (1) 
displeased (1) 
do (1) 
down (1) 
dressinggown (1) 
equine (1) 
even (1) 
eyes (1) 
face (2) 
faced (1) 
fearful (1) 
folds (1) 
for (1) 
forward (1) 
from (1) 
gathering (1) 
gave (1) 
gently (1) 
gents (1) 
genuine (1) 
glistening (1) 
gold (1) 
gown (1) 
grained (1) 
gravely (2) 
gunrest (2) 
gurgling (2) 
hair (1) 
halted (1) 
he (10) 
head (1) 
held (1) 
here (1) 
him (3) 
his (8) 
hued (1) 
i (1) 
in (6) 
instant (1) 
intoned (1) 
introibo (1) 
is (1) 
its (1) 
james (1) 
jesuit (1) 
jowl (1) 
joyce (1) 
kinch (1) 
land (1) 
lather (1) 
lay (1) 
leaned (1) 
legs (1) 
length (1) 
light (1) 
like (1) 
lips (1) 
little (1) 
long (1) 
looked (2) 
loose (1) 
made (1) 
middle (1) 
mild (1) 
mirror (2) 
moment (1) 
morning (1) 
mountains (1) 
mounted (1) 
mulligan (2) 
music (1) 
nicely (1) 
o (1) 
oak (1) 
of (6) 
off (2) 
old (1) 
on (3) 
one (1) 
ouns (1) 
out (1) 
oval (1) 
over (1) 
pale (1) 
patron (1) 
paused (1) 
peeped (1) 
peered (2) 
pleasant (1) 
please (1) 
plump (2) 
points (1) 
preachers (1) 
prelate (1) 
quietly (1) 
rapid (1) 
rapt (1) 
razor (1) 
recalled (1) 
round (1) 
said (1) 
shadowed (1) 
shaking (2) 
shrill (1) 
shut (1) 
sideways (1) 
sight (1) 
silence (1) 
skipped (1) 
sleepy (1) 
slow (2) 
smartly (1) 
smile (1) 
solemnly (1) 
soul (1) 
staircase (1) 
stairhead (1) 
stairs (1) 
stately (1) 
stephen (2) 
sternly (1) 
strong (1) 
sullen (1) 
surrounding (1) 
sustained (1) 
switch (1) 
teeth (1) 
thanks (1) 
that (2) 
the (22) 
then (3) 
there (1) 
this (1) 
those (1) 
thrice (1) 
throat (1) 
through (1) 
to (1) 
tone (1) 
top (1) 
toto (22) 
towards (1) 
tower (1) 
trouble (1) 
two (1) 
ulysses (1) 
under (1) 
ungirdled (1) 
untonsured (1) 
up (3) 
was (1) 
watcher (1) 
which (1) 
whistle (1) 
whistles (1) 
white (2) 
will (2) 
winding (1) 
with (1) 
yellow (1) 
you (2) 
your (1) 

214
the toto