Rhokai
Rhokai

Reputation: 373

Separate words from a paragraph and insert to a tree structure

I want to create a program that use as a word counter. I intended to use a tree structure for store words. I also want to count the frequencies of word. I already built the binary tree and the method to get user input. How should i do this? Help please.

This is my code.

public class Node {

int a;
String b;

Node leftChild;
Node rightChild;

Node(String b){
    this.a = 0;
    this.b = b;

    this.leftChild = null;
    this.rightChild = null;

}

public void display(){
    System.out.println(" "+ b + " ");
}



public class Tree {

public Node root;

public Tree(){
    root = null;
}


public void insert(String word){

    Node newNode = new Node(word);
    newNode.b = word;

    if(root==null){
        root = newNode;
    }else{
        Node current = root;
        Node parent;

        while(true){
           parent = current;
           if(word.compareToIgnoreCase(current.b)<0){

               current = current.leftChild;

           if(current == null){
               parent.leftChild = newNode;
               return;
           }                   
        }else{
               current = current.rightChild;

               if(current == null){
                   parent.rightChild  = newNode;
                   return;
               }
           }
    }

}






public Node search(String word){
    Node current = root;

    while(!current.b.equalsIgnoreCase(word)){

        if(word.compareToIgnoreCase(current.b)<0){
            current= current.leftChild;
        }else{
            current = current.rightChild;
        }

        if (current == null)
            return null;
    }
    return current;
}


public void inOrder(Node localRoot){

    if(localRoot != null){
        inOrder(localRoot.leftChild);
        localRoot.display();
        inOrder(localRoot.rightChild);
    }
}

This is main method. (Though it is not even near complete)

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Tree newTree = new Tree();          

    Scanner inputString = new Scanner(System.in);

    System.out.println("Type the paragraph and press \"Enter\" :");
    String input = inputString.nextLine();            

    newTree.inOrder(newTree.root);  


}

Upvotes: 0

Views: 1063

Answers (2)

Bohemian
Bohemian

Reputation: 425043

IMHO the problem with your tree is that it exists - you don't need one!

Counting word frequencies has a canonical, and quite terse, solution:

public static Map<String, Integer> wordCounts(String input) {
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (String word : input.toLowerCase().split("(?s)\\s+"))
        map.put(word, map.containsKey(word) ? map.get(word) + 1 : 1);
    return map;
}

Upvotes: 0

JMP
JMP

Reputation: 81

It would be easier to use a map or something like that for storing the number of occurrences of the words.

But if you want to use the tree structure for some reason and I understand your question correctly, you should modify your insert method:

//...
}else{
  current = current.rightChild;

  if(current == null){
   parent.rightChild  = newNode;
   return;
  } /*begin modification*/ else if (current.b.equalsIgnoreCase(word)) { // check if word already is stored in tree
    ++current.a; // increase counter
  } /*end modification*/
}

Now you can use your insert method and count the words add the same time. But note that your counter begins with 0 at the moment because of this.a = 0; in the Node constructor.

In your main method you can then split the paragraph into words (String[] words = input.split(" ");) and add every word in the array with newTree.insert(words[i]); in a for loop.

If this is not what you wanted to know you would have to specify your question more clearly.

Upvotes: 1

Related Questions