Golang

 

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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)