BSTWrite a program to insert elements into a Binary Search Tree (BST). The program should allow the user to input elements until they enter -1 to stop. After the insertion, print the inorder traversal of the BST.Constraints:NAExample:Sample Input:5030702040-1Sample Output:20 30 40 50 70
Question
BSTWrite a program to insert elements into a Binary Search Tree (BST). The program should allow the user to input elements until they enter -1 to stop. After the insertion, print the inorder traversal of the BST.Constraints:NAExample:Sample Input:5030702040-1Sample Output:20 30 40 50 70
Solution
Here is a Python program that creates a Binary Search Tree (BST) and performs an inorder traversal:
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 inorder(root):
if root:
inorder(root.left)
print(root.val),
inorder(root.right)
r = Node(50)
r = insert(r, 30)
r = insert(r, 20)
r = insert(r, 40)
r = insert(r, 70)
print("Inorder traversal of the given tree is:")
inorder(r)
This program first creates a root node with value 50. Then it inserts 30, 20, 40, and 70 into the tree. Finally, it prints the inorder traversal of the tree.
Please note that this program does not take user input. If you want to take user input, you can modify the program as follows:
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 inorder(root):
if root:
inorder(root.left)
print(root.val),
inorder(root.right)
r = Node(int(input("Enter root node value: ")))
while True:
val = int(input("Enter value to insert (-1 to stop): "))
if val == -1:
break
else:
r = insert(r, val)
print("Inorder traversal of the given tree is:")
inorder(r)
This program first asks the user to enter the root node value. Then it repeatedly asks the user to enter a value to insert into the tree until the user enters -1. Finally, it prints the inorder traversal of the tree.
Similar Questions
Deletion in a Binary Search TreeWrite a program to delete an element from a Binary Search Tree (BST). The program should first allow the user to input elements to construct the BST until they enter -1 to stop. Then, it should prompt the user for an element to delete and print the inorder traversal of the BST after deletion.Constraints:NAExample:Sample Input:5030702040-130Sample Output:
Level 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
A binary search tree is generated by inserting following integers: 41, 30, 60, 25, 78, 90, 39, 50The number of nodes in the left subtree and right subtree of the root respectivelyA3, 4B5, 3C3, 5D4, 4
How to insert a new node to Binary Search Tree? a. Insert a new node as the last element of the last level and call heapify() method b. Starting from the root go to the right or left child of each node according to the new node`s key recursively till it finds an empty position c. Place a new node as the root of BST and set a previous root as a right child of a new root d. None of the above e. Starting from the root go to the right or left child of each node according to the new node`s key recursively till it finds a node with the same key
Find preorder traversal of height balanced BSTGiven a sorted array. Convert it into a Height balanced Binary Search Tree (BST). Find the preorder traversal of height balanced BST. If there exist many such balanced BST consider the tree whose preorder is lexicographically smallest.Height balanced BST means a binary tree in which the depth of the left subtree and the right subtree of every node never differ by more than 1.Sample input25 1 4 5 7 8 12 14 15 19 20 22 25 26 27 28 29 33 34 37 40 41 42 43 47 48Sample outputoutput=26 12 5 1 4 7 8 19 14 15 22 20 25 37 2
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.