Keeping track of duplicates in BST

Basically, I have a binary search tree. I can insert, delete, and print it. However, when I print it, I need to find a way to keep track of how many times each word is in the tree.

User input: This is an awesome forum forum.

printtree
an, 1
awesome, 1
forum, 2
is, 1
This, 1

is what needs to be the output. I have the print, just can't figure out a way to keep track.

Also, say for insert(awesome), awesome would then go to 2. Same for delete but it would -1.

I was thinking of maybe a search method but then it would have to keep searching after the word is found? and keep a counter?

Thank you in advance for ideas.
Last edited on
Since it is a search tree, equal words will occur in a sequence. So instead of just printing them, look at the last word and compare; depending on the result increment a counter or print the word.
Topic archived. No new replies allowed.