Question

Please help to this program: Using Java programming language Create a binary tree class. Using the...

Please help to this program:

Using Java programming language Create a binary tree class. Using the class, create a program that prompts the user for a text file. The program should parse the file, assigning each word the file contains to a node within the binary tree. Each node within the binary tree should store a word and keep a count of the number of times the word has occurred within the file. The program should output the words in alphabetical order along with their count.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BNode.java

//BNode class is user to create a node
class BNode
{
   BNode left,right;
   int count;
   String word;

   public BNode(String word)
   {
       this.word=word;
       this.count=1;
       this.left=null;
       this.right=null;
   }
}

BST.java

//BST class contains implementation of all required methods
class BST
{
   private BNode root;
   //Constructor
   public BST()
   {
       root=null;
   }
   //call insert by passing root
   public void insert(String word)
   {
       insert(root,word);
   }
   public void insert(BNode node, String word)
   {
       //If root is null then make it as root
       if(root==null)
       {
           root=new BNode(word);
       }
       //If current node value is lexiographically greater than word then go to left subtree
       else if(word.compareToIgnoreCase(node.word)<0)
       {
           //If left node is null then make it as left node
           if(node.left==null)
           {
               node.left=new BNode(word);
               return;
           }
           //Else go to left node
           insert(node.left,word);
       }
       //If current node value is lexiographically smaller than word then go to right subtree
       else if(word.compareToIgnoreCase(node.word)>0)
       {
           //If right node is null then make it as right node
           if(node.right==null)
           {
               node.right=new BNode(word);
               return;
           }
           //Else go to right node
           insert(node.right,word);
       }
       //If node already existed then increments it's occurance
       else
       {
           node.count++;
       }
   }
   public void inorder()
   {
       inorder(root);
   }
   //Inorder to print the tree in lexiographic order
   public void inorder(BNode node)
   {
       if(node!=null)
       {
           inorder(node.left);
           System.out.println(node.word+" "+node.count);
           inorder(node.right);
       }
   }
}

Demo.java

import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

class Demo
{
   public static void main(String args[])throws IOException
   {
       //Scanner class to take file name from user
       Scanner scanner=new Scanner(System.in);
       System.out.print("Enter file name: ");
       String file=scanner.next();

       //Open file
       FileReader fr=new FileReader(file);
       BufferedReader br=new BufferedReader(fr);
       //Create object for BST
       BST bst=new BST();
       String s="";
       //Get each line of file to string S
       while((s=br.readLine())!=null)
       {
           //Split string S into words
           StringTokenizer st=new StringTokenizer(s);
           //Iterate until there are no words in line
           while(st.hasMoreTokens())
           {
               //Insert each word
               bst.insert(st.nextToken());
           }
       }
      
       //Call inorder to print the output as lexiographic order
       bst.inorder();

       //close the file
       br.close();
       fr.close();
   }
}

output screenshot:

input.txt:

ant ball bingo cat dog ant cat goat
jeep ball zoo yet good boy you are
you are very good boy you are very good girl
i am david
i am doing job

Add a comment
Know the answer?
Add Answer to:
Please help to this program: Using Java programming language Create a binary tree class. Using the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT