I’m currently learning about binary trees and binary search trees, and one of the exercises I’m working on involves reading a text file, storing each word in a binary tree alphabetically, and traversing the tree using different methods.
Here are the exact specifications:
Read in the text and build a binary search tree comprising of all the words in the text (based alphabetically), store the word and keep a count of the word’s frequency (the number of times each word appears in the text) in a node, and perform tree traversals mentioned in class.
My question is, how can I keep track of a word’s frequency when I’m adding it to the tree? We’ve never covered identical nodes in class, so I’m stuck here. Any suggestions are appreciated!
Simple. Binary tree node will comprise of two elements one being the string (say the key) and the other being the integer count (say the value). While adding an element check as to if it is already there, if yes then simply increment count else add the element as a new binary tree node with a count of 1.