Golang program to find the depth of a node in a binary search tree


In this Golang article, we are going to find the depth of a node in a binary search tree using recursion and iterative method. The binary search tree is a useful data structure for efficient searching, insertion, and deletion of elements. A binary search tree (BST) is a type of binary tree where every node has at most two children, commonly referred to as the left child and the right child.

Syntax

func (n *Node) Depth(value int) int {…}

The Depth() function is used to find the depth of a node in a binary search tree. It takes an argument as integer value.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, initialize a node struct and assign three variables in it. The first variable stores the integer value, while the second and third pointer variable stores the address to the left and right node respectively.

  • Step 3 − Now, create an insert() function that takes a node and a value to insert. This function recursively inserts the value into the appropriate binary search tree.

  • Step 4 − This function recursively/iterative searches for the appropriate position to insert the new node based on the binary search tree property.

  • Step 5 − Now, define a function called Depth(). It is used to find the depth of a node in a binary search tree. This function takes a integer value as input and returns the depth of the node with the given value.

  • Step 6 − The Depth() function uses recursion to search for the node with the given value and calculates the depth of the node as it traverses the tree.

  • Step 7 − Start the main() function. Inside the main() function, insert several nodes into the Binary Search tree.

  • Step 8 − Now, calls the Depth() function and pass the integer value as argument to the function.

  • Step 9 − Further, the depth of a node in binary search tree is printed on the screen by using the fmt.Println() function.

Example 1

In this example, we will define a Depth() function using recursion that is used to find the depth of a node in a binary search tree. The depth of a node is defined as the number of edges from the root to the node.

package main

import (
   "fmt"
)

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

func (n *Node) Insert(value int) *Node {
   if n == nil {
      return &Node{value, nil, nil}
   }

   if value < n.value {
      n.left = n.left.Insert(value)
   } else {
      n.right = n.right.Insert(value)
   }
   return n
}

func (n *Node) Depth(value int) int {
   if n == nil {
      return -1
   }

   if value == n.value {
      return 0
   }

   if value < n.value {
      return n.left.Depth(value) + 1
   }
   return n.right.Depth(value) + 1
}

func main() {
   root := &Node{5, nil, nil}
   root.Insert(5).Insert(3).Insert(7).Insert(2).Insert(4)

   fmt.Println(root.Depth(5))
   fmt.Println(root.Depth(7))
}

Output

0
2

Example 2

In this example, we will define a Depth() function using iterative approach that is used to find the depth of a node in a binary search tree. The depth of a node is defined as the number of edges from the root to the node.

package main

import (
   "fmt"
)

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

func (n *Node) Insert(value int) {
   if n == nil {
      return
   }
   if value < n.value {
      if n.left == nil {
         n.left = &Node{value: value}
      } else {
         n.left.Insert(value)
      }
   } else {
      if n.right == nil {
         n.right = &Node{value: value}
      } else {
         n.right.Insert(value)
      }
   }
}

func (n *Node) Depth(value int) int {
   depth := 0
   for n != nil {
      if n.value == value {
         return depth
      } else if value < n.value {
         n = n.left
      } else {
         n = n.right
      }
      depth++
   }
   return -1
}

func main() {
   root := &Node{value: 5}
   root.Insert(5)
   root.Insert(8)
   root.Insert(3)
   root.Insert(4)
   root.Insert(2)
   fmt.Println(root.Depth(8)) 
   fmt.Println(root.Depth(5)) 
   fmt.Println(root.Depth(2)) 
}

Output

2
0
2

Conclusion

We have successfully compiled and executed a go language program to find the depth of a node in a binary search tree using recursion and iterative method along with two examples. In the first example, we have used the recursive method and in the second example, we have used the iterative method. The resultant depth of the node in a BST is printed to the console as Output.

Updated on: 03-Apr-2023

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements