Binary Tree Traversal in Go: In-order, Pre-order, Post-order Algorithms

 

Introduction

In this tutorial, we will learn how to implement binary tree traversal algorithms in Go programming language. Binary trees are fundamental data structures in computer science. Traversing a binary tree involves visiting all nodes in a specific order. The three primary methods of traversal are:

  • In-order Traversal: Left, Root, Right
  • Pre-order Traversal: Root, Left, Right
  • Post-order Traversal: Left, Right, Root

These algorithms are used in various applications like expression tree evaluation, sorting, and more.

Objective

The objective of this tutorial is to implement a binary tree and perform the three common types of tree traversal (in-order, pre-order, and post-order) in Go programming language.

Go Program for Binary Tree Traversal


package main

import "fmt"

// TreeNode represents a node in the binary tree
type TreeNode struct {
    Value int
    Left  *TreeNode
    Right *TreeNode
}

// In-order traversal (Left, Root, Right)
func inOrderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    inOrderTraversal(root.Left)
    fmt.Print(root.Value, " ")
    inOrderTraversal(root.Right)
}

// Pre-order traversal (Root, Left, Right)
func preOrderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    fmt.Print(root.Value, " ")
    preOrderTraversal(root.Left)
    preOrderTraversal(root.Right)
}

// Post-order traversal (Left, Right, Root)
func postOrderTraversal(root *TreeNode) {
    if root == nil {
        return
    }
    postOrderTraversal(root.Left)
    postOrderTraversal(root.Right)
    fmt.Print(root.Value, " ")
}

// Helper function to create a new TreeNode
func newTreeNode(value int) *TreeNode {
    return &TreeNode{Value: value}
}

func main() {
    // Creating the binary tree
    root := newTreeNode(1)
    root.Left = newTreeNode(2)
    root.Right = newTreeNode(3)
    root.Left.Left = newTreeNode(4)
    root.Left.Right = newTreeNode(5)
    root.Right.Left = newTreeNode(6)
    root.Right.Right = newTreeNode(7)

    fmt.Println("In-order Traversal:")
    inOrderTraversal(root)
    fmt.Println()

    fmt.Println("Pre-order Traversal:")
    preOrderTraversal(root)
    fmt.Println()

    fmt.Println("Post-order Traversal:")
    postOrderTraversal(root)
    fmt.Println()
}

Explanation of the Program Structure

The program consists of the following key components:

  • TreeNode struct: This struct represents a node in the binary tree. It holds an integer value and pointers to the left and right children.
  • Traversal Functions: We implement three traversal algorithms:
    • inOrderTraversal: This function recursively visits the left subtree, then the root, and then the right subtree.
    • preOrderTraversal: This function recursively visits the root, then the left subtree, and then the right subtree.
    • postOrderTraversal: This function recursively visits the left subtree, then the right subtree, and then the root.
  • Helper Function: The newTreeNode function is used to create new nodes in the tree.
  • Binary Tree Creation: In the main function, we create a binary tree by manually setting the left and right children of each node.

How to Run the Program

To run the program:

    1. Ensure that you have Go installed on your system. You can download Go from https://golang.org/dl/.
    2. Save the program to a file, e.g., binary_tree.go.
    3. Open a terminal and navigate to the directory containing the file.
    4. Run the program using the following command:
go run binary_tree.go
  1. The output will display the results of the different tree traversal methods.
© 2025 Learn Programming. All rights reserved.

 

Leave a Reply

Your email address will not be published. Required fields are marked *