PIXELES CLASSES BCA & MCA (IGNOU) Course Code Course Title : MCS-021 : Data and File Structures Assignment Number : BCA (III)/021/Assignment/ 2015 October, 2015 (For July 2015 Session) April, 2016 (For January 2016 Session) Page | 1 1. Write an algorithm for the implementation of a Dequeue. (20 Marks) Ans: #include<stdio.h> #define max 10 //macro definition struct queue // Define a structure having an array and two variable front and rear { int arr[max]; int front, rear; }q; void init() // initialize queue’s front and rear position { q.front=-1,q.rear=-1; } void add_front(int n) // add and element into the linked list { if (q.front==0) { printf("Element can not be inserted at front"); return; } else { q.front--; q.arr[q.front]=n; if(q.front==-1) q.front=0; } } void del_front() { int a; if(q.front==-1) { printf("queue is empty"); return; } else a=q.front; printf("\n\deleted number is =%d at position %d",q.arr[a],a); if(q.front==q.rear) q.front=q.rear-1; else q.front=q.front+1; } void add_rear(int n) // add and element into the linked list www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) { if (q.front==max-1) { printf("Element can not be inserted at rear"); return; } Page | 2 else { q.rear=q.rear+1; q.arr[q.rear]=n; if(q.rear==-1) q.rear=0; } } void del_rear() { int a; if(q.rear==-1) { printf("queue is empty"); return; } else { a=q.front; printf("\n\deleted number is =%d ",q.arr[a]); if(q.front==q.rear) q.front=q.rear-1; else { q.rear=q.rear-1; } } } void disp() { int i; printf("\n\nQueue as an array\n\n"); for(i=q.front;i<=q.rear;i++) printf("--->%d",q.arr[i]); } main() { int ch,n; char c; init(); clrscr(); do { printf("\n1-add front\n"); printf("2-add rear\n"); printf("3-delete front\n"); www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) printf("4-delete rear\n"); printf("5-display rear\n"); printf("6- Exit\n"); printf("\n\n enter the choice"); scanf("%d",&ch); switch(ch) Page | 3 { case 1: printf("\nenter the number"); scanf("%d",&n); add_front(n); break; case 2: printf("\nenter the number"); scanf("%d",&n); add_rear(n); break; case 3: del_front(); break; case 4: del_rear(); break; case 5: disp(); break; case 6: break; default: printf("sorry wrong choice"); } printf("\nD you want to continue(y/n)"); c=getche(); }while(c=='y'); getch(); } 2. Implement multiple queues in a single dimensional array. Write algorithms for various queue operations for them. (20 Marks) Ans: Multiqueue is a data structure where multiple queues are maintained. This type of data structures are used for process scheduling. We may use one dimensional array or multidimensional array to represent a multiple queue. www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) Page | 4 #include<stdio.h> #define max 10 //macro definition struct queue // Define a structure having an array and two variable front and rear { int arr[max]; int front, rear; }q; void init() // initialize queue’s front and rear position { q.front=-1,q.rear=-1; } void add_front(int n) // add and element into the linked list { if (q.front==0) { printf("Element can not be inserted at front"); return; } else { q.front--; q.arr[q.front]=n; if(q.front==-1) q.front=0; www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) } } Page | 5 void del_front() { int a; if(q.front==-1) { printf("queue is empty"); return; } else a=q.front; printf("\n\deleted number is =%d at position %d",q.arr[a],a); if(q.front==q.rear) q.front=q.rear-1; else q.front=q.front+1; } void add_rear(int n) // add and element into the linked list { if (q.front==max-1) { printf("Element can not be inserted at rear"); return; } else { q.rear=q.rear+1; q.arr[q.rear]=n; if(q.rear==-1) www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) q.rear=0; } } Page | 6 void del_rear() { int a; if(q.rear==-1) { printf("queue is empty"); return; } else { a=q.front; printf("\n\deleted number is =%d ",q.arr[a]); if(q.front==q.rear) q.front=q.rear-1; else { q.rear=q.rear-1; } } } void disp() { int i; printf("\n\nQueue as an array\n\n"); for(i=q.front;i<=q.rear;i++) printf("--->%d",q.arr[i]); www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) } main() We are teaching IGNOU’s BCA & MCA Students { Page | 7 int ch,n; Why join us? char c; Regular Classes BCA & MCA IGNOU Special Institute init(); clrscr(); Free Trial Classes do Subjective Knowledge { Free PIXELES Guide Books (Prepared by printf("\n1-add front\n"); printf("2-add rear\n"); our teachers) Free Solved Assignments printf("3-delete front\n"); printf("4-delete rear\n"); Experienced Faculties printf("5-display rear\n"); 100% Results printf("6- Exit\n"); Home Test Series printf("\n\n enter the choice"); Class Test Series scanf("%d",&ch); switch(ch) We teach you until you pass { Final Year Synopsis & Project case 1: Proper Guidance printf("\nenter the number"); scanf("%d",&n); add_front(n); break; case 2: printf("\nenter the number"); scanf("%d",&n); add_rear(n); break; case 3: www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) del_front(); break; case 4: Page | 8 del_rear(); break; case 5: disp(); break; case 6: break; default: printf("sorry wrong choice"); } printf("\nD you want to continue(y/n)"); c=getche(); }while(c=='y'); getch(); } 3. Write a note of not more than 5 pages summarizing the latest research in the area of “Trees”. Refer to various journals and other online resources. Indicate them in your assignment. (20 Marks) Ans: Do yourself [this is latest research; you can search easily in Google] 4. What are AVL trees? What are Red-Black trees? What are the differences between them? ANs: AVL (Highest Balance Tree) This tree is discovered by Russian mathematician their names are Adlson, Velskii and Landis. So, it is also called AVL tree. Height of left sub tree minus height of right sub tree is 1, 0 or -1 for each node is called balance factor (BF). A tree is called unbalanced if balance factor of any node in tree become other than 1, 0 or -1. Thus AVL tree is a binary tree having balance factor 1, 0 or 0 -1. Balance factor is written on the head node. www.pixelesindia.com PIXELES CLASSES BCA & MCA (IGNOU) A red-black tree (RBT)is a binary search tree that satisfies the following red-black properties: Page | 9 Every node has a color that is either red or black. Every leaf is black. If a node is red, both children are black. Every path from a given node down to any descendant leaf contains the same number of black nodes. The number of black nodes on such a path (not including the initial node but including leaves) is called the black-height (bh)of the node. The root of the tree is black. Differences: AVL trees are always at the optimal balance point, but can slow down inserts and deletes because they never allow that tree height to be outside the -1 to 1 range. But you will have the fastest look times. AVL trees are often compared with red-black trees because both support the same set of operations and take O(log n) time for the basic operations. Red black trees are also self balancing but can become slightly imbalanced to improve insert and delete times, with a small potential hit to search times. The balancing of the tree is not perfect but it is good enough to allow it to guarantee searching in O (log n) time, where n is the total number of elements in the tree. The insertion and deletion operations, along with the tree rearrangement and recoloring, are also performed in O(log n) time Branches & Contacts Details Uttam Nagar:-WZ-B7, Old Pankha Road (Opp. Primary School), Near East Metro Station, Uttam Nagar, New Delhi-59 Nangloi:-Plot. No-19, Ext- 2A, oppBanke-Bihari, Talabwali Road, Nangloi, Delhi-41 Ph: 9213327975, 9716339580 8750321695 pixeles@rediffmail.com, Web: www.pixelesindia.com Disclaimer: This Assignment is prepared by Our Students. Institution and publisher are neither responsible for the result of the any action taken on the basis of this work or any omissions or errors. www.pixelesindia.com