The past few weeks I have been learning about iterators. I still do not understand the main difference between iterating through a link list and traversing through one. I know that traversing means to go through (visiting every element) the link list, and when iterating you basically do the same thing, but what is different, and why cannot you iterate through everything (standard library data-structures)?
“Traversal” just means walking through (all or some) elements of a data structure.
Historically, “iteration” in computer science is a special form of recursion for which no additional stack space is needed1 – in other words, tail recursion. This form is computationally exactly equivalent to what we now colloquially know as “iteration”, namely a finite loop (such as a for
loop with a fixed lower and upper bound).
Now this makes iteration especially well suited (compared to general recursion) for traversing linear data structures2. Note that it does not work for all containers (e.g. general graphs) because here you need to remember the already visited nodes (e.g. using depth-first search, which is defined recursively in terms of adjacent nodes, rather than via the C++ concept of iterators).
It is in this context that the term “iteration” is used in C++ applied to containers.
In summary: not every traversal is an iteration but every iteration (over a data structure) is a traversal. However, it’s worth noting that many people make no such distinction and use the terms interchangeably.
1 In particular this is the usage of Gerald Sussman of SICP fame.
2 But seemingly non-linear data structures such as trees can be linearised for the purpose of iteration by applying the right-hand rule (wall-following algorithm) and can thus be traversed without a stack.
not every traversal is an iteration
Do you have an example for this? –
Expressionism every iteration is a traversal
: This seems too strong to me -- traversal is with respect to a data structure, while iteration in general does not. As a very simple example, an iterative algorithm to multiply two numbers by repeated addition isn't traversing anything. –
Dispersal AFAIK they are synonymous. What makes you think there is a difference?
I feel ;) that "traverse" is sometimes used to indicate that internal structure is exploited. You traverse a tree by going from parentnodes to childnodes or you traverse a list by following the next pointers.
An array on the other hand you iterate over. You have all the elements, just work through them one by one.
Note: I just made this definition up, but it fits my mental model, so I'm going with it.
Iteration is discrete, traversal may or may not be. So, you can traverse the range of allowable volumes on the analog volume knob on your speaker, but you cannot iterate through them.
But iteration is a type of traversal. So every iteration is a traversal, but not every traversal is an iteration.
I would call iterator
the "agent" and traverse
the "action". Actually, it often confuses me when people refer to traversing
over something as iterating
over something (because to me iteration
is related to numerical methods which are converging towards a mathematical point via iteration). On the other hand, even I use the words interchangeably.
You cannot iterate
over containers for which there is no concept of traversal
. At a minimum, in order to traverse
something, you need to know at least whether you have a neighbor, and how to get to it.
A iterator basically gives you the functionality to traverse through a datastructure - visiting every element. I would call traversing
and iterating
synonyms. There are iterators
, but I don't know traversers
in programming.
and why cannot you iterate through everything(STL datastructures)?
Some datastructures do not have information that allows this. For example on a stack you should not be able to traverse though all the elements because you can only access the top of the stack.
Traverse: go through and consider or discuss the whole extent of a subject. travel or extend across or through by means of a practicable series of movements that consider or discuss the whole extent of a subject. basically go through the items of a subject.
Iterate: perform an operation repeatedly, applying it to the result of the previous application.
so
by definition, traverse doesn't care how or what you're going through, only that you cover the entire subject (but not necessarily every item in context). iterate repeats a process that can be applied at each step of a traversal.
or
for any subject with a collection of entries:
- traverse: go through the entries and cover the whole subject.
- iterate: repeat a process, which can be for each entry as you traverse something.
Note: iterate is not a type of traversal, it is in essence a repeated action. for example the iteration of a for
or foreach
loop can be a traversal of something however you can iterate each block like in a while
or do while
loop that does not need to traverse anything.
© 2022 - 2025 — McMap. All rights reserved.
iterator
is something thattraverses
a container. – Dotation