# Chapter 12.2 Binary tree traversal algorithms

**URL:** <https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [December 20, 2020, 5:13pm UTC](https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089 "2020-12-20T17:13:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hlung](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hlung/32/94171_2.png) [@hlung](https://forums.kodeco.com/u/hlung)\
**Post date:** [December 20, 2020, 5:13pm UTC](https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089/1 "2020-12-20T17:13:10Z")

</div>

It hard for me how to memorize the difference between each traversal algorithms… until I see the code side-by-side in the playground:

```auto
public func traversePreOrder(visit: (Element) -> Void) {
  visit(value)
  leftChild?.traversePreOrder(visit: visit)
  rightChild?.traversePreOrder(visit: visit)
}

public func traverseInOrder(visit: (Element) -> Void) {
  leftChild?.traverseInOrder(visit: visit)
  visit(value)
  rightChild?.traverseInOrder(visit: visit)
}

public func traversePostOrder(visit: (Element) -> Void) {
  leftChild?.traversePostOrder(visit: visit)
  rightChild?.traversePostOrder(visit: visit)
  visit(value)
}

```

pre-order, visit() at front  
in-order, visit() in middle  
post-order, visit() at back

It’s like a eureka moment for me. I could easily create a mental model for all the types of traversals. I wish the book, although already very near, has a side-by-side view of the code like this.

---

<div class="post-metadata">

**Author:** ![gdelarosa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gdelarosa/32/109063_2.png) [@gdelarosa](https://forums.kodeco.com/u/gdelarosa)\
**Post date:** [December 21, 2020, 6:09pm UTC](https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089/2 "2020-12-21T18:09:43Z")

</div>

Thank you for sharing this @hlung! Being a visual learner I too enjoy looking at side by side comparisons.

Best,  
Gina
