Write a program to implement Binary Search tree
CORRECT
Student: Gugulothu Vaishnavi Difficulty: EASY
23
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
4 // Structure for a node in the Binary Search Tree
22
22
22
27
5 struct Node {
7
7
@
6 int data;
gm
gm
gm
gm
7 struct Node* left;
ai
ai
ai
ai
8 struct Node* right;
l.c
l.c
l.c
l.c
9 };
om
om
om
om
10
11 // Function to create a new node
23
23
23
12 struct Node* createNode(int data) {
ag
ag
ag
13 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
1a
1a
1a
1
14 newNode->data = data;
22
22
22
27
15 newNode->left = newNode->right = NULL;
7@
7@
7@
@
16 return newNode;
gm
gm
gm
gm
17 }
ai
ai
ai
ai
18
l.
l.
l.
l.
co
co
co
co
19 // Function to insert a node into the BST
m
20 struct Node* insert(struct Node* root, int data) {
Case Output Errors Time Memory Status
#1 5 10 15 20 25 30 No errors 0.007s 1MB PASSED
-1
#2 10 20 30 40 50 No errors 0.008s 1MB PASSED
3
Language: C (GCC 9.2.0) | Generated: 12/13/2024, 7:50:56 PM