2016-12-12 13 views
0

テキストファイルに各単語を入れてツリーに追加する必要があります。 私の最初の問題は、Javaでファイルを処理する方法を知らないので、単語を挿入する必要がありますが、重複する単語がある場合は、単語を再度挿入するのではなく、その単語のカウンタを増やします。 これらは私が持って挿入する方法です:テキストファイルから単語を単語に挿入する(Java)

public void insert(String txt) 
    { 
    this.root = insert(root, new Node(txt)); 
    } 

    private Node insert(Node parent, Node newNode) 
    { 
    if (parent == null) 
    { 
     return newNode; 
    } 
    else if (newNode.data.compareTo(parent.data) > 0) 
    { 
     parent.right = insert(parent.right, newNode); 
    } 
    else if (newNode.data.compareTo(parent.data) < 0) 
    { 
     parent.left = insert(parent.left, newNode); 
    } 
    return parent; 
    } 

誰も私を助けてもらえますか?

答えて

0

Nodeには、countという名前のインスタンス変数をNodeクラスに追加して、テキストが表示された回数を覚えておくことができます。 コンストラクタをcountに設定して1に設定する必要があります。


    public void insert(String txt) 
    { 
     root = insert(root, new Node(txt)); 
    } 

    private Node insert(Node parent, Node newNode) 
    { 
     if (parent == null) 
     { 
     return newNode; 
     } 

     final int comparison = newNode.data.compareTo(parent.data) 
     if (comparison > 0) 
     { 
     parent.right = insert(parent.right, newNode); 
     } 
     else if (comparison < 0) 
     { 
     parent.left = insert(parent.left, newNode); 
     } 
     else { // If the new text matches the text of this node. parent.count++; } 
     return parent; 
    } 

+0

これは役立ちます、ありがとう:

その後、あなたは、この(違った何をすべきかについて、太字部分を参照)のような何かを行うことができます – peppercumin

関連する問題