Golang Program to Print The Height of a Binary Tree


A binary tree is a tree which has at most two children and height refers to the no. of levels in the tree. In this article, we will use two examples to find the height of a binary tree. In this Golang article we will write programs to print the height of a binary tree.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

funclen(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • Step 1 − Create a Node struct and in that struct create three fields left and right of type node and data of type int.

  • Step 2 − Create a function get_height with the root node as the parameter.

  • Step 3 − In the function, check if the root node is null return 0.

  • Step 4 − If the condition is not true, find the height of the left subtree recursively using root.left.

  • Step 5 − Similarly, find the height of right subtree recursively using root.right.

  • Step 6 − Then, use if conditional check if left height is greater than the right height returnleft height+1 where 1 is for root element.

  • Step 7 − But, if left height is less than right height return right height+1.

  • Step 8 − In the main, create a tree but setting the data in the left and right subtree.

  • Step 9 − Call the function get_height with root as input parameter to calculate the height of tree.

Example 1

In this example, the left and right subtree will be called recursively to calculate the height of the tree. The tree will be created and the data will be set in the left and right subtree.

package main

import "fmt"

type Node struct {
   data  int
   left  *Node
   right *Node
}

func get_height(root *Node) int {
   if root == nil {
      return 0
   } else {
      left_height := get_height(root.left)
      right_height := get_height(root.right)

      if left_height>right_height {
         return left_height + 1
      } else {
         return right_height + 1
      }
   }
}

func main() {
	
   root := &Node{
      data: 10,
      left: &Node{
         data: 20,
         left: &Node{
            data:  40,
            left:  nil,
            right: nil,
         },
         right: &Node{
            data:  50,
            left:  nil,
            right: nil,
         },
      },
      right: &Node{
         data:  30,
         left:  nil,
         right: nil,
      },
   }

   fmt.Println("Height of the binary tree is:", get_height(root))
}

Output

Height of the binary tree is: 3

Example 2

In this example, queue will be used to add nodes in the current level of the tree i.e the left and the right subtree. The nodes then later be dequeued and height of the tree will be returned.

package main

import "fmt"

type Node struct {
   data  int
   left  *Node
   right *Node
}

func get_height(root *Node) int {
   if root == nil {
      return 0
   }

   var v []*Node
   var level_size int
   height := 0

   v = append(v, root)

   for len(v) > 0 {
		
      level_size = len(v)
		
      height++
		
      for i := 0; i<level_size; i++ {
         node := v[0]
         v = v[1:]

         if node.left != nil {
            v = append(v, node.left)
         }
         if node.right != nil {
            v = append(v, node.right)
         }
      }
   }

   return height
}

func main() {
	
   root := &Node{
      data: 10,
      left: &Node{
         data: 20,
         left: &Node{
            data:  40,
            left:  nil,
            right: nil,
         },
         right: &Node{
            data:  50,
            left:  nil,
            right: nil,
         },
      },
      right: &Node{
         data:  30,
         left:  nil,
         right: nil,
      },
   }
	
   fmt.Println("Height of the binary tree is:", get_height(root))
}

Output

Height of the binary tree is: 3

Conclusion

In this article we have looked at how we can execute the program of printing the height of a binary tree using two examples. In the first example we recursively called the left and right subtree to calculate the height of the tree and in the second example, we used a queue to calculate height.

Updated on: 20-Jul-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements