0% found this document useful (0 votes)
8 views

DSA File

The document discusses different data structures and algorithms. It includes implementations of binary trees, queues, stacks and various sorting algorithms like bubble sort, insertion sort and selection sort. It also covers searching algorithms like linear search and binary search.

Uploaded by

vivek7840016705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DSA File

The document discusses different data structures and algorithms. It includes implementations of binary trees, queues, stacks and various sorting algorithms like bubble sort, insertion sort and selection sort. It also covers searching algorithms like linear search and binary search.

Uploaded by

vivek7840016705
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Create a binary tree of 2 levels

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a binary tree node

struct node{

int data;

struct node*left;

struct node*right;

};

// Function to create a new node

struct node* createNode(int data){

struct node * n; // Creating a node pointer

n = (struct node*) malloc(sizeof(struct node)); //Allocating memoru in the heap

n->data = data; //Setting the data

n->left = NULL; //Setting the left and right children to NULL

n->right = NULL; //Setting the left and right children to NULL

return n; //Finally returning the created node

int main(){

struct node *p = createNode(1);

struct node *p1 = createNode(2);

struct node *p2 = createNode(3);

struct node *p3 = createNode(4);

struct node *p4 = createNode(5);

struct node *p5 = createNode(6);


struct node *p6 = createNode(7);

//Linking the root with left and right children

p->left = p1;

p->right = p2;

p->left->left = p3;

p->left->right = p4;

p->right->left = p5;

p->right->right = p6;

printf("Binary Tree Structure:\n");

printf(" %d\n", p->data);

printf(" / \\\n");

printf(" %d %d\n", p->left->data, p->right->data);

printf(" / \\ / \\\n");

printf(" %d %d %d %d\n", p->left->left->data, p->left->right->data,

p->right->left->data, p->right->right->data);

return 0;

}
Enqueue and Dequeue Operation

#include<stdio.h>

#include<stdlib.h>

struct queue

int size;

int f;

int r;

int* arr;

};

// For checking Empty

int isEmpty(struct queue *q){

if(q->r==q->f){

return 1;

return 0;

//For checking full

int isFull(struct queue *q){

if(q->r==q->size-1){

return 1;

return 0;

}
//Enqueue

void enqueue(struct queue *q, int val){

if(isFull(q)){

printf("This Queue is full\n");

else{

q->r++;

q->arr[q->r] = val;

printf("Enqued element: %d\n", val);

//Dequeue

int dequeue(struct queue *q){

int a = -1;

if(isEmpty(q)){

printf("This Queue is empty\n");

else{

q->f++;

a = q->arr[q->f];

printf("Dequeued element: %d\n",a);

return a;

//Calling function

int main(){

struct queue q;

q.size = 100;
q.f = q.r = 0;

q.arr = (int*) malloc(q.size*sizeof(int));

// Enqueue few elements

enqueue(&q, 12);

enqueue(&q, 15);

enqueue(&q, 1);

dequeue(&q);

dequeue(&q);

dequeue(&q);

return 0;

}
Circular Queue – Enqueue and Dequeue Operation

#include<stdio.h>

#include<stdlib.h>

struct circularQueue

int size;

int f;

int r;

int* arr;

};

// For checking Empty

int isEmpty(struct circularQueue *q){

if(q->r==q->f){

return 1;

return 0;

//For checking full

int isFull(struct circularQueue *q){

if((q->r+1) %q->size==q->f){

return 1;

return 0;

}
//Enqueue

void enqueue(struct circularQueue *q, int val){

if(isFull(q)){

printf("This Queue is full\n");

else{

q->r = ((q->r+1)%q->size);

q->arr[q->r] = val;

printf("Enqued element: %d\n", val);

//Dequeue

int dequeue(struct circularQueue *q){

int a = -1;

if(isEmpty(q)){

printf("This Queue is empty\n");

else{

q->f++;

a = q->arr[q->f];

printf("Dequeued element: %d\n",a);

return a;

//Calling function

int main(){

struct circularQueue q;

q.size = 100;
q.f = q.r = 0;

q.arr = (int*) malloc(q.size*sizeof(int));

// Enqueue few elements

enqueue(&q, 12);

enqueue(&q, 15);

enqueue(&q, 1);

dequeue(&q);

dequeue(&q);

dequeue(&q);

return 0;

}
Binary tree- Pre, Post and InOrder Traversal

#include <stdio.h>

#include <stdlib.h>

struct node{

int data;

struct node*left;

struct node*right;

};

struct node* createNode(int data){

struct node * n; // Creating a node pointer

n = (struct node*) malloc(sizeof(struct node)); //Allocating memory in the heap

n->data = data; //Setting the data

n->left = NULL; //Setting the left and right children to NULL

n->right = NULL; //Setting the left and right children to NULL

return n; //Finally returning the created node

void preOrder(struct node* root){

if (root!= NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if (root!= NULL){
postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!= NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

int main(){

//Constructing the root node - using function

struct node *p = createNode(1);

struct node *p1 = createNode(2);

struct node *p2 = createNode(3);

struct node *p3 = createNode(5);

struct node *p4 = createNode(4);

//Linking the root with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p2->right = p4;

preOrder(p);
printf("\n");

postOrder(p);

printf("\n");

inOrder(p);

return 0;

}
Bubble Sort

#include <stdio.h>

void printArray(int *A, int n){

for(int i=0; i<n; i++)

printf("%d ", A[i]);

printf("\n");

void bubbleSort(int *A, int n){

int temp;

for(int i=0; i<n-1; i++)// for number of pass

for(int j=0; j<n-1-i; j++)//for comparison of pass

if (A[j]>A[j+1]){

temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

int main(){
int A[] = {7,8,65,45,4};

int n = 5;

printArray(A, n);

bubbleSort(A, n);

printArray(A, n);

}
Insertion Sort

#include<stdio.h>

void printArray(int* A, int n){

for (int i = 0; i < n; i++)

printf("%d ", A[i]);

printf("\n");

void insertionSort(int *A, int n){

int key, j;

// Loop for passes

for (int i = 1; i <= n-1; i++)

key = A[i];

j = i-1;

// Loop for each pass

while(j>=0 && A[j] > key){

A[j+1] = A[j];

j--;

A[j+1] = key;

int main(){

int A[] = {12, 54, 65, 7, 23, 9};


int n = 6;

printArray(A, n);

insertionSort(A, n);

printArray(A, n);

return 0;

}
Selection Sort

#include<stdio.h>

void printArray(int* A, int n){

for (int i = 0; i < n; i++)

printf("%d ", A[i]);

printf("\n");

void selectionSort(int *A, int n){

int indexOfMin, temp;

printf("Running Selection sort...\n");

for (int i = 0; i < n-1; i++)

indexOfMin = i;

for (int j = i+1; j < n; j++)

if(A[j] < A[indexOfMin]){

indexOfMin = j;

// Swap A[i] and A[indexOfMin]

temp = A[i];

A[i] = A[indexOfMin];

A[indexOfMin] = temp;

}
int main(){

int A[] = {3, 5, 2, 13, 12};

int n = 5;

printArray(A, n);

selectionSort(A, n);

printArray(A, n);

return 0;

}
Array

#include <stdio.h>

int main() {

int myArray[5];

myArray[0] = 10;

myArray[1] = 20;

myArray[2] = 30;

myArray[3] = 40;

myArray[4] = 50;

printf("Elements of the array : ");

for (int i = 0; i < 5; ++i) {

printf("%d ", myArray[i]);

return 0;

}
Linear Search

#include <stdio.h>

int linearSearch(int arr[], int size, int element)

for (int i = 0; i < size; ++i) {

if (arr[i] == element) {

return i;

return -1;

int main()

int arr[] = {10, 20, 30, 40, 50};

int size = sizeof(arr) / sizeof(arr[0]);

int element = 30;

int result = linearSearch(arr, size, element);

if (result != -1) {

printf("Element %d found at index %d.\n", element, result);

} else {

printf("Element %d not found in the array.\n", element);

return 0;

}
Binary Search

#include <stdio.h>

int binarySearch(int arr[], int size, int element) {

int low, mid, high;

low = 0;

high = size - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == element) {

return mid;

} else if (arr[mid] < element) {

low = mid + 1;

} else {

high = mid - 1;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int size = sizeof(arr) / sizeof(arr[0]);

int element = 30;

int result = binarySearch(arr, size, element);


if (result != -1) {

printf("Element %d found at index: %d\n", element, result);

} else {

printf("Element %d not found in the array\n", element);

return 0;

}
Push and Pop Operations

#include <stdio.h>

#include <stdlib.h>

struct stack{

int size;

int top;

int * arr;

};

//ptr stands for pointer

int isEmpty(struct stack* ptr){

if(ptr-> top==-1){

return 1;

else{

return 0;

int isFull(struct stack* ptr){

if(ptr->top == ptr->size - 1){

return 1;

}
else{

return 0;

void push(struct stack* ptr, int val){

if (isFull(ptr)){

printf("Stack overflow! cannot push %d to the Stack\n", val);

else{

ptr->top++;

ptr->arr[ptr->top] = val;

int pop(struct stack* ptr){

if (isEmpty(ptr)){

printf("Stack Underflow! cannot pop the stack\n");

return -1;

else{

int val = ptr->arr[ptr->top];

ptr->top--;

return val;

}
int main()

struct stack * sp = (struct stack*) malloc(sizeof(struct stack));

sp->size = 10;

sp->top = -1;

sp->arr = (int * ) malloc(sp->size * sizeof(int));

printf("Stack has been created succesfully\n");

printf("Before pushing Full: %d\n", isFull(sp));

printf("Before pushing Empty: %d\n", isEmpty(sp));

push(sp, 56);

push(sp, 56);

push(sp, 56);

push(sp, 56);

push(sp, 56);

push(sp, 76);

push(sp, 56);

push(sp, 86);

push(sp, 56);

push(sp, 6); // pushed to 10 values

push(sp, 6); //stack overflow since the size of stack is 10

printf("After pushing Full: %d\n", isFull(sp));

printf("After pushing Empty: %d\n", isEmpty(sp));

printf("Poppped %d from the Stack\n", pop(sp));

return 0;

}
Count leaf nodes in a binary tree

#include <stdio.h>

#include <stdlib.h>

// Define the structure for a binary tree node

struct node{

int data;

struct node*left;

struct node*right;

};

// Function to create a new node

struct node* createNode(int data){

struct node * n; // Creating a node pointer

n = (struct node*) malloc(sizeof(struct node)); //Allocating memoru in the heap

n->data = data; //Setting the data

n->left = NULL; //Setting the left and right children to NULL

n->right = NULL; //Setting the left and right children to NULL

return n; //Finally returning the created node

// funnction to count the leaf nodes in a binary tree

int countleafNodes (struct node* Z){

//If the tree is Empty

if(Z == NULL){

return 0;

}
///If the node is a leaf node

if (Z->left == NULL && Z->right == NULL){

return 1;

//Recursively count leaf nodes in the left and right subtrees

return countleafNodes(Z->left) + countleafNodes(Z->right);

int main(){

//Constructing the root node - using function

struct node *Z = createNode(1);

struct node *Z1 = createNode(2);

struct node *Z2 = createNode(3);

struct node *Z3 = createNode(4);

struct node *Z4 = createNode(5);

struct node *Z5 = createNode(6);

struct node *Z6 = createNode(7);

//Linking the root with left and right children

Z->left = Z1;

Z->right = Z2;

Z->left->left = Z3;

Z->left->right = Z4;

Z->right->left = Z5;

Z->right->right = Z6;

int leafNodeCount = countleafNodes(Z);


printf("Number of leaf nodes in the binary tree: %d\n", leafNodeCount);

return 0;

You might also like