0% found this document useful (0 votes)
40 views35 pages

Advanced Data Structures: Practical

The document describes an assignment on implementing various advanced data structures using C programming language. It provides 7 programming problems to write code for linked lists, binary search trees, AVL trees, Red Black trees, B-trees, heap sort, and all pairs shortest path algorithms. For each data structure concept, it lists the functions to be coded, along with snippets of sample code implementing linked lists and binary search trees.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views35 pages

Advanced Data Structures: Practical

The document describes an assignment on implementing various advanced data structures using C programming language. It provides 7 programming problems to write code for linked lists, binary search trees, AVL trees, Red Black trees, B-trees, heap sort, and all pairs shortest path algorithms. For each data structure concept, it lists the functions to be coded, along with snippets of sample code implementing linked lists and binary search trees.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 35

Advanced Data Structures

Practical
(Department of Computer Engineering)

Submitted to: Submitted by:

INDEX
S.No. Practical 1. Write a program to insert and delete the nodes in link list at different locations. 2. Write a program to implement the concept of binary search tree. 3. Write a program to implement the concept of AVL tree. 4. Write a program to implement the concept of Red Black tree. 5. Write a program to implement the concept of B trees. 6. Write a program to implement the heap sort. 7. Write a program to implement All Pairs Shortest Path. Sign

1. Write a program to implement Linked List.


#include<stdio.h> #include<conio.h> #include<string.h> struct node { int b; struct node *link; }*x,*ptr,*pre,*first; int main() { int a,n; first=NULL; while(1) { printf("\n\nenter choice \n"); printf("1.Inserting a node at the end\n"); printf("2.Inserting a node in the beg\n "); printf("3.To traverse the list\n"); printf("4.Deleting a node\n"); printf("5.exit\n"); scanf("%d",&n); switch(n) { case 1: { printf("\nenter value u want to insert : "); scanf("%d",&a); x=(struct node *)malloc(sizeof(struct node)); x->link=NULL; x->b=a; if(first==NULL) { first=x; } else { ptr=first; while(ptr->link!=NULL) { ptr=ptr->link; } ptr->link=x; } printf("\nNODE SUCCESSFULLY INSERTED\n"); break; } case 2: {

printf("enter value u want to insert : "); scanf("%d",&a); x=malloc(sizeof(struct node)); x->link=first; x->b=a; first=x; printf("NODE SUCCESSFULLY INSERTED\n"); break; } case 3: { if(first==NULL) { printf("SORRY!! There is no node to be traversed\n"); } ptr=first; printf(" THIS IS THE DESIRED LIST : "); while(ptr!=NULL) { printf(" %d",ptr->b); ptr=ptr->link; } break; } case 4: { printf("\nEnter value which u want to delete : "); scanf("%d",&a); ptr=first; pre=NULL; if(first==NULL) { printf("\nTHERE IS NO NODE TO BE DELETED\n"); } while(ptr!=NULL) { if((ptr->b)==a) { (pre->link)=(ptr->link); break; } else { pre=ptr; ptr=ptr->link; } } printf("\nNODE SUCCESSFULLY DELETED\n"); break; } case 5: {

exit(0); } default: { printf("u entered wrong choice\n"); } } } getch(); return 0; }

2. Write a program to implement the concept of binary search tree.


#include<stdio.h> #include<conio.h> struct tree { int data; struct tree *left; struct tree *right; }; struct tree *create(); void preorder(struct tree *); void inorder(struct tree *); void postorder(struct tree *); struct tree *create() { struct tree *p,*root; int m,x; char s; root=(struct tree *)malloc(sizeof(struct tree)); printf("\nenter the value of the main root"); scanf("%d",&m); root->data=m; root->left=NULL; root->right=NULL; printf("\nenter n to stop creation of the binary search tree"); fflush(stdin); scanf("%c",&s); while(s!='n') { p=root; printf("\nenter the value of the newnode"); fflush(stdin); scanf("%d",&x); while(1) { if(x<p->data) { if(p->left==NULL) { p->left=(struct tree *)malloc(sizeof(struct tree)); p=p->left; p->data=x; p->right=NULL; p->left=NULL; break; } else p=p->left; }

else { if(p->right==NULL) { p->right=(struct tree *)malloc(sizeof(struct tree)); p=p->right; p->data=x; p->right=NULL; p->left=NULL; break; } else p=p->right; } } printf("\nwant to continue"); fflush(stdin); scanf("%c",&s); } return(root); } void preorder(struct tree *p) { if(p!=NULL) { printf("%d ",p->data); preorder(p->left); preorder(p->right); } } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left); printf("\t%d",p->data); inorder(p->right); } } void postorder(struct tree *p) { if(p!=NULL) { postorder(p->left); postorder(p->right); printf("\t%d",p->data); } } int main() { int h;

struct tree *root; while(1) { printf("\nenter 1. for creation of the binary search tree"); printf("\nenter 2. for preorder traversal"); printf("\nenter 3. for inorder traversal"); printf("\nenter 4. for postorder traversal"); printf("\nenter 5. for exit"); printf("\nenter your choice"); scanf("%d",&h); switch(h) { case 1: root=create(); break; case 2: preorder(root); break; case 3: inorder(root); break; case 4: postorder(root); break; case 5: exit(0); default: printf("\nentered a wrong choice"); } } }

3. Write a program to implement the concept of AVL tree.


# include<stdio.h> # include<malloc.h> # define F 0 # define T 1 struct NODE { char Info; int Flag; struct NODE *Left_Child; struct NODE *Right_Child; }; struct NODE *Binary_Tree (char , struct NODE *, int *); void Output(struct NODE *, int ); struct NODE *Balance_Right_Heavy(struct NODE *, int *); struct NODE *Balance_Left_Heavy(struct NODE *, int *); struct NODE *DELETE(struct NODE *, struct NODE *, int *); struct NODE *Delete_Element(struct NODE *, char , int *); /* Function to insert an element into tree */ struct NODE * Binary_Tree (char Info, struct NODE *Parent, int *H) { struct NODE *Node1; struct NODE *Node2; if(!Parent) { Parent = (struct NODE *) malloc(sizeof(struct NODE)); Parent->Info = Info; Parent->Left_Child = NULL; Parent->Right_Child = NULL; Parent->Flag = 0; *H = T; return (Parent); } if(Info < Parent->Info) { Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H); if(*H) /* Left branch has grown higher */ { switch(Parent->Flag) { case 1: /* Right heavy */ Parent->Flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->Flag = -1;

break; case -1: /* Left heavy */ Node1 = Parent->Left_Child; if(Node1->Flag == -1) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child; Node1->Right_Child = Parent; Parent->Flag = 0; Parent = Node1; } else { printf("\n Left to right rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent; if(Node2->Flag == -1) Parent->Flag = 1; else Parent->Flag = 0; if(Node2->Flag == 1) Node1->Flag = -1; else Node1->Flag = 0; Parent = Node2; } Parent->Flag = 0; *H = F; } } } if(Info > Parent->Info) { Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H); if(*H) /* Right branch has grown higher */ { switch(Parent->Flag) { case -1: /* Left heavy */ Parent->Flag = 0; *H = F; break; case 0: /* Balanced tree */ Parent->Flag = 1; break; case 1: /* Right heavy */ Node1 = Parent->Right_Child; if(Node1->Flag == 1)

{ printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; Parent->Flag = 0; Parent = Node1; } else { printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child; Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent; if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; } Parent->Flag = 0; *H = F; } } } return(Parent); } /* Output function */ void Output(struct NODE *Tree,int Level) { int i; if (Tree) { Output(Tree->Right_Child, Level+1); printf("\n"); for (i = 0; i < Level; i++) printf(" "); printf("%c", Tree->Info); Output(Tree->Left_Child, Level+1); } } /* Balancing Right Heavy */ struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H) {

struct NODE *Node1, *Node2; switch(Parent->Flag) { case -1: Parent->Flag = 0; break; case 0: Parent->Flag = 1; *H= F; break; case 1: /* Rebalance */ Node1 = Parent->Right_Child; if(Node1->Flag >= 0) { printf("\n Right to Right Rotation\n"); Parent->Right_Child= Node1->Left_Child; Node1->Left_Child = Parent; if(Node1->Flag == 0) { Parent->Flag = 1; Node1->Flag = -1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } Parent = Node1; } else { printf("\n Right to Left Rotation\n"); Node2 = Node1->Left_Child; Node1->Left_Child = Node2->Right_Child; Node2->Right_Child = Node1; Parent->Right_Child = Node2->Left_Child; Node2->Left_Child = Parent; if(Node2->Flag == 1) Parent->Flag = -1; else Parent->Flag = 0; if(Node2->Flag == -1) Node1->Flag = 1; else Node1->Flag = 0; Parent = Node2; Node2->Flag = 0; } } return(Parent); }

/* Balancing Left Heavy */ struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H) { struct NODE *Node1, *Node2; switch(Parent->Flag) { case 1: Parent->Flag = 0; break; case 0: Parent->Flag = -1; *H= F; break; case -1: /* Rebalance */ Node1 = Parent->Left_Child; if(Node1->Flag <= 0) { printf("\n Left to Left Rotation\n"); Parent->Left_Child= Node1->Right_Child; Node1->Right_Child = Parent; if(Node1->Flag == 0) { Parent->Flag = -1; Node1->Flag = 1; *H = F; } else { Parent->Flag = Node1->Flag = 0; } Parent = Node1; } else { printf("\n Left to Right Rotation\n"); Node2 = Node1->Right_Child; Node1->Right_Child = Node2->Left_Child; Node2->Left_Child = Node1; Parent->Left_Child = Node2->Right_Child; Node2->Right_Child = Parent; if(Node2->Flag == -1) Parent->Flag = 1; else Parent->Flag = 0; if(Node2->Flag == 1) Node1->Flag = -1; else Node1->Flag = 0;

Parent = Node2; Node2->Flag = 0; } } return(Parent); } /* Replace the node at which key is found with last right key of a left child */ struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H) { struct NODE *Dnode = R; if( R->Right_Child != NULL) { R->Right_Child = DELETE(R->Right_Child, Temp, H); if(*H) R = Balance_Left_Heavy(R, H); } else { Dnode = R; Temp->Info = R->Info; R = R->Left_Child; free(Dnode); *H = T; } return(R); } /* Delete the key element from the tree */ struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H) { struct NODE *Temp; if(!Parent) { printf("\n Information does not exist"); return(Parent); } else { if (Info < Parent->Info ) { Parent->Left_Child = Delete_Element(Parent->Left_Child, Info, H); if(*H) Parent = Balance_Right_Heavy(Parent, H); } else if(Info > Parent->Info) { Parent->Right_Child = Delete_Element(Parent->Right_Child, Info, H); if(*H) Parent = Balance_Left_Heavy(Parent, H); } else

{ Temp= Parent; if(Temp->Right_Child == NULL) { Parent = Temp->Left_Child; *H = T; free(Temp); } else if(Temp->Left_Child == NULL) { Parent = Temp->Right_Child; *H = T; free(Temp); } else { Temp->Left_Child = DELETE(Temp->Left_Child, Temp, H); if(*H) Parent = Balance_Right_Heavy(Parent, H); } } } return(Parent); } /* Function main */ void main() { int H; char Info ; char choice; struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE)); Tree = NULL; printf("\n Input choice "); choice = getchar(); while(choice != 'b') { fflush(stdin); printf("\n Input information of the node: "); scanf("%c", &Info); Tree = Binary_Tree(Info, Tree, &H); printf("\n Tree is:\n"); Output(Tree, 1); fflush(stdin); printf("\n Input choice or 'b' to break:"); choice = getchar(); } fflush(stdin); while(1) { printf("\n Input choice or 'b' to break:"); printf("\n Input the key value for which you want to deletedir:");

scanf("%c", &Info); if (Info == 'b') break; Tree = Delete_Element(Tree, Info, &H); printf("\n Tree is:\n"); Output(Tree, 1); } } OUTPUT

4. Write a program to implement the concept of Red-Black trees.


#include<iostream> #define BLACK 0 #define RED 1 using namespace std; typedef struct rb_red_blk_node { int info; int color; /* if red=0 then the node is black */ struct rb_red_blk_node* left; struct rb_red_blk_node* right; struct rb_red_blk_node* parent; } rb_node;

void rotate_right(rb_node *N); void rotate_left(rb_node *N); rb_node* grandparent(rb_node *n); rb_node* uncle(rb_node *n); rb_node* inorder_search(rb_node* node,int val); rb_node* initialize(rb_node *parent); void insert(rb_node* node, int val); void insert_case1(rb_node* node); void insert_case2(rb_node *n); void insert_case3(rb_node *n); void insert_case4(rb_node *n); void insert_case5(rb_node *n); rb_node* insert_node(rb_node *root);

// Rotate tree right -- performing right rotation void rotate_right(rb_node *N) { rb_node *P; P = N -> parent; if(P) { N -> parent = P -> parent; P -> left = N -> right; N -> right = P; } }

// Rotate tree left -- performing left rotation void rotate_left(rb_node *N) { rb_node *P; P = N -> parent; if(P) { N -> parent = P -> parent; P -> right = N -> left; N -> left = P; } }

// Finding the grand parent rb_node* grandparent(rb_node *n) { if ((n != NULL) && (n -> parent != NULL)) return n->parent->parent; else return NULL; } // Finding the Uncle rb_node* uncle(rb_node *n) { rb_node *g = grandparent(n); if (g == NULL) return NULL; // No grandparent means no uncle if (n->parent == g->left) return g->right; else return g->left; }

// inorder search for the element rb_node* inorder_search(rb_node* node,int val) { if(node -> info == -1) return node; else if(node -> info == val) return NULL; else if(node -> info < val) return inorder_search(node->left, val); else return inorder_search(node->right, val); }

// initializing the element rb_node* initialize(rb_node *parent) { rb_node *node; node = new rb_node; node -> parent = parent; node -> color = BLACK; node -> info = -1; node -> left = NULL; node -> right = NULL; return node; }

// inserting the node at the required place void insert(rb_node* node, int val) { rb_node *child; child = initialize(node); node -> info = val; node -> color = RED; // node parent remains same; node -> left = child; node -> right = child; }

void insert_case1(rb_node* node) { if (node -> parent == NULL) node -> color = BLACK; else insert_case2(node); }

void insert_case2(rb_node *n) { if (n -> parent -> color == BLACK) return; /* Tree is still valid */ else insert_case3(n); }

void insert_case3(rb_node *n) { rb_node *u = uncle(n), *g; if ((u != NULL) && (u->color == RED)) { n -> parent -> color = BLACK; u -> color = BLACK; g = grandparent(n); g -> color = RED; insert_case1(g); } else insert_case4(n); }

void insert_case4(rb_node *n) { rb_node *g = grandparent(n); if ((n == n->parent->right) && (n->parent == g->left)) { rotate_left(n->parent); n = n->left; } else if ((n == n->parent->left) && (n->parent == g->right)) { rotate_right(n->parent); n = n->right; } insert_case5(n); }

void insert_case5(rb_node *n) { rb_node *g = grandparent(n); n->parent->color = BLACK; g-> color = RED; if ((n == n->parent->left) && (n->parent == g->left)) { rotate_right(g) else { /* (n == n->parent->right) and (n->parent == g->right) */ rotate_left(g); }

rb_node* insert_node(rb_node *root) { int i, j, val; rb_node *node; cin>>val; if(val == 0) return NULL; node = inorder_search(root, val); if(!node) { cout<<"\n\n Node already exists . . . "; return NULL; } else { insert(node, val); insert_case1(node); return node; } } void tr(rb_node *node) { if(node -> info == -1) { tr(node->left); cout<<node->info; tr(node->right); } }

int main() { rb_node *root, *node; root = initialize(NULL); do node = insert_node(root); while(node); cout<<" \n\n inorder_traversal of thet tree is :";

tr(root); system("pause"); }

OUTPUT:

5. Write a program to implement the concept of B trees.


// Program for insertion and deletion in B tree #include<stdio.h> #include<stdlib.h> #include<conio.h> #define M 5 // M gives the order of the tree struct node { int n; // n < M No. of keys in node int keys[M-1]; // array of keys struct node *p[M]; // (n+1 pointers will be in use) }*root=NULL; enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys }; void insert(int key); void display(struct node *root,int); void DelNode(int x); void search(int x); enum KeyStatus ins(struct node *r, int x, int* y, struct node** u); int searchPos(int x,int *key_arr, int n); enum KeyStatus del(struct node *r, int x); int main() { int key; int choice; printf("Creation of B tree of order %d\n",M); while(1) { printf("\n1.Insert\n"); printf("2.Delete\n"); printf("3.Search\n"); printf("4.Display\n"); printf("5.Quit\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the key : "); scanf("%d",&key);

insert(key); break; case 2: printf("Enter the key : "); scanf("%d",&key); DelNode(key); break; case 3: printf("Enter the key : "); scanf("%d",&key); search(key); break; case 4: printf("Btree is :\n"); display(root,0); break; case 5: getch(); exit(1); default: printf("Wrong choice\n"); break; } } return 0; } void insert(int key) { struct node *newnode; int upKey; enum KeyStatus value; value = ins(root, key, &upKey, &newnode); if (value == Duplicate) printf("Key already available\n"); if (value == InsertIt) { struct node *uproot = root; root=(node*)malloc(sizeof(struct node)); root->n = 1; root->keys[0] = upKey; root->p[0] = uproot; root->p[1] = newnode; } }

enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node **newnode) { struct node *newPtr, *lastPtr; int pos, i, n,splitPos; int newKey, lastKey; enum KeyStatus value; if (ptr == NULL) { *newnode = NULL; *upKey = key; return InsertIt; } n = ptr->n; pos = searchPos(key, ptr->keys, n); if (pos < n && key == ptr->keys[pos]) return Duplicate; value = ins(ptr->p[pos], key, &newKey, &newPtr); if (value != InsertIt) return value; // If no. of keys in node is less than M-1 if (n < M - 1) { pos = searchPos(newKey, ptr->keys, n); // Shifting the key and pointer right for inserting the new key for (i=n; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } // Key is inserted at exact location ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr; ++ptr->n; // incrementing the number of keys in node return Success; } //If keys in nodes are maximum and position of node to be inserted is last if (pos == M - 1) { lastKey = newKey; lastPtr = newPtr; } else //If keys in node are maximum and position of node to be inserted is not last { lastKey = ptr->keys[M-2];

lastPtr = ptr->p[M-1]; for (i=M-2; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr; } splitPos = (M - 1)/2; (*upKey) = ptr->keys[splitPos]; (*newnode)=(node*)malloc(sizeof(struct node)); // Right node after split ptr->n = splitPos; // No. of keys for left splitted node (*newnode)->n = M-1-splitPos; //No. of keys for right splitted node for (i=0; i < (*newnode)->n; i++) { (*newnode)->p[i] = ptr->p[i + splitPos + 1]; if(i < (*newnode)->n - 1) (*newnode)->keys[i] = ptr->keys[i + splitPos + 1]; else (*newnode)->keys[i] = lastKey; } (*newnode)->p[(*newnode)->n] = lastPtr; return InsertIt; } void display(struct node *ptr, int blanks) { if (ptr) { int i; for(i=1;i<=blanks;i++) printf(" "); for (i=0; i < ptr->n; i++) printf("%d ",ptr->keys[i]); printf("\n"); for (i=0; i <= ptr->n; i++) display(ptr->p[i], blanks+10); } } void search(int key) { int pos, i, n; struct node *ptr = root; printf("Search path:\n");

while (ptr) { n = ptr->n; for (i=0; i < ptr->n; i++) printf(" %d",ptr->keys[i]); printf("\n"); pos = searchPos(key, ptr->keys, n); if (pos < n && key == ptr->keys[pos]) { printf("Key %d found in position %d of last displayed node\n",key,i); return; } ptr = ptr->p[pos]; } printf("Key %d is not available\n",key); } int searchPos(int key, int *key_arr, int n) { int pos=0; while (pos < n && key > key_arr[pos]) pos++; return pos; } void DelNode(int key) { struct node *uproot; enum KeyStatus value; value = del(root,key); switch (value) { case SearchFailure: printf("Key %d is not available\n",key); break; case LessKeys: uproot = root; root = root->p[0]; free(uproot); break; } } enum KeyStatus del(struct node *ptr, int key) { int pos, i, pivot, n ,min; int *key_arr;

enum KeyStatus value; struct node **p,*lptr,*rptr; if (ptr == NULL) return SearchFailure; // Assigns values of node n=ptr->n; key_arr = ptr->keys; p = ptr->p; min = (M - 1)/2; //Minimum number of keys pos = searchPos(key, key_arr, n); if (p[0] == NULL) { if (pos == n || key < key_arr[pos]) return SearchFailure; //Shift keys and pointers left for (i=pos+1; i < n; i++) { key_arr[i-1] = key_arr[i]; p[i] = p[i+1]; } return --ptr->n >= (ptr==root ? 1 : min) ? Success : LessKeys; } if (pos < n && key == key_arr[pos]) { struct node *qp = p[pos], *qp1; int nkey; while(1) { nkey = qp->n; qp1 = qp->p[nkey]; if (qp1 == NULL) break; qp = qp1; } key_arr[pos] = qp->keys[nkey-1]; qp->keys[nkey - 1] = key; } value = del(p[pos], key); if (value != LessKeys) return value; if (pos > 0 && p[pos-1]->n > min) { pivot = pos - 1; //pivot for left and right node lptr = p[pivot];

rptr = p[pos]; // Assigns values for right node rptr->p[rptr->n + 1] = rptr->p[rptr->n]; for (i=rptr->n; i>0; i--) { rptr->keys[i] = rptr->keys[i-1]; rptr->p[i] = rptr->p[i-1]; } rptr->n++; rptr->keys[0] = key_arr[pivot]; rptr->p[0] = lptr->p[lptr->n]; key_arr[pivot] = lptr->keys[--lptr->n]; return Success; } if (pos > min) { pivot = pos; // pivot for left and right node lptr = p[pivot]; rptr = p[pivot+1]; // Assigns values for left node lptr->keys[lptr->n] = key_arr[pivot]; lptr->p[lptr->n + 1] = rptr->p[0]; key_arr[pivot] = rptr->keys[0]; lptr->n++; rptr->n--; for (i=0; i < rptr->n; i++) { rptr->keys[i] = rptr->keys[i+1]; rptr->p[i] = rptr->p[i+1]; } rptr->p[rptr->n] = rptr->p[rptr->n + 1]; return Success; } if(pos == n) pivot = pos-1; else pivot = pos; lptr = p[pivot]; rptr = p[pivot+1]; // merge right node with left node lptr->keys[lptr->n] = key_arr[pivot]; lptr->p[lptr->n + 1] = rptr->p[0]; for (i=0; i < rptr->n; i++) { lptr->keys[lptr->n + 1 + i] = rptr->keys[i]; lptr->p[lptr->n + 2 + i] = rptr->p[i+1];

} lptr->n = lptr->n + rptr->n +1; free(rptr); //Remove right node for (i=pos+1; i < n; i++) { key_arr[i-1] = key_arr[i]; p[i] = p[i+1]; } return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys; }

6. Write a program to implement heap sort.


#include<stdio.h> #include<conio.h> #include<stdlib.h> void Heapify(int *a, int n); void Adjust(int *a, int i, int n); void HeapSort(int *a, int n) { int i,t; Heapify(a,n); for (i=n;i<=2;i--) { t=a[i]; a[i]=a[1]; a[1]=t; Adjust(a,1,i-1); } } void Heapify(int *a, int n) { int i,k; k=n/2; for(i=k;i<=1;i--) { Adjust(a,i,n); } } void Adjust(int *a, int i, int n) { int k,j,item; j=2*i; item=a[i]; while(j<=n) { if((j<=n) && (a[j]<a[j+1])) j=j+1; if(item>=a[j]) break; k=j/2; a[k]=a[j]; j=2*j; } } int main() { int *a,n,i; printf("Enter the number of elements: "); scanf("%d", &n); a=(int *)malloc((n+1)*sizeof(int)); a[0]=0; printf("Enter the elements of array: \n"); for(i=1;i<=n;i++) {

scanf("%d", &a[i]); } HeapSort(a,n); printf("\nThe sorted array is:\n"); for(i=1;i<=n;i++) { printf("%d ", a[i]); } getch(); }

7. Write a program to implement All Pairs Shortest Path.


#include<stdio.h> #include<conio.h> int main() { int i,j,k,n,a[100][100]; printf("enter no of nodes : "); scanf("%d",&n); printf("enter the graph matrix : "); printf("enter 999 if there is no edge between 2 nodes"); printf("\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } printf("\n"); } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(a[i][j]>(a[i][k]+a[k][j])) { a[i][j]=(a[i][k]+a[k][j]); } printf(" %d",a[i][j]); } printf("\n"); } if(k==n-1) { printf("\nthis is the final matrix "); } printf("\n\n"); } getch(); return 0; }

You might also like