Golang Program to Check if a Binary Tree is a Binary Search Tree


A binary tree is a tree having at most two children whereas a binary search tree is the tree in which the elements in the left side of the tree are less than the elements in the right side of the tree. In this article, we will write Go language programs to check if a binary tree is a binary search tree. Here we will use different examples to provide a better understanding of the concept.

Algorithm

  • Step 1 − Create a Node struct with three fields the data of the node of type int, Left and right subtree node of type Node.

  • Step 2 − Create a function is_valid_bstNode with a node, minimum and maximum element as parameters and it returns a value of type bool.

  • Step 3 − In the function return true if the node is null.

  • Step 4 − In this step, return false if the value of the node is less than the minimum element or if the value is greater than the maximum element.

  • Step 5 − If none of the condition is true the function will recursively check the left and right subtrees to make sure they are binary search trees.

  • Step 6 − In this step, create a function named isBinary_search_tree with root node as the input element.

  • Step 7 − Return true if the root node is null otherwise call the is_valid_bstNode with root node and the negative infinity and positive infinity as inputs.

  • Step 8 − In the main, create a tree using left and right nodes of a tree and call the function isBinary_search_tree with root node as parameter.

  • Step 9 − If the output received is true, the binary tree is a binary search treeotherwise it is not.

Example

In this example, the bstNode() function will be used to compare the node element with the maximum and minimum element and the isBinary_search_tree will be used to determine whether the tree is a binary search tree or not by calling the bstNode function.

package main

import (
   "fmt"
)

type Node struct {
   num   int
   Left  *Node
   Right *Node
}

func is_valid_bstNode(node *Node, min int, max int) bool {
   if node == nil {
      return true
   }
   if node.num<= min || node.num>= max {
      return false
   }
   return is_valid_bstNode(node.Left, min, node.num) &&is_valid_bstNode(node.Right, node.num, max)
}

func isBinary_search_tree(root *Node) bool {
   if root == nil {
      return true
   }
   return is_valid_bstNode(root, -999999, 999999)
}

func main() {
	
   root := &Node{num: 40}
   root.Left = &Node{num: 20}
   root.Right = &Node{num: 50}
   root.Left.Left = &Node{num: 10}
   root.Left.Right = &Node{num: 30}

   if isBinary_search_tree(root) {
      fmt.Println("The binary tree is a binary search tree")
   } else {
      fmt.Println("The binary tree is not a binary search tree")
   }
}

Output

The binary tree is a binary search tree

Conclusion

In this article we have looked how we can execute the program of checking if a binary tree is a binary search tree using an example. In that example, we created two functions isBinary_search_tree to check whether a tree is binary search tree and is_valid_bstNode to check if a node a valid binary search tree node or not.

Updated on: 20-Jul-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements