# Help understanding the recursion for Depth-first traversal

**URL:** <https://forums.kodeco.com/t/help-understanding-the-recursion-for-depth-first-traversal/56552>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [November 28, 2018, 11:57pm UTC](https://forums.kodeco.com/t/help-understanding-the-recursion-for-depth-first-traversal/56552 "2018-11-28T23:57:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anothershitdev](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anothershitdev/32/81494_2.png) [@anothershitdev](https://forums.kodeco.com/u/anothershitdev)\
**Post date:** [November 28, 2018, 11:57pm UTC](https://forums.kodeco.com/t/help-understanding-the-recursion-for-depth-first-traversal/56552/1 "2018-11-28T23:57:42Z")

</div>

Can anyone help me understand what exactly is going on through the Depth-first traversal method?

```nohighlight
    public func forEachDepthFirst(visit: (TreeNode) -> Void) {
        visit(self)
        
        children.forEach {
            $0.forEachDepthFirst(visit: visit)
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [December 12, 2018, 5:01am UTC](https://forums.kodeco.com/t/help-understanding-the-recursion-for-depth-first-traversal/56552/2 "2018-12-12T05:01:37Z")

</div>

@jomoka Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jomoka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jomoka/32/109828_2.png) [@jomoka](https://forums.kodeco.com/u/jomoka)\
**Post date:** [December 17, 2018, 2:45pm UTC](https://forums.kodeco.com/t/help-understanding-the-recursion-for-depth-first-traversal/56552/3 "2018-12-17T14:45:06Z")

</div>

@anothershitdev yea for sure!

So keep in mind that:  
**Depth-first traversal** keeps going down one branch till it reaches a leaf node, hence **depth**.  
**Breadth-first traversal** goes down level by level, meaning it traverses all it’s child nodes before it goes down another level, hence **breadth**.

This depth-first traversal function is a little tricky. `forEachDepthFirst` takes in a closure called `visit` which returns a `TreeNode` and returns nothing.

```auto
 public func forEachDepthFirst(visit: (TreeNode) -> Void) {
        visit(self) // 1
        
        children.forEach { // 2
            $0.forEachDepthFirst(visit: visit)
        }
    }
}

```

The method does the following:

1. Executes the `visit` closure. This basically just executes any function that gets passed into `forEachDepthFirst`. For example printing the current node we are on.
2. For every child node this will recursively continue to go down one branch of a tree till it reaches a leaf node. Once it reaches a leaf node, it backtracks and continues down another branch!

If you look at the **example** code as shown below:

```auto
example(of: "depth-first traversal") {
  let tree = makeBeverageTree() // 1
  tree.forEachDepthFirst { print($0.value) } // 2
}

```

1. You first create a binary tree of beverages.
2. You call the `forEachDepthFirst` which passes in a **closure**. A closure is just a function or method. In this case the function just prints out every node during the depth-first traversal. `$0` is just a short hand argument, but it’s type is just `TreeNode`.

Hopefully this helps, let me know if you have any other questions!
