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
  • Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserial...

    Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test should pass: node = Node('root', Node('left', Node('left.left')), Node('right')) assert deserialize(serialize(node)).left.left.val == 'left.left'

  • in PYTHON   create a binary search tree that has the minimum possible height (i.e., is as balanced...

    in PYTHON   create a binary search tree that has the minimum possible height (i.e., is as balanced as possible). You're given the elements in sorted order. Here are some example inputs and the minimum possible height of the resulting BST: [0] 0 [-3, -2, -1] 1 [-3, 0, 1, 3, 4] 2 [-9, -6, -5, -3, -2, 1, 2, 4, 8] 3 [-19, -17, -15, -12, -11, -9, -7, -1, 1, 4, 5, 7, 8, 9, 11, 12, 13, 14, 18]...

  • PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in...

    PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in an arbitrary node of a BST, and return the value of the predecessor of the given node. Code : class Node: def __init__(self, value): self.value = value self.left = None self.right = None    def predecessor(node): # TODO

  • Can you complete this program using same code please. DO not change any code. Thank you...

    Can you complete this program using same code please. DO not change any code. Thank you In this problem you are given the root of a BST and you want to return the summation of all the values in the BST. Remember that the summation of a BST is the summation of all the nodes in the left subtree + the summation of all the nodes in the right subtree + the value of the current node Expected behavior: root...

  • Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The nat...

    Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...

  • python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be...

    python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in...

  • Python question. How do i make this code work? im trying to print the value of...

    Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value)    def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

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