Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*************I have the code, just can\'t figure out how to make it give an outp

ID: 667264 • Letter: #

Question

*************I have the code, just can't figure out how to make it give an output and of words bigger than 3.************

When an author produce an index for his or her book, the first step in this process is to decide which words should go into the index; the second is to produce a list of the pages where each word occurs. Instead of trying to choose words out of our heads, we decided to let the computer produce a list of all the unique words used in the manuscript and their frequency of occurrence. We could then go over the list and choose which words to put into the index.

The main object in this problem is a "word" with associated frequency. The tentative definition of "word" here is a string of alphanumeric characters between markers where markers are white space and all punctuation marks; anything non-alphanumeric stops the reading. If we skip all un-allowed characters before getting the string, we should have exactly what we want. Ignoring words of fewer than three letters will remove from consideration such as "a", "is", "to", "do", and "by" that do not belong in an index.

In this project, you are asked to write a program to read any text file and then list all the "words" in alphabetic order with their frequency together appeared in the article. The "word" is defined above and has at least three letters.

Note:

Your result should be printed to an output file named YourUserID.txt.

You need to create a Binary Search Tree (BST) to store all the word object by writing an insertion or increment function. Finally, a proper traversal print function of the BST should be able to output the required results.

The BST class in the text can not be used directly to solve this problem. It is also NOT a good idea to modify the BST class to solve this problem. Instead, the following codes are recommended to start your program.

Sample Run

Please type the text file name: Lincoln.txt

Please give the output text file name: mus11.txt

You are done! You can open the file "mus11.txt" to check.

Press any key to continue

------------------------------------------------------------------------------------------------------------------------------------------------

Here's the code--

#include

#include

#include

#include

using namespace std;

struct tree_node

{

tree_node* left;

tree_node* right;

int data;

string word;

};

tree_node* root=NULL;

void inorder(tree_node*);

void insert(string);

// Smaller elements go left

// larger elements go right

void insert(string word)

{

tree_node* t = new tree_node;

tree_node* parent;

t->data = 1;

t->left = NULL;

t->right = NULL;

t->word=word;

parent = NULL;

  

// is this a new tree?

if(root==NULL)

{

root = t;

return;

}

  

//Note: ALL insertions are as leaf nodes

tree_node* curr;

curr = root;

// Find the Node's parent

while(curr)

{

  

parent = curr;

if(curr->word == t->word)

{

curr->data +=1;

return;

}

else if(t->word > curr->word)

curr = curr->right;

else

curr = curr->left;

}

  

if(t->data < parent->data)

parent->left = t;

else

parent->right = t;

  

}

void inorder(tree_node* print)

{

  

if(print==NULL)

return;

  

inorder(print->left);

ofstream out ("Output.txt", ios::app);

cout<word<<" "<data<

out<word<<" "<data<

out.close();

inorder(print->right);

  

}

int main()

{

string tmp;

ifstream in("yourId.txt",ios::in);

  

while(in>>tmp)

{

  

insert(tmp);

  

}

cout<< "output " << "--------" << endl;

inorder(root);

getchar();

}

-–––––––––––––––––––––––––––––––––––––-–––––––––––––––––––––––––––––––––––––-––––––––––––––––––––––––––

Help would be Greatly appreciated.

1. How to edit this code so only words of size bigger than 3 are indexed.

2. I cant actually see any output

Explanation / Answer

I have coded in java ,don't have sufficient knowledge in C:

import java.io.*;
import java.util.*;

public class AlphabeticBinarySortTree
{

private static TreeNode root;
private static TreeNode runner;

static String[] alphaArray;

static int alphaCounter;

private static TreeNode alphaRunner;

//Inner class
private static class TreeNode
{
String word;
TreeNode left;
TreeNode right;
int count;

public TreeNode(String word)
{
this.word = word;
left = null;
right = null;
}


public void insertAll(TreeNode newNode)
{
if(newNode.word.compareTo(runner.word) < 1)
{
System.out.println("newNode.word = " + newNode.word);
if(runner.left == null)
{
runner.left = newNode;
runner = runner.left;
}

else
{
insertAll(newNode);
}
}

else if(newNode.word.compareTo(runner.word) > 1)
{
System.out.println("newNode.word = " + newNode.word);
if(runner.right == null)
{
runner.right = newNode;
runner = runner.right;

}

else
{
insertAll(newNode);
}
}

else
{
count++;
}
}// end method insertAll

public static void printInPreOrder(TreeNode root)
{
System.out.println(root.word + " ");

if(root.left != null)
{
printInPreOrder(root.left);
}

if(root.right != null)
{
printInPreOrder(root.right);
}

} //end method printInPreOrder()


//called from inside main
public static void arrangeInAscendingOrder(TreeNode root, PrintWriter pWriter)
{
if(root.left != null)
{
arrangeInAscendingOrder(root.left, pWriter);
}

System.out.println();
System.out.println();
System.out.println(root.word + " ");
pWriter.write(root.word + " ");

if(root.right != null)
{
arrangeInAscendingOrder(root.right, pWriter);
}

}

}//end inner class TreeNode

public AlphabeticBinarySortTree()
{
root = null;
}

//belong to the outer class

public static void main(String[] args)
{

System.out.println("This program reads text from a file that it will parse. ");
System.out.println("In doing so, it will eliminate duplicate strings and ");
System.out.println("pick up only unique strings.These strings will be in a ");
System.out.println("stored in alphabetical order in a binary Search tree before they are ");
System.out.println("written out to another text file in alphabetic order");

//open the file for reading
try
{
BufferedReader bReader = new BufferedReader(new FileReader("YourUserID.txt"));
String words;
int count;


//System.out.println("A test to inspect the contents of words: " + words);

//System.out.println("Words =" + words);
count = 0;

//why is there an endless loop when
//I use "while(str != null)

StringTokenizer st;
st = null;


//based on http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html
while ((words = bReader.readLine()) != null)
{
st = new StringTokenizer(words);

while(st.hasMoreTokens())
{
//shiffman.net/teaching/a2z/concordance
String token = st.nextToken();
System.out.println("Token = " +token);
AlphabeticBinarySortTree.initiateInsert(token);

//count the number of tokens in the string
count++;
}//end inner while

}//end outer while


System.out.println("Here are the contents of your tree:");
//System.out.println("before the call to print()");
print();

System.out.println("the no of words in the file is: " + count);


bReader.close();

}//end of try

catch(IOException exception)
{
exception.printStackTrace();
}
/**try
{
FileWriter fWriter = new FileWriter("Output.txt");

BufferedWriter bWriter = new BufferedWriter(fWriter);

PrintWriter pWriter = new PrintWriter(bWriter);

}

catch(IOExcepion exception)
{
exception.printStackTrace();
}*/

} // end main here


//this method belongs to the outer class
static void initiateInsert(String word)
{
//TreeNode is also static by the way
TreeNode newNode = new TreeNode(word);

if(root == null)
{
root = newNode;
System.out.println("root.word = " + root.word);
runner = root;
}

else
{
runner.insertAll(newNode);
}

}


// Start the recursive traversing of the tree
  

public static void print()
{

if (root != null)
{
  
AlphabeticBinarySortTree.TreeNode.printInPreOrder(root);

try
{
FileWriter fWriter = new FileWriter("Output.txt");

BufferedWriter bWriter = new BufferedWriter(fWriter);

PrintWriter pWriter = new PrintWriter(bWriter);

AlphabeticBinarySortTree.TreeNode.arrangeInAscendingOrder(root, pWriter);

pWriter.close();

}

catch(IOException eException)
{
eException.printStackTrace();
}

}//end of if block

} // end of method print


}//end outer enclosing class here