DS Tutorial 3
DS Tutorial 3
REG NO – 19MCA10049
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
};
if (current == NULL)
return;
if (current->data == current->next->data)
next_next = current->next->next;
free(current->next);
current->next = next_next;
else
current = current->next;
}
void push(struct Node** head_ref, int new_data)
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
while (node!=NULL)
int main()
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
printList(head);
removeDuplicates(head);
printf("\n Linked list after duplicate removal ");
printList(head);
return 0;
OUTPUT
2. Write a program for AVL Tree to implement the insertion operations: (For
nodes as integers) .Test the program for all cases (LL, RR, RL, LR rotation).
#include<stdio.h>
#include<stdlib.h>
struct Node
int key;
int height;
};
if (N == NULL)
return 0;
return N->height;
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return(node);
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right))+1;
return x;
y->left = x;
x->right = T2;
if (N == NULL)
return 0;
if (node == NULL)
return(newNode(key));
return node;
node->height = 1 + max(height(node->left),
height(node->right));
return rightRotate(node);
return leftRotate(node);
{
node->left = leftRotate(node->left);
return rightRotate(node);
node->right = rightRotate(node->right);
return leftRotate(node);
return node;
if(root != NULL)
preOrder(root->left);
preOrder(root->right);
}
int main()
preOrder(root);
return 0;
}
OUTPUT
3. Given a graph G = (V, E) and |V| = n and |E| = m, where V is the set of
vertices and E is the set of edges. Write a program that will output the parent
node of each node in each of the following traversal mechanisms: a. Depth
First Traversal, b. Breadth First Traversal
import java.io.*;
import java.util.*;
class Graph
{
private int V;
private LinkedList<Integer> adj[];
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
queue.add(s);
while (queue.size() != 0)
{
s = queue.poll();
System.out.print(s+" ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
void DFS(int s) {
while(stack.empty() == false)
{
// Pop a vertex from stack and print it
s = stack.peek();
stack.pop();
while (itr.hasNext())
{
int v = itr.next();
if(!visited.get(v))
stack.push(v);
}
}
}
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.BFS(2);
g1.addEdge(1, 0);
g1.addEdge(0, 2);
g1.addEdge(2, 1);
g1.addEdge(0, 3);
g1.addEdge(1, 4);
}
}
OUTPUT