DSAVisualiseData Structures & Algorithms Studio
Trees & Ordered Maps Medium

Word Frequency Map (TreeMap)

Count exact, already separated strings with a TreeMap. The supplied items are the words: this lesson does not tokenize text or normalize keys.

TreeMap<String, Integer>

Input

Exact strings

Each JSON string is one supplied word. Whitespace, case, and accents stay exactly as entered.

Use a JSON array; "two words" remains one key.Missing keys return 0. An empty string is a valid key.

Scenarios

Ordered word map

Distinct keys in Java natural string order

0 keys
TreeMap keyCount

The TreeMap orders keys; frequencies do not change their position.

Current operation

Trace state
Key—
Lookup result—
Iterator position—
Map writes 0Queries 0Keys yielded 0
BestO(log U)
AverageO(log U)
WorstO(log U)

U is the number of distinct keys; TreeMap keeps its search tree balanced.

Algorithm code

Java
WordCounter.java · algorithms_exercises @ 9cf5b34Step linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1