> For the complete documentation index, see [llms.txt](https://gofast.disasm.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gofast.disasm.dev/getting-started/depth-first-traversal.md).

# Depth-First Traversal

## Depth-First Traversal in AST

**Depth-first traversal** is a method of exploring a tree structure, such as an Abstract Syntax Tree (AST), by diving deep into each branch before backtracking. In `go-fAST`, this is the default traversal strategy used when implementing custom visitors.

This page walks through what depth-first traversal means in practice, using a JavaScript example and mapping it to the order of AST node visitation.

***

### 📘 What It Means

In a depth-first traversal:

* You visit a node
* Then recursively visit its children
* Then move on to the next sibling

This pattern is ideal for AST processing because it naturally follows the syntactic structure of code.

***

### 📄 JavaScript Example

Here’s a simple JavaScript program:

```javascript
var a = 1;
var b = 2;
function foo(d, e) {
   return d + e;
}
console.log(foo(a, b));
```

***

### 🔍  Traversal Order

The **depth-first traversal** would look like this:

1. **Program** (root)
2. **VariableDeclaration** (var a = 1)
   * **VariableDeclarator**
     * **Identifier** `a`
     * **NumberLiteral** `1`
3. **VariableDeclaration** (var b = 2)
   * **VariableDeclarator**
     * **Identifier** `b`
     * **NumberLiteral** `2`
4. **FunctionDeclaration** `foo`
   * **Identifier** `foo`
   * **ParameterList**
     * **Identifier** `d`
     * **Identifier** `e`
   * **BlockStatement**
     * **ReturnStatement**
       * **BinaryExpression** `d + e`
         * **Identifier** `d`
         * **Identifier** `e`
5. **ExpressionStatement** (console.log(...))
   * **CallExpression**
     * **MemberExpression** `console.log`
       * **Identifier** `console`
       * **Identifier** `log`
     * **Arguments**
       * **CallExpression** `foo(a, b)`
         * **Identifier** `foo`
         * **Identifier** `a`
         * **Identifier** `b`

***

### 💡 Visualization Tip

Imagine the tree as a nested structure:

```
Program
├── VarDecl (a)
│   ├── Identifier
│   └── NumberLiteral
├── VarDecl (b)
│   ├── Identifier
│   └── NumberLiteral
├── FunctionDecl (foo)
│   ├── Identifier
│   ├── Params
│   │   ├── Identifier (d)
│   │   └── Identifier (e)
│   └── Block
│       └── Return
│           └── BinaryExpr (d + e)
│               ├── Identifier
│               └── Identifier
└── ExpressionStmt (console.log)
    └── CallExpr
        ├── MemberExpr (console.log)
        │   ├── Identifier
        │   └── Identifier
        └── CallExpr (foo(a, b))
            ├── Identifier
            ├── Identifier
            └── Identifier
```

***

### ✅ Summary

* go-fAST performs **depth-first traversal** by default via `VisitWith()`
* This means each node is visited before its siblings, and all its children are visited first


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gofast.disasm.dev/getting-started/depth-first-traversal.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
