Upload
kurumeti
View
249
Download
2
Embed Size (px)
DESCRIPTION
WER
Citation preview
ADVANCED DATA STRUCTURES LAB MANUAL
1) Program to implement functions of Dictionary using Hashing.AIM : Program to implement functions of Dictionary using Hashing.PROGRAM :#include
#include
struct dict
{
int k;
int v;
}a[10];
int insert(int key,int value)
{
int h=0,i=0,j;
h=key%10;
if(a[h].k==-1)
{
a[h].k=key;
a[h].v=value;
return 1;
}
for(i=h+1;ib)?a:b)
typedef struct AvlNode
{
int data;
struct AvlNode *left,*right;
}avlnode;
avlnode *root;avlnode* rotate_LL(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=child->right;
child->right=parent;
return child;
}avlnode* rotate_RR(avlnode *parent)
{
avlnode *child=parent->right;
parent->right=child->left;
child->left=parent;
return child;
}
avlnode* rotate_RL(avlnode *parent)
{
avlnode *child = parent->right;
parent->right=rotate_LL(child);
return rotate_RR(parent);
}
avlnode* rotate_LR(avlnode *parent)
{
avlnode *child=parent->left;
parent->left=rotate_RR(child);
return rotate_LL(parent);
}
int get_height(avlnode *node)
{
int height=0;
if(node!=NULL)
height=1+max(get_height(node->left),get_height(node->right));
return height;
}
int get_balance(avlnode *node)
{
if(node==NULL) return 0;
return get_height(node->left)-get_height(node->right);
}
avlnode* balance_tree(avlnode **node)
{
int bal_factor= get_balance(*node);
if(bal_factor>1)
{
if(get_balance((*node)->left) > 0)
*node=rotate_LL(*node);
else
*node=rotate_LR(*node);
}
else if(bal_factorright) < 0)
*node = rotate_RR(*node);
else
*node = rotate_RL(*node);
}
return *node;
}
avlnode* insert(avlnode **root,int key)
{
if(*root==NULL)
{
*root=(avlnode*)malloc(sizeof(avlnode));
(*root)->data = key;
(*root)->left=(*root)->right=NULL;
}
else if(keydata)
{
(*root)->left=insert(&((*root)->left),key);
(*root)=balance_tree(root);
}
else if(key>(*root)->data)
{
(*root)->right=insert(&((*root)->right),key);
(*root)=balance_tree(root);
}
return *root;
}
avlnode* search(avlnode *node, int key)
{
if(node==NULL) return NULL;
printf("%d->",node->data);
if(key==node->data)
return node;
else if(keydata)
search(node->left,key);
else
search(node->right,key);
}
void display(avlnode *root)
{
if(root==NULL)
return;
printf(" %d",root->data);
display(root->left);
display(root->right);
}main()
{
int ch,x;
while(1)
{
printf("\n1.INSERT\n2.SEARCH\n3.DISPLAY\n4.EXIT"); printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 :printf("\nEnter a key to insert");
scanf("%d",&x);
insert(&root,x);
break;
case 2 :printf("\nEnter search key");
scanf("%d",&x);
search(root,x);
break;
case 3:display(root);
break;
case 4:exit(0);
}
}
}
OUTPUT :1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 3
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 2
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 4
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 3
324
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 5
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 1
Enter a key to insert : 6
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 3
32546
1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 2
Enter the key to search : 4
3 ( 5 ( 4 (1. INSERT
2. SEARCH
3. DISPLAY
4. EXIT
Enter Your choice : 4
AIM : To perform various on 2-3 trees.PROGRAM :
#include
#include
using namespace std;
// TwoThreeNode class
class TwoThreeNode {
private:
// Gets the value of the smallest data item in the subtree
// rooted by this node
int getSmallest() {
TwoThreeNode *node = this;
while (!node->isLeaf()) node = node->child[0];
return node->key[0];
}
// Insert into a node with 1 child
void insert1Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newNode is inserted as first child of root
child[1] = child[0];
child[0] = newChild;
key[0] = child[1]->getSmallest();
}
else {
// newNode is iserted as second child of root
child[1] = newChild;
key[0] = newSmallest;
}
}
// Insert into a node with 2 children
void insert2Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
newChild->parent = this;
if (newKey < child[0]->key[0]) {
child[2] = child[1];
child[1] = child[0];
child[0] = newChild;
key[1] = key[0];
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else if (newKey < child[1]->key[0]) {
child[2] = child[1];
child[1] = newChild;
key[1] = key[0];
key[0] = newSmallest;
}
else {
child[2] = newChild;
key[1] = newSmallest;
}
}
// Insert into a node with 3 children
void insert3Siblings(TwoThreeNode *newChild, int newSmallest) {
int newKey = newChild->key[0];
int splitSmallest = -1;
TwoThreeNode *splitNode = new TwoThreeNode();
splitNode->parent = parent;
if (newKey < child[0]->key[0] || newKey < child[1]->key[0]) {
// newChild is inserted in current node
splitSmallest = key[0];
splitNode->child[0] = child[1];
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
child[1]->parent = splitNode;
child[2]->parent = splitNode;
newChild->parent = this;
if (newKey < child[0]->key[0]) {
// newChild is inserted as first child
child[1] = child[0];
child[0] = newChild;
key[0] = child[1]->getSmallest();
updateParentSmallest(newSmallest);
}
else {
// newChild is inserted as second child
child[1] = newChild;
key[0] = newSmallest;
}
}
else {
// newChild is inserted in split node
child[2]->parent = splitNode;
newChild->parent = splitNode;
if (newKey < child[2]->key[0]) {
// newChild is inserted as first child
splitSmallest = newSmallest;
splitNode->child[0] = newChild;
splitNode->child[1] = child[2];
splitNode->key[0] = key[1];
}
else {
// newChild is inserted as second child
splitSmallest = key[1];
splitNode->child[0] = child[2];
splitNode->child[1] = newChild;
splitNode->key[0] = newSmallest;
}
}
child[2] = NULL;
key[1] = -1;
if (parent->parent == NULL) {
// At root, so new root needs to be created
TwoThreeNode *newNode = new TwoThreeNode();
parent->child[0] = newNode;
newNode->parent = parent;
newNode->child[0] = this;
parent = newNode;
}
parent->insert(splitNode, splitSmallest);
}
// Update the parent nods efor the smallest child value
void updateParentSmallest(int data) {
switch (sibNumber()) {
case 0: if (parent->parent != NULL) parent->updateParentSmallest(data); break;
case 1: parent->key[0] = data; break;
case 2: parent->key[1] = data; break;
}
}
public:
int key[2];
TwoThreeNode *parent, *child[3];
// Constructor
TwoThreeNode(int data = -1) {
key[0] = data;
key[1] = -1;
parent = child[0] = child[1] = child[2] = NULL;
}
// Check if node is a leaf
bool isLeaf() {
return (child[0] == NULL);
}
// Get which sibling the node is
int sibNumber() {
for (int i = 0; i < 3; ++i) {
if (this == parent->child[i]) return i;
}
return -1;
}
// Insertion
void insert(TwoThreeNode *newChild, int newSmallest) {
if (child[1] == NULL) insert1Siblings(newChild, newSmallest);
else if (child[2] == NULL) insert2Siblings(newChild, newSmallest);
else insert3Siblings(newChild, newSmallest);
}
};
// TwoThreeTree class
class TwoThreeTree {
private:
TwoThreeNode *root;
// Find the appropriate operation point
TwoThreeNode* findSpot(TwoThreeNode *node, int data) {
if (node == NULL) return NULL;
while (!node->isLeaf()) {
if (node->key[0] == data || node->key[1] == data)
return NULL;
if (node->key[0] == -1 || data < node->key[0])
node = node->child[0];
else if (node->key[1] == -1 || data < node->key[1])
node = node->child[1];
else
node = node->child[2];
}
if (node->key[0] == data) return NULL;
return node->parent;
}
// Recursively print the subtree starting from the given node
void print(TwoThreeNode *node, int tabs = 0) {
for (int i = 0; i < tabs; ++i) {
cout child[0]->parent = root;
}
// Insert
bool insert(int data) {
TwoThreeNode *newNode = new TwoThreeNode(data);
TwoThreeNode *spot = root->child[0];
if (spot->child[0] == NULL) {
// First insertion
newNode->parent = spot;
spot->child[0] = newNode;
}
else {
spot = findSpot(spot, data);
if (spot == NULL) return false;
spot->insert(new TwoThreeNode(data), data);
}
return true;
}
void print() {
print(root->child[0]);
cout ",temp->data);
temp1=temp->adj;
while (temp1!=NULL)
{
printf( "%c->",temp1->dest);
temp1=temp1->link;
}
printf( "\n" );
temp=temp->next;
}
}
OUTPUT :1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : A
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : B
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : C1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : D1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 2
Enter an edge to be inserted : A B
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted: A C
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : B D
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : C D
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
A(B(C(B(D(C(D(D(1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 3
Enter a vertex to be deleted : A
B(D(C(D(D(1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 4
Enter a vertex to Find : D
Vertex Found.
AIM : Program to perform the following operations on Graph.
1. Insert Vertex2.Insert Edge
3.Delete Edge4.Find Vertex
PROGRAM #include
struct vertex
{
struct vertex *next;
char data;
struct edge *adj;
}*first=NULL,*p;struct edge
{
char dest;
struct edge *link;
};
struct vertex *find_vertex(char);
main()
{
int choice;
char x,origin, destin;
while(1)
{
printf( "1.InsertVertex\n" );
printf( "2.InsertEdge\n" );
printf( "3.DeleteEdge\n" );
printf( "4.FindVertex\n");
printf( "5.Display\n" );
printf( "6.Exit\n" );
printf( "Enter your choice : " );
scanf( "%d",&choice );
switch(choice)
{
case 1: printf("Enter a vertex to be inserted : " );
scanf(" %c",&x);
insert_vertex(x);
break;
case 2: printf( "Enter an edge to be inserted : " );
scanf( " %c %c", &origin, &destin );
insert_edge( origin, destin );
break;
case 3: printf( "Enter an edge to be deleted : " );
scanf( " %c %c", &origin, &destin );
delete_edge( origin, destin );
break;
case 4:printf( "Enter a vertex to find:" );
scanf( " %c",&x);
p=find_vertex(x);
if(p==NULL)
printf("\n vertex not found\n");
else
printf("\n vertex %c found\n",p->data);
break;
case 5: display();
break;
case 6:exit();
}
}
}
insert_vertex( char x)
{
struct vertex *temp,*newnode;
newnode=(struct vertex *) malloc( sizeof( struct vertex));
newnode->data=x;
newnode->next = NULL;
newnode->adj=NULL;
if(first==NULL)
{
first=newnode;
return;
}
temp=first;
while(temp->next!=NULL )
temp=temp->next;
temp->next =newnode;
}
insert_edge(char s, char d)
{
struct vertex *locs, *locd;
struct edge *newnode,*temp;
locs=find_vertex(s);
newnode=(struct edge *)malloc(sizeof( struct edge));
newnode->dest=d;
newnode->link=NULL;
if(locs->adj==NULL)
{
locs->adj=newnode;
return;
}
temp=locs->adj;
while(temp->link!=NULL)
temp=temp->link;
temp->link=newnode;
}
struct vertex *find_vertex( char x)
{
struct vertex *temp,*loc=NULL;
temp=first;
while (temp!=NULL )
{
if(temp->data==x)
{
loc=temp;
return loc;
}
else
temp=temp->next;
}
return loc;
}
delete_edge(char s, char d)
{
struct vertex *locs,*locd;
struct edge *ptr,*temp;
locs=find_vertex(s);
if (locs->adj->dest==d)
{
temp=locs->adj;
locs->adj=locs->adj->link;
free(temp);
return ;
}
temp=locs->adj;
while (temp->link->link!=NULL )
{
if (temp->link->dest==d)
{
ptr=temp->link;
temp->link =ptr->link;
free(temp);
return;
}
temp=temp->link;
}
if ( temp->link->dest==d)
{
ptr=temp->link;
free(ptr);
temp->link=NULL;
return;
}
}
display()
{
struct node *temp;
struct edge *temp1;
temp=first;
while(temp!=NULL )
{
printf( "%c ->",temp->data);
temp1=temp->adj;
while (temp1!=NULL)
{
printf( "%c->",temp1->dest);
temp1=temp1->link;
}
printf( "\n" );
temp=temp->next;
}
}OUTPUT :
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : A
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : B
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : C1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 1
Enter a vertex to be inserted : D1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice : 2
Enter an edge to be inserted : A B
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted: A C
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : B D
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 2
Enter an edge to be inserted : C D
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
A(B(C(B(D(C(D(D(1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 3
Enter an edge to be deleted : A C
1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 5
A(B(B(D(C(D(D(1. Insert Vertex2. Insert Edge
3. Delete Edge
4. Find Vertex
5. Display
6. Exit
Enter your choice: 6AIM : Program to implement Depth First Search for a graph non-recursively.PROGRAM :
#include
int top=-1,a[20][20],visit[20],stack[20];
void push(int ele)
{
if(top==19)
printf("Over Flow...");
else
stack[++top]=ele;
}
int pop()
{
int key;
if(top==-1)
return (0);
else
{
key=stack[top--];
return (key);
}
}
main()
{
int i,j,n,s,k;
printf("Enter no of Vertices : ");
scanf("%d",&n);
for(i=1;i