Junior — Middle
What is the time complexity of searching for an element by key in a TreeMap data structure?
sobes.tech AI
Answer from AI
In Java, the TreeMap data structure is implemented based on a red-black tree — a balanced binary search tree. The time complexity of searching for an element by key in a TreeMap is O(log n), where n is the number of elements in the map. This is because the search involves traversing a tree whose height is proportional to the logarithm of the number of elements.