Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output
Question
Question4Max. score: 100.00Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample output
Solution
The question is asking to write a code snippet that will find the minimum and maximum values in a binary search tree (BST). Here is a Python solution:
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def insert(root, key):
if root is None:
return Node(key)
else:
if root.val < key:
root.right = insert(root.right, key)
else:
root.left = insert(root.left, key)
return root
def minValue(node):
current = node
while(current.left is not None):
current = current.left
return current.val
def maxValue(node):
current = node
while(current.right is not None):
current = current.right
return current.val
r = Node(40)
r = insert(r, 20)
r = insert(r, 60)
r = insert(r, 10)
r = insert(r, 30)
r = insert(r, 50)
r = insert(r, 70)
r = insert(r, 5)
r = insert(r, 15)
r = insert(r, 25)
r = insert(r, 35)
r = insert(r, 45)
r = insert(r, 55)
r = insert(r, 65)
r = insert(r, 75)
print("Minimum value is : ", minValue(r))
print("Maximum value is : ", maxValue(r))
This Python code first defines a Node class for the BST, and then defines functions to insert nodes into the tree, and to find the minimum and maximum values. The minimum value is found by traversing the tree to the left as far as possible, and the maximum value is found by traversing to the right as far as possible. The code then creates a BST and inserts the given values, and finally prints the minimum and maximum values.
Similar Questions
Find Minimum and Maximum Values of BSTFind Minimum and MaximumWrite a relevant code snippet to print the maximum and minimum values of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:Minimum is : 5Maximum is : 75Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1Sample outputMinimum is = 5 Maximum is = 75
Question3Max. score: 100.00Level Order of TreeGiven an array of unique elements, construct a Binary Search Tree and print the Level Order of the tree.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.ConstraintsPrint the Level Order of the Binary Search Tree, separate each level by newline.Output Format1 <= N <= 10000 <= ar[i] <= 10000Sample input6 10 5 15 2 7 12Sample output10 5 15 2 7 12
Question6Max. score: 100.00Delete a node in BSTWrite a relevant code snippet to define the deleteNode function of the binary search tree Sample Input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 Sample Output:5 15 10 25 35 30 20 45 55 50 65 75 70 60 40Sample input40 20 60 10 30 50 70 5 15 25 35 45 55 65 75 -1 60Sample outputoutput=5 15 10 25 35 30 20 45 55 50 65 75 70 4
Question1Max. score: 10.00Find a highest sumGiven a binary tree in which each node element contains a number. Find the maximum possible path sum from one special node to another special node.Note: Here speci
Where is the maximum number in a binary search tree? a) At the root of the tree. b) Anywhere other than root and leaf nodes c) At the leftmost leaf node. d) At the rightmost leaf node
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.