Tree Search

/*
BST: Binary SeaRCh Tree, LC: Left Child, RC: Right Child, TN: Temp Node
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
   int data;
   struct node *LC;
   struct node *RC;
};
struct node *root = NULL;
void insert(int data) {
   struct node *TN = (struct node*) malloc(sizeof(struct node));
   struct node *current;
   struct node *parent;
   TN->data = data;
   TN->LC = NULL;
   TN->RC = NULL;
   if(root == NULL) { //if tree is empty
      root = TN;
   } else {
      current = root;
      parent = NULL;
      while(1) {
         parent = current;
         if(data < parent->data) {//go to left of the tree
            current = current->LC;
            if(current == NULL) {//insert to the left
               parent->LC = TN;
               return;
            }
         }  //go to right of the tree
         else {
            current = current->RC;
            if(current == NULL) {//insert to the right
               parent->RC = TN;
               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->LC;
      }
      //else go to right tree
      else {
         current = current->RC;
      }
      //not found
      if(current == NULL) {
         return NULL;
      }
   }
   return current;
}
int main() {
   int x;
   printf(“Enter tree nodes, -1 for exit.);
   do{
scanf(“%d”,&x);
insert(x);
   }while(x != -1);
   printf(“Enter item to search: “);
   scanf(“%d”,&x);
   struct node * temp = search(x);
   if(temp != NULL) {
      printf(“[%d] Element found.”, temp->data);
      printf(“\n”);
   }else {
      printf(“[ x ] Element not found (%d).\n”, x);
   }
   return 0;
}

Tree Traversal, In-order, Pre-order, Post-order

/*
BST: Binary SeaRCh Tree, LC: Left Child, RC: Right Child, TN: Temp Node, POT: Pre-Order Traversal,IOT: In-Order Traversal,PST: Post-Order Traversal,
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
   int data;
   struct node *LC;
   struct node *RC;
};
struct node *root = NULL;
void insert(int data) {
   struct node *TN = (struct node*) malloc(sizeof(struct node));
   struct node *current;
   struct node *parent;
   TN->data = data;
   TN->LC = NULL;
   TN->RC = NULL;
   if(root == NULL) { //if tree is empty
      root = TN;
   } else {
      current = root;
      parent = NULL;
      while(1) {
         parent = current;
         if(data < parent->data) {//go to left of the tree
            current = current->LC;
            if(current == NULL) {//insert to the left
               parent->LC = TN;
               return;
            }
         }  //go to right of the tree
         else {
            current = current->RC;
            if(current == NULL) {//insert to the right
               parent->RC = TN;
               return;
            }
         }
      }
   }
}
void POT(struct node* root) {
   if(root != NULL) {
      printf(“%d “,root->data);
      POT(root->LC);
      POT(root->RC);
   }
}
void IOT(struct node* root) {
   if(root != NULL) {
      IOT(root->LC);
      printf(“%d “,root->data);
      IOT(root->RC);
   }
}
void PST(struct node* root) {
   if(root != NULL) {
      PST(root->LC);
      PST(root->RC);
      printf(“%d “, root->data);
   }
}
int main() {
   int x;
   printf(“Enter tree nodes, -1 for exit.”);
   scanf(“%d”,&x);
   while(x != -1){
insert(x);
scanf(“%d”,&x);
   }
   printf(“\nPreorder traversal: “);
   POT(root);
   printf(“\nInorder traversal: “);
   IOT(root);
   printf(“\nPost order traversal: “);
   PST(root);
   return 0;
}

C program to implement quicksort algorithm

#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first; i=first; j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last) i++;
while(number[j]>number[pivot]) j–;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;

quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, n, number[25];
printf(“How many elements are u going to enter?: “);
scanf(“%d”,&n);
printf(“Enter %d elements: “, n);

for(i=0;i<n;i++)
scanf(“%d”,&number[i]);

quicksort(number,0,n-1);

printf(“Order of Sorted elements: “);

for(i=0;i<n;i++)
printf(” %d”,number[i]);

return 0;
}

C program to implement binary search

//Binary Search

#include<stdio.h>

int main()
{
int beg,end,mid,item,n,data[20];

printf(“\nPlease Enter Number of elements in an array\n”);
scanf(“%d”, &n);

for(int i = 0; i <n; ++i)
{
scanf(“%d”, &data[i]);
}
printf(“Enter the item to search\n”);
scanf(“%d”,&item);

beg=0;
end=n;
mid=((beg+end)/2);
while((beg<=end)&&(data[mid]!=item))
{
if(data[mid]>item)
end=mid-1;
else
beg=mid+1;
mid=((beg+end)/2);
}

if(data[mid]==item)
printf(“\nBinary search successfully found searching item %d in location %d”,item,mid);
else
printf(“\nData is not in the array”);
return 0;
}

C program to find sum of two matrices of size 3×3

/* C program to find sum of two matrices of size 3×3 */

#include <stdio.h>

int main()
{
int A[3][3];
int B[3][3];
int C[3][3];

int row, col;

/* Input elements in first matrix*/
printf(“Enter elements in matrix A of size 3×3: \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf(“%d”, &A[row][col]);
}
}

/* Input elements in second matrix */
printf(“\nEnter elements in matrix B of size 3×3: \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
scanf(“%d”, &B[row][col]);
}
}
/* C= A +B */
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
C[row][col] = A[row][col] + B[row][col];
}
}

/* Print the value of resultant matrix C */
printf(“\nSum of matrices A+B = \n”);
for(row=0; row<3; row++)
{
for(col=0; col<3; col++)
{
printf(“%d “, C[row][col]);
}
printf(“\n”);
}

return 0;
}

C program to implement matrix multiplication

#include<stdio.h>

//matrix multiplication with demo data

int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2]={{10,11},{20,21},{30,31}};
int c[2][2]={0};
int i,j,k;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<2;i++)
{for(j=0;j<2;j++)
{printf(” %d”,c[i][j]);}
printf(“\n”);
}

}

C Program to Delete an Element from an Array

/* C Program to Delete an Element from an Array */
#include <stdio.h>
int main()
{
int Array[10], Position, i, Size;
printf(“\n Please Enter Number of elements in an array  :   “);
scanf(“%d”, &Size);
printf(“\n Please Enter %d elements of an Array \n”, Size);
for (i = 0; i < Size; i++)
{
    scanf(“%d”, &Array[i]);
    }
  printf(“\n Please Enter a Valid Index Position of a Element that you want to Delete  :  “);
  scanf(“%d”, &Position);
  for (i = Position-1; i <= Size-1; i++)
    {
    Array[i] = Array[i + 1];
    }
    Size–;
  printf(“\n Final Array after Deleteing an Array Elemnt is:\n”);
  for (i = 0; i < Size; i++)
  {
  printf(“%d\t”, Array[i]);
  }
  return 0;
}

C Program to Insert an item into an Array

#include <stdio.h>
int main()
{
  int Array[10], Position, i, Number, Value;
  printf(“\n Number of elements in an array\n”);
  scanf(“%d”, &Number);
  printf(“\n Enter %d elements of an Array \n”, Number);
  for (i = 0; i < Number; i++)
   {
     scanf(“%d”, &Array[i]);
   }
  printf(“\n location to insert\n”);
  scanf(“%d”, &Position);
  printf(“\nvalue to insert\n”);
  scanf(“%d”, &Value);
  for (i = Number – 1; i >= Position – 1; i–)
   {
     Array[i+1] = Array[i];
   }
  Array[Position-1] = Value;
  Number++;
 printf(“\n Final Array after Inserting an  Elemnt is:\n”);
 for (i = 0; i <Number; i++)
  {
  printf(“%d\t”, Array[i]);
  }
 return 0;
}

C program to implement bubble sort

#include<stdio.h>

int main()
{
int temp,data[8]={10,13,14,21,54,12,20,65},pass=1,k=0;

printf(“Before sorting we had\n”);
for(k=0;k<8;k++)
printf(” %d”,data[k]);

for(pass=1;pass<8;pass++)
{
for(k=0;k<(8-pass);k++)
{
if(data[k]>data[k+1])
{
temp=data[k];
data[k]=data[k+1];
data[k+1]=temp;
}
}
}
printf(“\nAfter sorting in ascending order we have\n”);
for(k=0;k<8;k++)
printf(” %d”,data[k]);
return 0;
}