• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching Binary Tree for occurrences of letters

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like some help, I have a binary tree which holds a story and I want to count the occurrences of letters (i.e. how many times e is used and how many time a is used). And do this for each letter of the alphabet. I'm not too sure what methods to use, any help would be much appreciated.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the data in the tree? Words? Sentences? Letters?
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its a story, and each line is a node.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, my first approach would just be to declare an array of 26 integers (for the letters of the alphabet), then do any tree traversal (preorder, inorder, postorder) that's convenient. At each node, you need a for-loop over the String; use Character.isLetter() at each char to see if it's a letter. If it is, force it to lower-case, subtract 'a', and increment the integer at that index in the array. Pretty simple.

Now, of course, that's going to work only for ASCII text, and since Java characters can have many other letter values, there's a large potential for error here. You can deal with this in many ways: ignore it, check each character to see if it's an ASCII letter, use a bigger array (one with 65536 entries)... depends on your problem definition.
reply
    Bookmark Topic Watch Topic
  • New Topic