I'm getting some logical error in Huffman's encoding java - java

Here is the code...
public class Huffman_Coding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to compress: ");
String str = sc.nextLine();
sc.close();
HashString hs = new HashString();
HashMap<Character, Integer> hm = hs.getStringHash(str);
PriorityQueue<Node> pq = new PriorityQueue<Node>();
for (char ch : hm.keySet()) {
pq.add(new Node(null, null, hm.get(ch), ch));
}
System.out.println(pq);
while (pq.size() != 1) {
Node left = pq.poll();
Node right = pq.poll();
Node parent = new Node(left, right, left.freq + right.freq, '\0');
pq.add(parent);
System.out.println(pq);
}
Huffman_Tree ht = new Huffman_Tree();
String ans = "";
ht.inOrder(pq.poll(), ans);
}
}
class Node implements Comparable<Node> {
#Override
public String toString() {
return "Node [freq=" + freq + ", ch=" + ch + "]";
}
Node lptr;
Node rptr;
int freq;
char ch;
Node(Node lptr, Node rptr, int freq, char ch) {
this.freq = freq;
this.lptr = lptr;
this.rptr = rptr;
this.ch = ch;
}
public int compareTo(Node o) {
int comparedvalue = Integer.compare(this.freq, o.freq);
if (comparedvalue != 0)
return comparedvalue;
else
return Integer.compare(this.ch, o.ch);
}
}
boolean isLeaf() {
return this.lptr == null && this.rptr == null;
}
}
class Huffman_Tree {
void inOrder(Node root, String code) {
if (!root.isLeaf()) {
inOrder(root.lptr, code + '0');
inOrder(root.rptr, code + '1');
} else
System.out.println(root.ch + " : " + code);
}
}
Here, for input string abccddeeee,
I'm getting something like:
[Node [freq=1, ch=a], Node [freq=1, ch=b], Node [freq=2, ch=c], Node [freq=2, ch=d], Node [freq=4, ch=e]]
[Node [freq=2, ch= ]]
I'm confused why in the second step Node having 'd' is coming ahead from 'e'. This is getting me errors in final encoding. Why compareTo method is failing I cant understand.
getHashString returns a Hash which has characters in key and their freq in value.

I can't figure out why the order of the elements in PriorityQueue is not the expected one after polling elements ant adding new "synthetic" elements, but I think you can solve the problem switching to a TreeSet, as I have done with success with
TreeSet<Node> pq = new TreeSet<Node>((n1, n2) -> n1.compareTo(n2)); // explicit but unnecessary comparator
and changind each pq.poll() invocation into pq.pollFirst()...
I hope this workaround can help you!
EDIT
As you can read in official PriorityQueue documentation, * The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).*
This should explain why the toString() method invocation shows queue elements in an order different than the expected priority order...

Related

Radix(Trie) Tree implementation for Cutomer search in Java

I am working on a project and need to search in data of millions of customers. I want to implement radix(trie) search algorithm. I have read and implement radix for a simple string collections. But Here I have a collection of customers and want to search it by name or by mobile number.
Customer Class:
public class Customer {
String name;
String mobileNumer;
public Customer (String name, String phoneNumer) {
this.name = name;
this.mobileNumer = phoneNumer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumer() {
return mobileNumer;
}
public void setPhoneNumer(String phoneNumer) {
this.mobileNumer = phoneNumer;
}
}
RadixNode Class:
import java.util.HashMap;
import java.util.Map;
class RadixNode {
private final Map<Character, RadixNode> child = new HashMap<>();
private final Map<Customer, RadixNode> mobileNum = new HashMap<>();
private boolean endOfWord;
Map<Character, RadixNode> getChild() {
return child;
}
Map<Customer, RadixNode> getChildPhoneDir() {
return mobileNum;
}
boolean isEndOfWord() {
return endOfWord;
}
void setEndOfWord(boolean endOfWord) {
this.endOfWord = endOfWord;
}
}
Radix Class:
class Radix {
private RadixNode root;
Radix() {
root = new RadixNode();
}
void insert(String word) {
RadixNode current = root;
for (int i = 0; i < word.length(); i++) {
current = current.getChild().computeIfAbsent(word.charAt(i), c -> new RadixNode());
}
current.setEndOfWord(true);
}
void insert(Customer word) {
RadixNode current = root;
System.out.println("==========================================");
System.out.println(word.mobileNumer.length());
for (int i = 0; i < word.mobileNumer.length(); i++) {
current = current.getChildPhoneDir().computeIfAbsent(word.mobileNumer.charAt(i), c -> new RadixNode());
System.out.println(current);
}
current.setEndOfWord(true);
}
boolean delete(String word) {
return delete(root, word, 0);
}
boolean containsNode(String word) {
RadixNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
RadixNode node = current.getChild().get(ch);
if (node == null) {
return false;
}
current = node;
}
return current.isEndOfWord();
}
boolean isEmpty() {
return root == null;
}
private boolean delete(RadixNode current, String word, int index) {
if (index == word.length()) {
if (!current.isEndOfWord()) {
return false;
}
current.setEndOfWord(false);
return current.getChild().isEmpty();
}
char ch = word.charAt(index);
RadixNode node = current.getChild().get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1) && !node.isEndOfWord();
if (shouldDeleteCurrentNode) {
current.getChild().remove(ch);
return current.getChild().isEmpty();
}
return false;
}
public void displayContactsUtil(RadixNode curNode, String prefix)
{
// Check if the string 'prefix' ends at this Node
// If yes then display the string found so far
if (curNode.isEndOfWord())
System.out.println(prefix);
// Find all the adjacent Nodes to the current
// Node and then call the function recursively
// This is similar to performing DFS on a graph
for (char i = 'a'; i <= 'z'; i++)
{
RadixNode nextNode = curNode.getChild().get(i);
if (nextNode != null)
{
displayContactsUtil(nextNode, prefix + i);
}
}
}
public boolean displayContacts(String str)
{
RadixNode prevNode = root;
// 'flag' denotes whether the string entered
// so far is present in the Contact List
String prefix = "";
int len = str.length();
// Display the contact List for string formed
// after entering every character
int i;
for (i = 0; i < len; i++)
{
// 'str' stores the string entered so far
prefix += str.charAt(i);
// Get the last character entered
char lastChar = prefix.charAt(i);
// Find the Node corresponding to the last
// character of 'str' which is pointed by
// prevNode of the Trie
RadixNode curNode = prevNode.getChild().get(lastChar);
// If nothing found, then break the loop as
// no more prefixes are going to be present.
if (curNode == null)
{
System.out.println("No Results Found for \"" + prefix + "\"");
i++;
break;
}
// If present in trie then display all
// the contacts with given prefix.
System.out.println("Suggestions based on \"" + prefix + "\" are");
displayContactsUtil(curNode, prefix);
// Change prevNode for next prefix
prevNode = curNode;
}
for ( ; i < len; i++)
{
prefix += str.charAt(i);
System.out.println("No Results Found for \"" + prefix + "\"");
}
return true;
}
public void displayContactsUtil(RadixNode curNode, String prefix, boolean isPhoneNumber)
{
// Check if the string 'prefix' ends at this Node
// If yes then display the string found so far
if (curNode.isEndOfWord())
System.out.println(prefix);
// Find all the adjacent Nodes to the current
// Node and then call the function recursively
// This is similar to performing DFS on a graph
for (char i = '0'; i <= '9'; i++)
{
RadixNode nextNode = curNode.getChildPhoneDir().get(i);
if (nextNode != null)
{
displayContactsUtil(nextNode, prefix + i);
}
}
}
public boolean displayContacts(String str, boolean isPhoneNumber)
{
RadixNode prevNode = root;
// 'flag' denotes whether the string entered
// so far is present in the Contact List
String prefix = "";
int len = str.length();
// Display the contact List for string formed
// after entering every character
int i;
for (i = 0; i < len; i++)
{
// 'str' stores the string entered so far
prefix += str.charAt(i);
// Get the last character entered
char lastChar = prefix.charAt(i);
// Find the Node corresponding to the last
// character of 'str' which is pointed by
// prevNode of the Trie
RadixNode curNode = prevNode.getChildPhoneDir().get(lastChar);
// If nothing found, then break the loop as
// no more prefixes are going to be present.
if (curNode == null)
{
System.out.println("No Results Found for \"" + prefix + "\"");
i++;
break;
}
// If present in trie then display all
// the contacts with given prefix.
System.out.println("Suggestions based on \"" + prefix + "\" are");
displayContactsUtil(curNode, prefix, isPhoneNumber);
// Change prevNode for next prefix
prevNode = curNode;
}
for ( ; i < len; i++)
{
prefix += str.charAt(i);
System.out.println("No Results Found for \"" + prefix + "\"");
}
return true;
}
}
I have tried to search in a collection but got stuck. Any help / suggestion would be appreciated.
I propose you 2 ways of doing it.
First way: with a single trie.
It is possible to store all you need in a single trie. Your customer class is fine, and here is a possible RadixNode implementation.
I consider that there cannot be two customers with the same name, or with the same phone number. If it is not the case (possibility to have people with same name and different phone nb for instance) tell me in a comment I'll edit.
The thing that is important to understand, is that if you want to have two different ways of finding a customer, and you use a single trie, each customer will appear twice in your trie. Once at the end of the path corresponding to its name, and once after the end of the path corresponding to its phone number.
import java.util.HashMap;
import java.util.Map;
class RadixNode {
private Map<Character, RadixNode> children;
private Customer customer;
public RadixNode(){
this.children = new Map<Character, RadixNode>();
this.Customer = NULL;
}
Map<Character, RadixNode> getChildren() {
return children;
}
boolean hasCustomer() {
return this.customer != NULL;
}
Customer getCustomer() {
return customer;
}
void setCustomer(Customer customer) {
this.customer = customer;
}
}
As you can see, there is only one map storing the node's children. That is because we can see a phone number as a string of digits, so this trie will store all the customers ... twice. Once per name, once per phone number.
Now let's see an insert function. Your trie will need a root,n let's call it root.
public void insert(RadixNode root, Customer customer){
insert_with_name(root, customer, 0);
insert_with_phone_nb(root, customer, 0);
}
public void insert_with_name(RadixNode node, Customer customer, int idx){
if (idx == customer.getName().length()){
node.setCustomer(customer);
} else {
Character current_char = customer.getName().chatAt(idx);
if (! node.getChlidren().containsKey(current_char){
RadixNode new_child = new RadixNode();
node.getChildren().put(current_char, new_child);
}
insert_with_name(node.getChildren().get(current_char), customer, idx+1);
}
}
The insert_with_phone_nb() method is similar. This will work as long as people has unique names, unique phone numbers, and that someone's name cannot be someone's phone number.
As you can see, the method is recursive. I advice you to build your trie structure (and generally, everything based on tree structures) recursively, as it makes for simpler, and generallay cleaner code.
The search function is almost a copy-paste of the insert function:
public void search_by_name(RadixNode node, String name, int idx){
// returns NULL if there is no user going by that name
if (idx == name.length()){
return node.getCustomer();
} else {
Character current_char = name.chatAt(idx);
if (! node.getChlidren().containsKey(current_char){
return NULL;
} else {
return search_by_name(node.getChildren().get(current_char), name, idx+1);
}
}
}
Second way: with 2 tries
The principle is the same, all you have to do is reuse the code above, but keep two distinct root nodes, each of them will build a trie (one for names, one for phone numbers).
The only difference will be the insert function (as it will call insert_with_name and insert_with_phone_nb with 2 different roots), and the search function which will have to search in the right trie as well.
public void insert(RadixNode root_name_trie, RadixNode root_phone_trie, Customer customer){
insert_with_name(root_name_trie, customer, 0);
insert_with_phone_nb(root_phone_trie, customer, 0);
}
Edit: After comment precising there might be customers with the same name, here is an alternative implementation, to allow a RadixNode to contain references toward several Customer.
Replace the Customer customer attribute in RadixNode by, for example, a Vector<Customer>. The methods will have to be modified accordingly of course, and a search by name will then return to you a vector of customers (possibly empty), since this search can then lead to several results.
In your case, I'd go for a single trie, containing vectors of customers. So you can have both a search by name and phone (cast the number as a String), and a single data structure to maintain.

How to use a stack to parse a string

I have a String:
String stringContent="{\\*\\listtable{\\list{\\listlevel{\\leveltext}{\\levelNumber}}}}"
How do I select values of all enclosing braces one by one in each pass like this:
"{\\levelNumber}"
"{\\leveltext}"
"{\\listlevel{\\leveltext}{\\levelNumber}}"
"{\\list{\\listlevel{\\leveltext}}}"
"{\\*\\listtable{\\list{\\listlevel{\\leveltext}}}}"
So far I've done this:
public class StringExtracter {
public String stringofObject(Section parentSectionObject, String stringContent) {
Stack stack=new Stack();
String returnString = "";
char arr[] = stringContent.toCharArray();
for(int i=0;i<=arr.length;i++){
while(arr[i]!='}'){
if(arr[i]=='{'){
stringContent=stringContent.substring(i+1);
returnString=stringContent;
System.out.println(stringContent);
braces=true;
Section sectionObject=new Section(parentSectionObject,stringContent);
stack.push(arr[i]);
}
}
return returnString;
}
But the problem is that it is not detecting the right } like this. How should I be doing this?
Output as of now:
\*\listtable{\list{\listlevel{\leveltext}{\fefw}}}}
\list{\listlevel{\leveltext}{\fefw}}}}
\listlevel{\leveltext}{\fefw}}}}
\leveltext}{\fefw}}}}
\fefw}}}}
Stack-based solution (problably could be simpler, but let's solve the problem first):
public class Main {
public static class Node {
public int level;
public String content = "";
public List<Node> children = new ArrayList<>();
}
public static void main(String[] args) {
String input="{\\\\*\\\\listtable{\\\\list{\\\\listlevel{\\\\leveltext}{\\\\levelNumber}}}}";
Node root = null;
Stack<Node> stack = new Stack<>();
for(char c: input.toCharArray()) {
if (c == '{') {
Node n = new Node();
n.level = stack.size() + 1;
n.content += c;
stack.push(n);
if (root == null) root = n;
} else if (c == '}') {
Node n = stack.pop();
n.content += c;
if (!stack.isEmpty()) {
stack.peek().children.add(n);
}
} else {
stack.peek().content += c;
}
}
TreeTraverser<Node> treeTraverser = new TreeTraverser<Node>() {
#Override
public Iterable<Node> children(Node root) {
return root.children;
}
};
for(Node node : treeTraverser.preOrderTraversal(root)) {
String indent = String.format("%" + node.level + "s", " ");
System.out.println(indent + node.content);
}
}
}
Note: Google's Guava library is needed for the TreeTraverser
Output:
{\\*\\listtable}
{\\list}
{\\listlevel}
{\\leveltext}
{\\levelNumber}
Edit 1: modified to create a tree after additional input from the OP
Edit 2: modified to treat the siblings correctly
I recommend you to, instead of using a for loop, create a variable called i and increase it in the while loop. You're checking for "arr[i]!='}'" in the while loop, but as it's inside the for loop, i never increases, and therefore it's always checking the same character.

Create a Binary Tree from postfix expression

Let's say I have the following postfix expression : 5372-*-
I want to create a binary tree from this expression. My algoritm is : If my char is number put it into a stack if it is an operator pop two elements from the stack and make them the childs of the operator. Then push the operator into the stack. Which seems like it is working but I cannot manage to connect the little trees that I create.
My current code is :
public void myInsert(char ch, Stack s) {
if (Character.isDigit(ch)) // initial cond.
s.push(ch);
else {
TreeNode tParent = new TreeNode(ch);
TreeNode t = new TreeNode(s.pop());
TreeNode t2 = new TreeNode(s.pop());
tParent.right = t;
tParent.left = t2;
s.push(ch);
System.out.println("par" + tParent.ch);
System.out.println("cright" + tParent.right.ch);
System.out.println("cleft" + tParent.left.ch);
}
}
My test class :
Stack stree = new Stack();
BST b = new BST();
String str = "5-3*(7-2)";
String postfix = b.convertToPosFix(str);
System.out.println(postfix);
for (char ch : postfix.toCharArray()) {
b.myInsert(ch, stree);
}
My output is :
par-
cright2
cleft7
par*
cright-
cleft3
par-
cright*
cleft5
Use a Stack of TreeNodes, not a Stack of chars. You have to combine the TreeNodes after all and not the chars:
public void myInsert(char ch, Stack<TreeNode> s) {
if (Character.isDigit(ch)) {
// leaf (literal)
s.push(new TreeNode(ch));
} else {
// operator node
TreeNode tParent = new TreeNode(ch);
// add operands
tParent.right = s.pop();
tParent.left = s.pop();
// push result to operand stack
s.push(tParent);
}
}
TreeNode
public class TreeNode {
public TreeNode right = null;
public TreeNode left = null;
public final char ch;
TreeNode(char ch) {
this.ch = ch;
}
#Override
public String toString() {
return (right == null && left == null) ? Character.toString(ch) : "(" + left.toString()+ ch + right.toString() + ")";
}
}
main
public static TreeNode postfixToTree(String s) {
Stack<TreeNode> stree = new Stack<>();
BST b = new BST();
for (char ch : s.toCharArray()) {
b.myInsert(ch, stree);
}
return stree.pop();
}
public static void main(String[] args) {
System.out.println(postfixToTree("5372-*-"));
System.out.println(postfixToTree("512+4*+3−"));
System.out.println(postfixToTree("51*24*+"));
}
This will print
(5-(3*(7-2)))
((5+((1+2)*4))−3)
((5*1)+(2*4))

I'm making a word frequency counter and it's returning random words

I'm working on creating a program that will take an input text file and will print out the 10 most commonly used words and how many times they are used. However, it currently prints 10 random words, not ordered. Is there anything that I am missing?
public void insert(E word) {
if (word.equals("")) {
return;
}
//Adds 2 temporary nodes, and sets first to the first one if first is empty
Node temp = new Node(word);
Node temp2;
if (first == null) {
first = temp;
} else{
for (Node temp6 = first; temp6 != null; temp6 = temp6.next) {
if (temp6.key.equals(temp.key)) {
temp6.count++;
temp2 = temp6;
Node parent = first;
Node parent2 = first;
while (parent != null) {
if (parent.key.equals(word)) {
if (parent == first) {
first = first.next;
} else {
parent2.next = parent.next;
}
}
parent2 = parent;
parent = parent.next;
}
//replaces first with temp2 if temp2's count is higher than first's
if (temp2.count > first.count) {
Node temp3 = first;
first = temp2;
first.next = temp3;
}
//Adds 1 to the counter if the word is already in the linkedlist. Moves the node to the correct place and deletes the original node.
for (Node temp4 = first.next; temp4 != null; temp4 = temp4.next){
if(temp4.next.count < first.count){
Node temp5 = temp4.next;
temp4.next = temp2;
temp2.next = temp5;
break;
}
}
return;
}
}
current.next = temp;
}
current = temp;
}
The approach to your problem seems a bit overly complex at first glance. This may be because your Node class does something that requires a more complex approach. However, I would recommend using a Set. This way you can just create a POJO called Word that contains a String word, and Integer count. If you implement Comparable with this POJO then you can #Override compareTo(Word w) which you can then sort my count. Since a Set won't allow duplicates, you can then create a new Word for each word you read in, or simply increment the count of Word. Once you finish reading the entire file, you then just print out the first 10 objects in the list. Something to illustrate my point would be this example.
class Word implements Comparable<Word>{
String word;
Integer count;
Word(String w, Integer c) {
this.word = w;
this.count = c;
}
public String toString(){
return word + " appeared " + count + " times.";
}
#Override
public int compareTo(Word w) {
return this.count - w.count;
}
}
public class TestTreeMap {
public static void main(String[] args) {
//Add logic here for reading in from file and ...
}
}
Anyway, I hope this answer helps to point you in the right direction. As a side note, I tend to try and find the simplest solution, as the more clever we get the more unmaintainable our code becomes. Good luck!
Here is how we can do it using collection(s)
class WordCount {
public static void main (String[] are) {
//this should change. Used to keep it simple
String sentence = "Returns a key value mapping associated with the least key greater than or equal to the given key";
String[] array = sentence.split("\\s");
//to store the word and their count as we read them from the file
SortedMap<String, Integer> ht = new TreeMap<String, Integer>();
for (String s : array) {
if (ht.size() == 0) {
ht.put(s, 1);
} else {
if (ht.containsKey(s)) {
int count = (Integer) ht.get(s);
ht.put(s, count + 1);
} else {
ht.put(s, 1);
}
}
}
//impose reverse of the natural ordering on this map
SortedMap<Integer, String> ht1 = new TreeMap<Integer, String>(Collections.reverseOrder());
for (Map.Entry<String, Integer> entrySet : ht.entrySet()) {
//setting the values as key in this map
ht1.put(entrySet.getValue(), entrySet.getKey());
}
int firstTen = 0;
for (Map.Entry<Integer, String> entrySet : ht1.entrySet()) {
if (firstTen == 10)
break;
System.out.println("Word-" + entrySet.getValue() + " number of times-" + entrySet.getKey());
firstTen++;
}
}
}
there is one problem here...which is if there are two words with same frequency we see only one in the output.
So, I ended up modifying it again as below
class WordCount1 {
public static void main (String...arg) {
String sentence = "Returns a key value mapping mapping the mapping key the than or equal to the or key";
String[] array = sentence.split("\\s");
Map<String, Integer> hm = new HashMap<String, Integer>();
ValueComparator vc = new ValueComparator(hm);
SortedMap<String, Integer> ht = new TreeMap<String, Integer>(vc);
for (String s : array) {
if (hm.size() == 0) {
hm.put(s, 1);
} else {
if (hm.containsKey(s)) {
int count = (Integer) hm.get(s);
hm.put(s, count + 1);
} else {
hm.put(s, 1);
}
}
}
ht.putAll(hm);
int firstTen = 0;
for (Map.Entry<String, Integer> entrySet : ht.entrySet()) {
if (firstTen == 10)
break;
System.out.println("Word-" + entrySet.getKey() + " number of times-" + entrySet.getValue());
firstTen++;
}
}
and, the ValueComparator from here. Tweaked it a little and is as below
public class ValueComparator implements Comparator<String> {
Map<String, Integer> entry;
public ValueComparator(Map<String, Integer> entry) {
this.entry = entry;
}
public int compare(String a, String b) {
//return entry.get(a).compareTo(entry.get(b));
//return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));//from java source
return (entry.get(a) < entry.get(b) ? 1 : (entry.get(a) == entry.get(b) ? 1 : -1));
}
}
This program is case sensitive and in case you need case-insensitive behavior - just convert the strings to lowercase before putting into the Map.

Assignment on sorting a list

I'm very close to being done, but can't quite figure out how to tie everything together. I have the separate methods responsible for their particular task, but i feel like my cohesion is really bad. not real sure how they tie together and what needs to be called in main. The goal here is to read a text file from the command line and list the words in the story lexicographically.
% java Ten < story.txt
Word Occurs
==== ======
a 21
animal 3
.
.
.
zebra 1
%
Here's my code thus far:
import java.util.Scanner;
public class Ten2
{
public static void main(String [] arg)
{
Scanner input = new Scanner(System.in);
String word;
List sortedList = new List();
word = nextWord(input);
while (word!=null) {
sortedList.insert(word);
word = nextWord(input);
}
sortedList.printWords();
}
private static String nextWord(Scanner input)
{
// return null if there are no more words
if (input.hasNext() == false )
return null;
// take next element, convert to lowercase, store in s
else {
String s = input.next().toLowerCase() ;
// empty string
String token = "";
// loop through s and concatonate chars onto token
for (int i =0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)) == true)
token = token + s.charAt(i);
else if (s.charAt(i) == '\'' )
token = token + s.charAt(i);
else if (s.charAt(i) == '-')
token = token + s.charAt(i);
}
return token;
}
}
}
class List
{
/*
* Internally, the list of strings is represented by a linked chain
* of nodes belonging to the class ListNode. The strings are stored
* in lexicographical order.
*/
private static class ListNode
{
// instance variables for ListNode objects
public String word;
public ListNode next;
public int count;
// Listnode constructor
public ListNode(String w, ListNode nxt)
{
word = w; // token from nextWord()?
next = nxt; // link to next ListNode
count = 1; // number of word occurences
}
}
// instance variables for List object
private ListNode first;
private int numWords;
// constructor postcondition: creates new Listnode storing object
public List()
{
first = null; // pointer to ListNode?
numWords = 0; // counter for nodes in list
}
// Insert a specified word into the list, keeping the list
// in lexicographical order.
public void insert(String word)
{
// check if first is null
if (first == null) {
ListNode newNode;
newNode = addNode(word, null);
first = newNode;
}
// else if (first is not null) check if word matches first word in List
else if (word.equals(first.word)) {
// increase count
first.count++;
}
// else (first is not null && doesn't match first word)
else {
ListNode newNode;
ListNode current;
current = first;
ListNode previous;
previous = null;
int cmp = word.compareTo(current.word);
/*
* Fist two cases of empty list and word already existing
* handled in above if and else statements, now by using compareTo()
* method between the words, the insertion postion can be determined.
* Links between ListNode variables current and previous need to be
* modified in order to maintain the list
*/
// loop as long as value comparing to is positive
// when compareTo() returns positive this means the "word" parameter is greater than the word in the list
while ((cmp >0) && (current.next != null)) {
previous = current;
current = current.next;
cmp = word.compareTo(current.word);
}
// insert after current at end of list
if ((cmp >0 && current.next == null)) {
newNode = addNode(word, null);
current.next = newNode;
}
// increments count when word already exists
else if (cmp==0) {
current.count++;
}
// else (cmp < 0) we insert BEFORE current
else {
newNode = addNode(word, current);
// first node in list comes after new word
if (previous == null) {
first = newNode;
}
else {
// inserting new word in middle of list
previous.next = newNode;
}
}
}
}
// method to add new ListNode and increase counter
private ListNode addNode(String word, ListNode next)
{
ListNode newNode = new ListNode(word, next);
numWords++;
return newNode;
}
// Returns a string array that contains all the words in the list.
public String[] getWords()
{
String[] Words = new String[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Words[i] = current.word;
current = current.next;
i++;
}
return Words;
}
// Returns an int array that contains the number of times
// each word occurs in the list.
public int[] getNumbers()
{
int[] Numbers = new int[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Numbers[i] = current.count;
current = current.next;
i++;
}
return Numbers;
}
// Outputs the string array and int array containing all the
// words in the list and the number of times each occurs.
public void printWords()
{
int[] Numbers = getNumbers();
String[] Words = getWords();
System.out.println("Word \t \t Occurs");
System.out.println("==== \t \t ======");
for (int i =0; i < numWords; i++) {
System.out.println(Words[i] + " \t " + Numbers[i]);
}
}
}
Well, I would start by defining what you want your program to do, which you've already done:
The goal here is to read a text file from the command line and list the words in the story lexicographically.
You're main function does this almost. Basically, what you need is a loop to tie it together:
public static void main(String [] arg)
{
// print out your initial information first (i.e. your column headers)
// ...
List sortedList = new List();
String word = nextWord();
// now, the question is... what is the end condition of the loop?
// probably that there aren't any more words, so word in this case
// will be null
while (word != null)
{
sortedList.insert(word);
word = nextWord();
}
// now, your list takes care of the sorting, so let's just print the list
sortedList.printWords();
}
I think that's all there is to it. Normally, I don't like to post solutions to homework questions, but in this case, since you already had all of the code and you just needed a little nudge to drive you in the right direction, I think it's fine.
There are a few things I noticed that are incorrect with your
Your list constructor has a 'void' return type - there should be no return type on constructors:
public List() //make an empty list
{
first = null;
numWords = 0;
}
The 'else' statement in this method is unneeded:
public static String nextWord()
{
if ( keyboard.hasNext() == false )
return null;
else {
String start = keyboard.next().toLowerCase() ;
String organized = "";
for (int i =0; i < start.length(); i++) {
if (Character.isLetter(start.charAt(i)) == true)
organized = organized + start.charAt(i);
else if (start.charAt(i) == '\'' )
organized = organized + start.charAt(i);
else if (start.charAt(i) == '-')
organized = organized + start.charAt(i);
}
return organized;
}
}
So, this should be:
public static String nextWord()
{
if ( keyboard.hasNext() == false )
return null;
String start = keyboard.next().toLowerCase() ;
String organized = "";
for (int i =0; i < start.length(); i++) {
if (Character.isLetter(start.charAt(i)) == true)
organized = organized + start.charAt(i);
else if (start.charAt(i) == '\'' )
organized = organized + start.charAt(i);
else if (start.charAt(i) == '-')
organized = organized + start.charAt(i);
}
return organized;
}
If you want to use a BufferedReader, it's pretty easy. Just set it up in your main method:
if (arg.length > 0)
{
// open our file and read everything into a string buffer
BufferedReader bRead = null;
try {
bRead = new BufferedReader(new FileReader(arg[0]));
} catch(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
setupScanner(bRead);
}
Then, create a new method that sets up the scanner object:
public static void setupScanner(BufferedReader rdr)
{
keyboard = new Scanner(rdr);
}
And then just pass it in on the command line (i.e. java ten2 [filename])
import java.util.Scanner;
public class Ten2
{
public static void main(String [] arg)
{
Scanner input = new Scanner(System.in);
String word;
List sortedList = new List();
word = nextWord(input);
while (word!=null) {
sortedList.insert(word);
word = nextWord(input);
}
sortedList.printWords();
}
private static String nextWord(Scanner input)
{
// return null if there are no more words
if (input.hasNext() == false )
return null;
// take next element, convert to lowercase, store in s
else {
String s = input.next().toLowerCase() ;
// empty string
String token = "";
// loop through s and concatonate chars onto token
for (int i =0; i < s.length(); i++) {
if (Character.isLetter(s.charAt(i)) == true)
token = token + s.charAt(i);
else if (s.charAt(i) == '\'' )
token = token + s.charAt(i);
else if (s.charAt(i) == '-')
token = token + s.charAt(i);
}
return token;
}
}
}
class List
{
/*
* Internally, the list of strings is represented by a linked chain
* of nodes belonging to the class ListNode. The strings are stored
* in lexicographical order.
*/
private static class ListNode
{
// instance variables for ListNode objects
public String word;
public ListNode next;
public int count;
// Listnode constructor
public ListNode(String w, ListNode nxt)
{
word = w; // token from nextWord()?
next = nxt; // link to next ListNode
count = 1; // number of word occurences
}
}
// instance variables for List object
private ListNode first;
private int numWords;
// constructor postcondition: creates new Listnode storing object
public List()
{
first = null; // pointer to ListNode?
numWords = 0; // counter for nodes in list
}
// Insert a specified word into the list, keeping the list
// in lexicographical order.
public void insert(String word)
{
// check if first is null
if (first == null) {
ListNode newNode;
newNode = addNode(word, null);
first = newNode;
}
// else if (first is not null) check if word matches first word in List
else if (word.equals(first.word)) {
// increase count
first.count++;
}
// else (first is not null && doesn't match first word)
else {
ListNode newNode;
ListNode current;
current = first;
ListNode previous;
previous = null;
int cmp = word.compareTo(current.word);
/*
* Fist two cases of empty list and word already existing
* handled in above if and else statements, now by using compareTo()
* method between the words, the insertion postion can be determined.
* Links between ListNode variables current and previous need to be
* modified in order to maintain the list
*/
// loop as long as value comparing to is positive
// when compareTo() returns positive this means the "word" parameter is greater than the word in the list
while ((cmp >0) && (current.next != null)) {
previous = current;
current = current.next;
cmp = word.compareTo(current.word);
}
// insert after current at end of list
if ((cmp >0 && current.next == null)) {
newNode = addNode(word, null);
current.next = newNode;
}
// increments count when word already exists
else if (cmp==0) {
current.count++;
}
// else (cmp < 0) we insert BEFORE current
else {
newNode = addNode(word, current);
// first node in list comes after new word
if (previous == null) {
first = newNode;
}
else {
// inserting new word in middle of list
previous.next = newNode;
}
}
}
}
// method to add new ListNode and increase counter
private ListNode addNode(String word, ListNode next)
{
ListNode newNode = new ListNode(word, next);
numWords++;
return newNode;
}
// Returns a string array that contains all the words in the list.
public String[] getWords()
{
String[] Words = new String[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Words[i] = current.word;
current = current.next;
i++;
}
return Words;
}
// Returns an int array that contains the number of times
// each word occurs in the list.
public int[] getNumbers()
{
int[] Numbers = new int[numWords];
ListNode current = first;
int i =0;
while (current != null) {
Numbers[i] = current.count;
current = current.next;
i++;
}
return Numbers;
}
// Outputs the string array and int array containing all the
// words in the list and the number of times each occurs.
public void printWords()
{
int[] Numbers = getNumbers();
String[] Words = getWords();
System.out.println("Word \t \t Occurs");
System.out.println("==== \t \t ======");
for (int i =0; i < numWords; i++) {
System.out.println(Words[i] + " \t " + Numbers[i]);
}
}
}

Categories

Resources