获取二叉树中重复字符串的计数

时间:2016-05-01 23:07:35

标签: java tree

所以我必须读入一个文件并将其放入二叉树中,我已经完成了。对于第二部分,我需要遍历树并输出每个字符串重复的次数。

读入文件:

test sentence this is for binary trees this is a test sentence this this is 

它应该是什么样子的想法:

this = 4;
test = 2;
sentence = 2;
is = 3;

这是我的代码:

// A binary tree node
class Node {

String data;
Node left, right;

Node(String item) {
    data = item;
    left = right = null;
}
}


import java.util.*;
import java.io.*;
class BinaryTree {

static Node root;
static int preIndex = 0;

//tree builder
Node buildTree(String strArr[], int inStrt, int inEnd) {

    if (inStrt > inEnd) {
        return null;
    }

    /* Pick current node from Preorder traversal using preIndex
     and increment preIndex */
    Node tNode = new Node(strArr[preIndex++]);

    /* If this node has no children then return */
    if (inStrt == inEnd) {
        return tNode;
    }

    /* Else find the index of this node in Inorder traversal */
    int inIndex = search(strArr, inStrt, inEnd, tNode.data);

    /* Using index in Inorder traversal, construct left and
     right subtress */
    tNode.left = buildTree(strArr, inStrt, inIndex - 1);
    tNode.right = buildTree(strArr, inIndex + 1, inEnd);

    return tNode;
}

int search(String[] strArr, int strt, int end, String value) {
    int i;
    for (i = strt; i <= end; i++) {
        if (strArr[i] == value) {
            return i;
        }

    }
    return i;
}

//print function
void printInorder(Node node) {
    if (node == null) {
        return;
    }

    /* first recur on left child */
    printInorder(node.left);

    /* then print the data of node */
    System.out.print(node.data + " ");

    /* now recur on right child */
    printInorder(node.right);
}

// driver program to test above functions
public static void main(String args[]) throws IOException{
    BinaryTree tree = new BinaryTree();
    Scanner inFile = new Scanner(new File("testSentence.txt"));
    int i = 0;
    String strArr[] = new String[15];

    while(inFile.hasNext()){
      strArr[i] = inFile.next();
      i++;
    }


    /* Testing ArrayList
     * for(int i = 0; i < strArr.size(); i++){
      System.out.println("Word " + i + " = " + strArr.get(i));

    }*/


    int len = strArr.length;
    Node mynode = tree.buildTree(strArr, 0, len - 1);

    // building the tree by printing inorder traversal
    System.out.println("Inorder traversal of constructed tree is : ");
    tree.printInorder(mynode);

}      
}

输出:

Inorder traversal of constructed tree is : 
test sentence this is for binary trees this is a test sentence this this is 

1 个答案:

答案 0 :(得分:0)

您可以在BinaryTree类中使用一个方法,该方法将字符串的HashMap转换为整数来跟踪单词的频率。

public void getStringFreq (Node node, Map<String, Integer> map)
{
    if (node == null) {
       return;
    }

   /* left child */
   getStringFreq(node.left, map);

   /* get the data of node */
   String s = node.data;

   Integer i = map.get(s); // have we mapped it yet

   // not yet
   if (i == null)
      map.put(s, 1);
   // already mapped, increment freq
   else
      map.put(s, i + 1);

   /* right child */
   getStringFreq(node.right, map);
}

使用

进行调用
Map<String, Integer> map = new HashMap<String, Integer> ();
tree.getStringFreq(mynode, map);
System.out.println(map);

<强>输出

  

{sentence = 2,a = 1,test = 2,binary = 1,this = 4,for = 1,= 3,trees = 1}