write_a_program_to_implement_binary_search_tree
write_a_program_to_implement_binary_search_tree
CORRECT
23
23
ag
ag
ag
Question Description
1a
1a
1a
12
12
12
Problem Statement:
27
27
27
27
Write a program to implement Binary Search tree
@
@
Description: A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorte
gm
gm
gm
gm
Input Format:
ai
ai
ai
ai
l.c
l.c
l.c
l.c
The first input is an integer n, representing the number of elements to insert into the BST.
om
om
om
om
The next n integers represent the elements to insert into the BST.
The last input is the key to search for in the BST.
23
23
23
Output Format:
ag
ag
ag
The in-order traversal of the BST, showing elements in sorted order.
1a
1a
1a
Print the index of the search element or -1 if it's not found.
12
12
12
Sample Input:
27
27
27
27
5
@
@
30
gm
gm
gm
gm
10
ai
ai
ai
ai
l.c
l.c
l.c
l.c
20
om
om
om
om
40
50
23
23
23
40
ag
ag
ag
Sample Output:
1a
1a
1a
10 20 30 40 50
12
12
12
3
27
27
27
27
Explanation:
@
@
The elements [30, 10, 20, 40, 50] are inserted in the BST, and an in-order traversal yields the sorted order 10 20 30 40 50.
gm
gm
gm
gm
The key 40 is found in the BST at index 3.
ai
ai
ai
ai
l.
l.
l.
l.
co
co
co
co
Your Solution
m
m
1 #include <stdio.h>
23
23
23
2 #include <stdlib.h>
ag
ag
ag
3
1
1
a1
a1
a1
22
22
27
5 struct Node {
7
7
@
6 int data;
gm
gm
gm
gm
ai
ai
ai
l.c
l.c
l.c
9 };
om
om
om
om
10
11 // Function to create a new node
23
23
23
ag
ag
1a
1a
1
14 newNode->data = data;
22
22
22
27
7@
7@
@
16 return newNode;
gm
gm
gm
gm
17 }
ai
ai
ai
ai
18
l.
l.
l.
l.
co
co
co
co