C Programs
C Programs
Heap Sort
---------------
#include <stdio.h>
int main() {
int arr[] = {4, 10, 3, 5, 1}, n = 5;
heapSort(arr, n);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}
struct Node {
int data;
struct Node *left, *right;
};
int main() {
struct Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
inorder(root);
return 0;
}
int hashTable[SIZE];
void initHashTable() {
for (int i = 0; i < SIZE; i++)
hashTable[i] = -1; // -1 indicates empty
}
if (i < SIZE) {
hashTable[(index + i) % SIZE] = key;
} else {
printf("Hash table is full\n");
}
}
void display() {
printf("Hash Table:\n");
for (int i = 0; i < SIZE; i++)
printf("Index %d: %d\n", i, hashTable[i]);
}
int main() {
initHashTable();
int keys[] = {23, 43, 13, 27, 34};
int n = sizeof(keys) / sizeof(keys[0]);
display();
return 0;
}
2.2 Preorder Traversal (Recursive)
-----------------------------------
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);
root->left->right = createNode(50);
struct Result {
int min;
int max;
};
// If two elements
if (high == low + 1) {
if (arr[low] < arr[high]) {
result.min = arr[low];
result.max = arr[high];
} else {
result.min = arr[high];
result.max = arr[low];
}
return result;
}
// Combine results
result.min = (leftResult.min < rightResult.min) ? leftResult.min :
rightResult.min;
result.max = (leftResult.max > rightResult.max) ? leftResult.max :
rightResult.max;
return result;
}
int main() {
int arr[] = {100, 11, 445, 1, 330, 3000};
int n = sizeof(arr) / sizeof(arr[0]);
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
push(root);
while (!isEmpty()) {
struct Node* current = pop();
printf("%d ", current->data);
if (current->right)
push(current->right);
if (current->left)
push(current->left);
}
}
int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);
root->left->right = createNode(50);
#define SIZE 10
// Hash table
struct Node* hashTable[SIZE];
// Hash function
int hash(int key) {
return key % SIZE;
}
// Insert into hash table with chaining
void insert(int key) {
int index = hash(key);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = key;
newNode->next = NULL;
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
// Insert at the beginning for simplicity
newNode->next = hashTable[index];
hashTable[index] = newNode;
}
}
int main() {
// Initialize table
for (int i = 0; i < SIZE; i++)
hashTable[i] = NULL;
// Example keys
int keys[] = {15, 25, 35, 20, 30, 10, 40};
int n = sizeof(keys) / sizeof(keys[0]);
display();
return 0;
}
4.2. Preorder Traversal (Recursive Procedure)
----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* root = createNode(10);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(40);
root->left->right = createNode(50);
if (arr[mid] == key)
return mid;
else if (key < arr[mid])
return binarySearch(arr, low, mid - 1, key);
else
return binarySearch(arr, mid + 1, high, key);
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 50;
if (index != -1)
printf("Element %d found at index %d\n", key, index);
else
printf("Element %d not found\n", key);
return 0;
}
5.2. Postorder Traversal (Non-Recursive Procedure)
----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int isEmpty1() {
return top1 == -1;
}
push1(root);
while (!isEmpty1()) {
struct Node* curr = pop1();
push2(curr);
if (curr->left)
push1(curr->left);
if (curr->right)
push1(curr->right);
}
int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
i = j = 0;
k = left;
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
int isEmpty() {
return top == -1;
}
current = pop();
printf("%d ", current->data);
current = current->right;
}
}
int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);