Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Location
    USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Traverse Binary Tree and build Hashmap

    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:

    Code:
    // 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:");
    System.out.println(m);
    Explanations of your methods are especially helpful.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •