Data Structure
Lab Submitted
By: Nitesh
Kumar
Roll No.
24PGCS06
Aim: Write a program to insert an element at given position
in linear array
#include <stdio.h>
// Function to insert an element at a given position in an array
void insertElement(int arr[], int n, int pos, int element) {
// Check if position is valid
if (pos < 0 || pos > n) {
printf("Invalid position! Position should be between 0 and %d.\
n", n);
return;
}
// Shift elements to the right to make space for the new element
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
// Insert the new element
arr[pos] = element;
// Print the updated array
printf("Updated array: ");
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5; // Number of elements in the array
int pos = 2; // Position where the new element will be inserted
int element = 10; // New element to be inserted
printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
insertElement(arr, n, pos, element);
return 0;
}
Output:
Original array: 1 2 3 4 5
Updated array: 1 2 10 3 4 5
Aim: 2. Implement Heap data structure
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a heap node
typedef struct HeapNode {
int value;
} HeapNode;
// Define the structure for a heap
typedef struct Heap {
HeapNode* nodes;
int size;
int capacity;
} Heap;
// Function to create a new heap
Heap* createHeap(int capacity) {
Heap* heap = (Heap*)malloc(sizeof(Heap));
heap->nodes = (HeapNode*)malloc(sizeof(HeapNode) *
capacity);
heap->size = 0;
heap->capacity = capacity;
return heap;
}
// Function to swap two nodes in the heap
void swap(HeapNode* node1, HeapNode* node2) {
int temp = node1->value;
node1->value = node2->value;
node2->value = temp;
}
// Function to heapify the heap after insertion
void heapifyUp(Heap* heap, int index) {
if (index && heap->nodes[index].value > heap->nodes[(index -
1) / 2].value) {
swap(&heap->nodes[index], &heap->nodes[(index - 1) / 2]);
heapifyUp(heap, (index - 1) / 2);
}
}
// Function to insert a new node into the heap
void insert(Heap* heap, int value) {
if (heap->size == heap->capacity) {
printf("Heap is full. Cannot insert more elements.\n");
return;
}
heap->nodes[heap->size].value = value;
heap->size++;
heapifyUp(heap, heap->size - 1);
}
// Function to heapify the heap after deletion
void heapifyDown(Heap* heap, int index) {
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < heap->size && heap->nodes[left].value > heap-
>nodes[largest].value) {
largest = left;
}
if (right < heap->size && heap->nodes[right].value > heap-
>nodes[largest].value) {
largest = right;
}
if (largest != index) {
swap(&heap->nodes[index], &heap->nodes[largest]);
heapifyDown(heap, largest);
}
}
// Function to delete the maximum value from the heap
void deleteMax(Heap* heap) {
if (heap->size == 0) {
printf("Heap is empty. Cannot delete more elements.\n");
return;
}
heap->nodes[0].value = heap->nodes[heap->size - 1].value;
heap->size--;
heapifyDown(heap, 0);
}
// Function to print the heap
void printHeap(Heap* heap) {
for (int i = 0; i < heap->size; i++) {
printf("%d ", heap->nodes[i].value);
}
printf("\n");
}
int main() {
Heap* heap = createHeap(10);
insert(heap, 10);
insert(heap, 20);
insert(heap, 30);
insert(heap, 40);
insert(heap, 50);
printf("Heap: ");
printHeap(heap);
deleteMax(heap);
printf("Heap after deletion: ");
printHeap(heap);
return 0;
}
_Output:_
Heap: 50 40 30 10 20
Heap after deletion: 40 20 30 10
Aim: Implement the insertion of AVL trees with rotations.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for an AVL tree node
typedef struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
} Node;
// Function to create a new AVL tree node
Node* createNode(int key) {
Node* node = (Node*)malloc(sizeof(Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return node;
}
// Function to get the height of an AVL tree node
int getHeight(Node* node) {
if (node == NULL) {
return 0;
}
return node->height;
}
// Function to update the height of an AVL tree node
void updateHeight(Node* node) {
node->height = 1 + ((getHeight(node->left)) > (getHeight(node-
>right))) ? (getHeight(node->left)) : (getHeight(node->right));
}
// Function to get the balance factor of an AVL tree node
int getBalanceFactor(Node* node) {
if (node == NULL) {
return 0;
}
return getHeight(node->left) - getHeight(node->right);
}
// Function to perform a left rotation on an AVL tree node
Node* leftRotate(Node* node) {
Node* temp = node->right;
node->right = temp->left;
temp->left = node;
updateHeight(node);
updateHeight(temp);
return temp;
}
// Function to perform a right rotation on an AVL tree node
Node* rightRotate(Node* node) {
Node* temp = node->left;
node->left = temp->right;
temp->right = node;
updateHeight(node);
updateHeight(temp);
return temp;
}
// Function to rebalance an AVL tree after insertion
Node* rebalance(Node* node) {
int balanceFactor = getBalanceFactor(node);
if (balanceFactor > 1) {
if (getBalanceFactor(node->left) < 0) {
node->left = leftRotate(node->left);
}
return rightRotate(node);
} else if (balanceFactor < -1) {
if (getBalanceFactor(node->right) > 0) {
node->right = rightRotate(node->right);
}
return leftRotate(node);
}
return node;
}
// Function to insert a new key into an AVL tree
Node* insert(Node* node, int key) {
if (node == NULL) {
return createNode(key);
}
if (key < node->key) {
node->left = insert(node->left, key);
} else if (key > node->key) {
node->right = insert(node->right, key);
} else {
return node;
}
updateHeight(node);
return rebalance(node);
}
// Function to perform an in-order traversal of an AVL tree
void inOrderTraversal(Node* node) {
if (node == NULL) {
return;
}
inOrderTraversal(node->left);
printf("%d ", node->key);
inOrderTraversal(node->right);
}
int main() {
Node* root = NULL;
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
printf("In-order traversal: ");
inOrderTraversal(root);
printf("\n");
return 0;
}
Output:
In-order traversal: 10 20 25 30 40 50
Aim: Implement B-Tree