View Full Version : Traverse Binary Tree and build Hashmap

05-01-2012, 06:10 AM
I need to create a Hashmap that keeps track of the frequency of words in a BinarySearchTree.
How do i integrate the two to let the Hashmap know that its arguments are in the Tree?
Say my map is as follows:

// Initialize frequency table from command line
for (String a : args) {
Integer freq = m.get(a);
m.put(a, (freq == null) ? 1 : freq + 1);

System.out.println(m.size() + " distinct words:");

Explanations of your methods are especially helpful.

05-01-2012, 03:25 PM
You would use a Map<Object, Integer> datatype. The key is the object in question, and the value is the number of occurrences. Can be stored in a custom tree to internally keep track of the count. Alternatives include using two other collections to contain the object as well as the occurrences.