Tree - Visitor (Design Pattern)

Data System Architecture


Even if the visitor pattern can be applied to a list, it's usually associated to a Tree structure (Composites).

It separates the operation on the nodes of a tree from its structure. The node are then independent of the operations that apply to them.

The operations are packaged in a separate object, called a visitor. The visitor is then passed to the node of the tree as it’s traversed. If an node “accepts” the visitor, the visitor will then execute the operation for that node. (The node is an argument of the visitor method).

See Visitor


The visitor pattern extracts all operations on node from the tree structure because

  • Distributing all operations across the various tree node classes leads to a system that’s hard to understand, maintain, and change.
  • it's confusing to have mixed code such as pretty-printing code or flow analysis code.
  • adding a new operation usually requires recompiling all of these classes.


  • Accumulating state. Visitors can accumulate state as they visit each element in the object structure. Without a visitor, this state would be passed as extra arguments to the operations that perform the traversal, or they might appear as global variables.
  • It can visit objects that don’t have a common parent class. You can add any type of object to a Visitor interface.


With the Visitor pattern, you define two class hierarchies:

  • one for the elements being operated on (the Node hierarchy)
  • and one for the visitors that define operations on the elements (the NodeVisitor hierarchy).

This is the key to the Visitor pattern: The operation that gets executed depends on both the type of Visitor and the type of Node it visits.

Double dispatch

Effectively, the Visitor pattern lets you add operations to classes without changing them. In fact, some programming languages support it directly (CLOS, for example). Languages like C + + and Smalltalk support single-dispatch.

“Double-dispatch” simply means the operation that gets executed depends on:

  • the kind of request
  • and the types of two receivers.

Accept is a double-dispatch operation. Its meaning depends on two types:

  • the Visitor’s
  • and the Element’s. (Node)

An internal iterator will not cause double-dispatching— it will call an operation on the visitor with an element as an argument as opposed to calling an operation on the element with the visitor as an argument.

Structure and Iteration

Often the object structure is responsible for iteration.

  • A collection will simply iterate over its elements, calling the Accept operation on each.
  • A composite (tree) will commonly traverse itself by having each Accept operation traverse the element’s children and call Accept on each of them recursively.

The main reason to put the traversal strategy in the visitor is to implement a particularly complex traversal, one that depends on the results of the operations on the object structure.

  • Visitors can be used to apply an operation over an object structure defined by the Composite pattern.
  • Interpreter: Visitor may be applied to do the interpretation.

Documentation / Reference

Discover More
Data System Architecture
(Tree|Nested Set|Hierarchy) Data Structure

A tree is a node that may have children. Tree's are inherently recursive by definition as each child of a node is a Tree itself, with or without children nodes. A tree is a special case of a graph structure...
Card Puncher Data Processing
Antlr - Parse Tree Visitor

The in Antlr that will visit the parse tree in a depth-first search order In a visitor, you can: control the order of visite return data from the function Use mainly when the code is spread around...
Data System Architecture
Tree - (Traversal|Search)

In computer science, tree traversal (also known as tree search) refers to the process of visiting (examining and/or updating) each node in a tree data structure, exactly once, in a systematic way. Tree...
Data System Architecture
Tree - Operation

on a tree node get path size ... others:

Share this page:
Follow us:
Task Runner