1) Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers
Aim: To create a binary tree of integers
Program:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
int main()
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
getchar();
return 0;
Expected output:
ii) Traversing the above binary tree in preorder, inorder and postorder.
Aim: Traversing the above binary tree in preorder, inorder and postorder
Program :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
//go to left of the tree
if(data < parent->data) {
current = current->leftChild;
//insert to the left
if(current == NULL) {
parent->leftChild = tempNode;
return;
} //go to right of the tree
else {
current = current->rightChild;
//insert to the right
if(current == NULL) {
parent->rightChild = tempNode;
return;
struct node* search(int data) {
struct node *current = root;
printf("Visiting elements: ");
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//go to left tree
if(current->data > data) {
current = current->leftChild;
//else go to right tree
else {
current = current->rightChild;
//not found
if(current == NULL) {
return NULL;
return current;
void pre_order_traversal(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
void post_order_traversal(struct node* root) {
if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild)
printf("%d ", root->data);
int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };
for(i = 0; i < 7; i++)
insert(array[i]);
i = 31;
struct node * temp = search(i);
if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
i = 15;
temp = search(i);
if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
printf("\nPreorder traversal: ");
pre_order_traversal(root);
printf("\nInorder traversal: ");
inorder_traversal(root);
printf("\nPost order traversal: ");
post_order_traversal(root);
return 0;
Expected output:
Visiting elements: 27 35 [31] Element found.
Visiting elements: 27 14 19 [ x ] Element not found (15).
Preorder traversal: 27 14 10 19 35 31 42
Inorder traversal: 10 14 19 27 31 35 42
Post order traversal: 10 19 14 31 42 35 27
2)Write C programs that use both recursive and non-recursive functions to perform the
following searching operations for a key value in a given list of integers:
i) Linear search
Aim: To perfrom linear search
Program:
#include <stdio.h>
int main()
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
printf("%d is present at location %d.\n", search, c+1);
break;
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
output:
Enter number of elements in array 4
Enter 4 integer(s)
Enter a number to search 5
5 is present at location 2
ii) Binary search
Aim: To perform binary search
Program:
#include <stdio.h>
int main()
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
else
last = middle - 1;
middle = (first + last)/2;
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
output:
Enter number of elements in array 4
Enter 4 integer(s)
Enter a number to search 5
5 is present at location 2
3)Write a C program that implements the following sorting methods to sort a given list
of integers in ascending order
i) Bubble sort
Aim: To perform bubble sort
Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
for (d = 0 ; d < n - c - 1; d++)
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
output:
Enter number of elements 4
Enter 4 integers
Sorted list in ascending order:
3
5
ii) Selection sort
Aim: To perform selection sort
Program:
#include <stdio.h>
int main()
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
position = c;
for (d = c + 1; d < n; d++)
if (array[position] > array[d])
position = d;
if (position != c)
t = array[c];
array[c] = array[position];
array[position] = t;
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
output:
Enter number of elements 4
Enter 4 integers
Sorted list in ascending order:
ii) Selection sort
Aim: To perform selection sort
Program:
#include <stdio.h>
int main()
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
position = c;
for (d = c + 1; d < n; d++)
if (array[position] > array[d])
position = d;
if (position != c)
t = array[c];
array[c] = array[position];
array[position] = t;
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
output:
Enter number of elements 4
Enter 4 integers
Sorted list in ascending order:
(iii) Insertion sort
Aim:to perform insertion sort
Program:
#include <stdio.h>
int main()
int n, array[1000], c, d, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
t = array[c];
for (d = c - 1 ; d >= 0; d--) {
if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
else
break;
if (flag)
array[d+1] = t;
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
return 0;
output:
Enter number of elements 4
Enter 4 integers
Sorted list in ascending order: