0% found this document useful (0 votes)
6 views11 pages

dsa 9 10 11

Uploaded by

karthika20805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

dsa 9 10 11

Uploaded by

karthika20805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <stdio.

h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>

struct node {
int key;
struct node *left, *right, *parent;
int dist;
};

struct node *root = NULL;

struct node *createNode(int val) {


struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
temp->key = val;
temp->dist = 1;
return temp;
}

void inorder(struct node *r) {


struct node *stack[100];
int top = -1;
struct node *current = r;

while (current != NULL || top != -1) {


while (current != NULL) {
stack[++top] = current;
current = current->left;
}
current = stack[top--];
printf("%d ", current->key);
current = current->right;
}
}

int calculateDistance(struct node *node) {


int leftDist = 0, rightDist = 0;
if (node->left) {
leftDist = node->left->dist;
}
if (node->right) {
rightDist = node->right->dist;
}

if (leftDist < rightDist) {


return 1 + leftDist;
} else {
return 1 + rightDist;
}
}

bool isMinHeap(struct node *root) {


if (!root) return true;

struct node *queue[100];


int front = 0, rear = 0;
queue[rear++] = root;

while (front < rear) {


struct node *node = queue[front++];
if (node->left) {
if (node->left->key < node->key) return false;
queue[rear++] = node->left;
}
if (node->right) {
if (node->right->key < node->key) return false;
queue[rear++] = node->right;
}
}
return true;
}

bool isLeftistTree(struct node *root) {


if (!root) return true;

struct node *queue[100];


int front = 0, rear = 0;
queue[rear++] = root;

while (front < rear) {


struct node *node = queue[front++];
int leftDist = INT_MAX, rightDist = INT_MAX;
if (node->left) {
leftDist = node->left->dist;
}
if (node->right) {
rightDist = node->right->dist;
}

if (leftDist < rightDist) return false;


if (node->left) queue[rear++] = node->left;
if (node->right) queue[rear++] = node->right;
}
return true;
}

bool isLeftistMinHeap(struct node *root) {


return isMinHeap(root) && isLeftistTree(root);
}

void insert(int val) {


struct node *r;
struct node *newNode = createNode(val);
newNode->dist = 1;

if (root == NULL) {
root = newNode;
return;
}

r = root;
while (1) {
if (r->key > val) {
if (r->left) {
r = r->left;
} else {
r->left = newNode;
newNode->parent = r;
break;
}
} else {
if (r->right) {
r = r->right;
} else {
r->right = newNode;
newNode->parent = r;
break;
}
}
}

struct node *current = newNode;


while (current != NULL) {
current->dist = calculateDistance(current);
current = current->parent;
}
}

int main() {
int a[10] = {32, 23, 16, 35, 33, 71, 65, 20, 18, 24};
for (int i = 0; i < 10; i++) {
insert(a[i]);
}

if (isLeftistMinHeap(root))
printf("The tree is a Leftist Min Heap.\n");
else
printf("The tree is not a Leftist Min Heap.\n");

inorder(root);
return 0;
}
*****************************************************
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>

struct node {
int key;
struct node *left, *right, *parent;
int dist;
};

struct node *createNode(int val) {


struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
temp->key = val;
temp->dist = 1;
return temp;
}
int calculateDistance(struct node *node) {
int leftDist = 0, rightDist = 0;
if (node->left) {
leftDist = node->left->dist;
}
if (node->right) {
rightDist = node->right->dist;
}
if (leftDist < rightDist) {
return 1 + leftDist;
} else {
return 1 + rightDist;
}
}

struct node* merge(struct node* h1, struct node* h2) {


if (!h1) return h2;
if (!h2) return h1;

if (h1->key > h2->key) {


struct node* temp = h1;
h1 = h2;
h2 = temp;
}

struct node *queue[100];


int front = 0, rear = 0;

queue[rear++] = h1;
struct node *current = h1;

while (h2) {
if (!current->right) {
current->right = h2;
h2->parent = current;
current = h2;
h2 = NULL;
} else {
queue[rear++] = current->right;
current->right = h2;
h2->parent = current;
current = h2;
h2 = h2->right;
current->right = NULL;
}

if (current->left == NULL || calculateDistance(current->left) <


calculateDistance(current->right)) {
struct node* temp = current->left;
current->left = current->right;
current->right = temp;
}

current->dist = calculateDistance(current);
if (front < rear) {
current = queue[front++];
}
}
return h1;
}

void insert(struct node** root, int val) {


struct node* newNode = createNode(val);
*root = merge(*root, newNode);
}

void inorder(struct node *r) {


struct node *stack[100];
int top = -1;
struct node *current = r;

while (current != NULL || top != -1) {


while (current != NULL) {
stack[++top] = current;
current = current->left;
}
current = stack[top--];
printf("%d ", current->key);
current = current->right;
}
}

int main() {
struct node *root1 = NULL, *root2 = NULL;

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


int arr2[] = {15, 25, 35, 45, 55};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);

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


insert(&root1, arr1[i]);
}
for (int i = 0; i < size2; i++) {
insert(&root2, arr2[i]);
}

struct node* mergedRoot = merge(root1, root2);

printf("Merged Leftist Min Heap Tree (inorder traversal): ");


inorder(mergedRoot);
printf("\n");

return 0;
}
********************************************************************
#include <stdio.h>
#include <stdlib.h>

// Definition of a 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* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to perform Breadth-First Traversal (BFT)


void breadthFirstTraversal(struct Node* root) {
if (!root) return;

struct Node* queue[100];


int front = 0, rear = 0;

queue[rear++] = root;

while (front < rear) {


struct Node* node = queue[front++];

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

if (node->left) queue[rear++] = node->left;


if (node->right) queue[rear++] = node->right;
}
}

// Iterative function to perform Inorder Depth-First Traversal (DFT)


void iterativeInorderTraversal(struct Node* root) {
struct Node* stack[100];
int top = -1;
struct Node* current = root;

while (current != NULL || top != -1) {


while (current != NULL) {
stack[++top] = current;
current = current->left;
}
current = stack[top--];
printf("%d ", current->data);
current = current->right;
}
}

int main() {
// Create a sample tree:
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
printf("Breadth-First Traversal: ");
breadthFirstTraversal(root);
printf("\n");

printf("Iterative Inorder Depth-First Traversal: ");


iterativeInorderTraversal(root);
printf("\n");

return 0;
}
****************************************************
#include <stdio.h>
#include <stdlib.h>

struct Node {
int vertex;
struct Node* next;
};

struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};

struct Stack {
int* array;
int top;
int max;
};

// Function to create a node


struct Node* createNode(int v) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}

// Function to create a graph


struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = vertices;

graph->adjLists = (struct Node**)malloc(vertices * sizeof(struct Node*));


graph->visited = (int*)malloc(vertices * sizeof(int));

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


graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
}

// Function to add edge


void addEdge(struct Graph* graph, int src, int dest) {
// Add edge from src to dest
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
}

// Function to create a stack


struct Stack* createStack(int max) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->array = (int*)malloc(max * sizeof(int));
stack->top = -1;
stack->max = max;
return stack;
}

// Stack operations
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}

void push(struct Stack* stack, int item) {


stack->array[++stack->top] = item;
}

int pop(struct Stack* stack) {


if (!isEmpty(stack))
return stack->array[stack->top--];
return -1;
}

// DFS helper function to perform topological sort


void topologicalSortUtil(struct Graph* graph, int vertex, struct Stack* stack) {
graph->visited[vertex] = 1;

struct Node* temp = graph->adjLists[vertex];


while (temp != NULL) {
int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {
topologicalSortUtil(graph, connectedVertex, stack);
}

temp = temp->next;
}

push(stack, vertex);
}

// Function to perform topological sort


void topologicalSort(struct Graph* graph) {
struct Stack* stack = createStack(graph->numVertices);

for (int i = 0; i < graph->numVertices; i++) {


if (graph->visited[i] == 0) {
topologicalSortUtil(graph, i, stack);
}
}

while (!isEmpty(stack)) {
printf("%d ", pop(stack));
}
printf("\n");
}

int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 5, 2);
addEdge(graph, 5, 0);
addEdge(graph, 4, 0);
addEdge(graph, 4, 1);
addEdge(graph, 2, 3);
addEdge(graph, 3, 1);

printf("Topological Sort of the given graph: \n");


topologicalSort(graph);

return 0;
}
**************************************************************
#include <stdio.h>
#include <stdlib.h>

#define TABLE_SIZE 10

// Node for simple chaining


struct Node {
int key;
struct Node* next;
};

// Hash table for simple chaining


struct Node* chain[TABLE_SIZE];

// Hash table for linear and quadratic probing


int linearHashTable[TABLE_SIZE];
int quadraticHashTable[TABLE_SIZE];

// Initialize hash tables


void init() {
for (int i = 0; i < TABLE_SIZE; i++) {
chain[i] = NULL;
linearHashTable[i] = -1;
quadraticHashTable[i] = -1;
}
}

// Hash function
int hashFunction(int key) {
return key % TABLE_SIZE;
}

// Insert using simple chaining


void insertChaining(int key) {
int index = hashFunction(key);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->next = chain[index];
chain[index] = newNode;
}
// Insert using linear probing
void insertLinearProbing(int key) {
int index = hashFunction(key);
int i = 0;

while (linearHashTable[(index + i) % TABLE_SIZE] != -1) {


i++;
}
linearHashTable[(index + i) % TABLE_SIZE] = key;
}

// Insert using quadratic probing


void insertQuadraticProbing(int key) {
int index = hashFunction(key);
int i = 0;

while (quadraticHashTable[(index + i * i) % TABLE_SIZE] != -1) {


i++;
}
quadraticHashTable[(index + i * i) % TABLE_SIZE] = key;
}

// Display chain
void displayChaining() {
for (int i = 0; i < TABLE_SIZE; i++) {
struct Node* temp = chain[i];
printf("chain[%d] -> ", i);
while (temp) {
printf("%d -> ", temp->key);
temp = temp->next;
}
printf("NULL\n");
}
}

// Display linear probing table


void displayLinearProbing() {
for (int i = 0; i < TABLE_SIZE; i++) {
if (linearHashTable[i] != -1) {
printf("linearHashTable[%d] = %d\n", i, linearHashTable[i]);
} else {
printf("linearHashTable[%d] = NULL\n", i);
}
}
}

// Display quadratic probing table


void displayQuadraticProbing() {
for (int i = 0; i < TABLE_SIZE; i++) {
if (quadraticHashTable[i] != -1) {
printf("quadraticHashTable[%d] = %d\n", i, quadraticHashTable[i]);
} else {
printf("quadraticHashTable[%d] = NULL\n", i);
}
}
}

int main() {
init();

int keys[] = {23, 43, 13, 27, 88, 52, 19, 20, 55, 60};

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


insertChaining(keys[i]);
insertLinearProbing(keys[i]);
insertQuadraticProbing(keys[i]);
}

printf("Hash Table using Chaining:\n");


displayChaining();

printf("\nHash Table using Linear Probing:\n");


displayLinearProbing();

printf("\nHash Table using Quadratic Probing:\n");


displayQuadraticProbing();

return 0;
}
*

You might also like