Advanced Data Structures: Practical
Advanced Data Structures: Practical
Practical
(Department of Computer Engineering)
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
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: {
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"); } } }
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
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:
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; }
scanf("%d", &a[i]); } HeapSort(a,n); printf("\nThe sorted array is:\n"); for(i=1;i<=n;i++) { printf("%d ", a[i]); } getch(); }