0% found this document useful (0 votes)
1 views9 pages

Programming Assissment5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views9 pages

Programming Assissment5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMMING ASSISSMENT-4

Name: Ujjwal Raj USN: 1NT23CS260

1. a) Construct a binary search tree for the following input.


79, 54 , 66, 22, 89 98, 72, 82, 95, 5, 15, 30
b) Demonstrate how do you search for the key 30 in the binary tree
constructed in part (a)
c) Find the number of comparisons in finding the key 30.
CODE:
A)
79
/ \
54 89
/ \ / \
22 66 82 98
/ \ \ /
5 15 72 95
\
30
B) #include <stdio.h>
#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;


};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);

return root;

struct Node* search(struct Node* root, int key) {

if (root == NULL || root->data == key) {

return root;

if (key > root->data) {

return search(root->right, key);

}
return search(root->left, key);

void searchResult(struct Node* result, int key) {

if (result != NULL) {

printf("Node with key %d found.\n", key);

} else {

printf("Node with key %d not found.\n", key);

int main() {

struct Node* root = NULL;

root = insert(root, 36);

root = insert(root, 22);

root = insert(root, 10);

root = insert(root, 44);

root = insert(root, 42);

root = insert(root, 16);

root = insert(root, 25);

root = insert(root, 3);

root = insert(root, 23);

root = insert(root, 24);

int key1 = 25;

int key2 = 99;

struct Node* result1 = search(root, key1);

searchResult(result1, key1);
struct Node* result2 = search(root, key2);

searchResult(result2, key2);

return 0;

C) #include <stdio.h>
#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);


}

return root;

int search(struct Node* root, int key, int* comparisons) {

if (root == NULL) {

return 0;

(*comparisons)++;

if (root->data == key) {

return 1;

if (key < root->data) {

return search(root->left, key, comparisons);

} else {

return search(root->right, key, comparisons);

void searchResult(struct Node* root, int key) {

int comparisons = 0;

int found = search(root, key, &comparisons);

if (found) {

printf("Node with key %d found after %d comparisons.\n", key, comparisons);

} else {

printf("Node with key %d not found after %d comparisons.\n", key, comparisons);

}
}

int main() {

struct Node* root = NULL;

root = insert(root, 36);

root = insert(root, 22);

root = insert(root, 10);

root = insert(root, 44);

root = insert(root, 42);

root = insert(root, 16);

root = insert(root, 25);

root = insert(root, 3);

root = insert(root, 23);

root = insert(root, 24);

int key1 = 25;

int key2 = 99;

searchResult(root, key1);

searchResult(root, key2);

return 0;

}
2)

ANS:

Preorder Traversal: 1 2 4 8 9 5 10 11 3 6 13 7 14

Inorder Traversal: 8 4 9 2 10 5 11 1 13 6 3 14 7

Postorder Traversal: 8 9 4 10 11 5 2 13 6 14 7 3 1

3. BinarySearchTrees(BSTs) (a)Draw the BST where the data value at


each node is an integer and the values are entered in the following
order: 36,22,10,44,42
(b)Draw the BST after the following insertions have been done in
the tree of part(a): 16,25,3,23, 24 (c)Now draw the tree after
deletions of 42,23 and 22 in this order
(d)Write down the order on which the node values are reached
when the BST of part(c)is traversed (i) in inorder(ii) in postorder(iii)
in preorder
CODE:
A)
36
/ \
22 44
/ \
10 42

B)
36
/ \
22 44
/ \
10 42
/ \
3 16
\
25
\
23
\
24
C)
36
/ \
25 44
/ \
10 42
/ \
3 16
\
24
D)
Inorder: 3 10 16 24 25 36 44
Postorder: 3 16 24 10 25 44 36
Preorder: 36 25 10 3 16 24 44

You might also like