Question

Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the...

Can you help me with python question:

Implement the function isBinarySearchTree(root), which returns True if the binary tree passed to it is a valid binary search tree.

I've provided a simple TreeNode class, and the beginnings of isBinarySearchTree(root).

class TreeNode:
'''Node for a simple binary tree structure'''
def __init__(self, value, left, right):
self.value = value
self.left = left
self.right = right


  
def isBinarySearchTree(tree):

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

# Python program to check if a binary tree is BinarySearchTree or not

INT_MAX = 9999999999     #instalize maximum  and minimum values

INT_MIN = -9999999999

class TreeNode:          # This class is to assign data to node

  def __init__(self, value):

    self.data = value

    self.left = None

    self.right = None

def isBinarySearchTree(node, mini, maxi):      # method is used to find binary tree or not

  if node is None:

    return True

  if node.data < mini or node.data > maxi:     # this condition is to check data is in right order or not

    return False

  # this is for recursive calling of both left and right subtree of a tree

  return (isBinarySearchTree(node.left, mini, node.data -1) and isBinarySearchTree(node.right, node.data+1, maxi))

# Here we gave inputs to the trees

root = TreeNode(4)

root.left = TreeNode(2)

root.right = TreeNode(5)

root.left.left = TreeNode(1)

root.left.right = TreeNode(3)

isBinarySearchTree(root,INT_MIN, INT_MAX)    # here we calling the function to check wether it is binary search tree or not

Add a comment
Know the answer?
Add Answer to:
Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT