Why is TreeSet Iteration On instead of Onlogn?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the context of Java's TreeSet
, a frequently asked question is why iteration over a TreeSet
is an operation instead of . This article delves into the structure of a TreeSet
, how iteration is handled, and why its complexity remains linear despite its underlying data structure.
Understanding TreeSet and Its Underlying Structure
Java's TreeSet
is part of the Java Collections Framework and implements the SortedSet
interface. It maintains its elements in a sorted order based on their natural ordering or according to a specified comparator. Internally, TreeSet
leverages a TreeMap
, which is a Red-Black Tree data structure.
Characteristics of Red-Black Trees
Before discussing iteration, let's analyze some critical characteristics of Red-Black trees:
- Balanced Nature: Red-Black trees are balanced binary search trees, where every path from the root to the farthest leaf is no more than twice the length of any other such path.
- Operations Complexity: Due to their balanced nature, operations like insertion, deletion, and lookup have a time complexity of .
These attributes are crucial for ensuring efficient maintenance of order and balance within a TreeSet
.
The Iteration Process
To comprehend why iteration in a TreeSet
is , it’s necessary to dive into how iteration occurs:
- In-Order Traversal: Iteration over a
TreeSetemploys in-order traversal. In-order traversal visits nodes in ascending order by processing:- The left subtree
- The node itself
- The right subtree Such traversal ensures every element is visited once in the naturally sorted sequence.
- Traversal Complexity: Although navigation from a node to its next element using the tree's iterator is for a single step, in-order traversal inherently manages to move to the next node in constant time due to the presence of
Parentpointers. - Efficient Navigation Using Successor Pointer: A critical optimization in
TreeSetis using successor pointers that facilitate moving to the next higher node efficiently without restarting from the root. Consequently, traversal over allnelements ends up being , not .
Simplified Example of TreeSet Iteration:
Consider the following sequence in a TreeSet
structured as a Red-Black Tree:
5 15 2 7 12 17
Related reading
- Why is vector array doubled?
- Why isn't there an XML-serializable dictionary in .NET?
- Why k-d trees is not used for high dimensional data?
- Why Large Object Heap and why do we care?
- Why is UIBezierPath faster than Core Graphics path?
- Why is ''x'' in ''x'', faster than ''x'' ''x''?
- Why is try ... finally ... good; try ... catch bad?
- Why is UncaughtExceptionHandler not called by ExecutorService?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.