0% found this document useful (0 votes)
61 views37 pages

BCS304 Solved QNP

good

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
61 views37 pages

BCS304 Solved QNP

good

Uploaded by

Sahim Tambadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 37
BCS304 Model Question Paper-I with effect from 2023-24 (CBCS Scheme) os LTT TTL Third Semester B.E. Degree Examination Data Structures and Applications TIME: 03 Hours Max. Marks: 100 Note: 01. Answer any FIVE full questions, choosing at least ONE question from each MODULE. *Bloom’s Module -1 ‘Taxonomy | Marks Level QOI Ta | Define data structures. With a neat diagram, explain the classification of | E data structures with examples. ' | What do you mean by pattern matching? Outline the Knuth Morris Pratt (KMP) algorithm and illustrate it to find the occurrences of the following pattern. LB 8 P: ABCDABD S: ABC ABCDAB ABCDABCDABDE © | Write a program in C to implement push, pop and display operations for | 5 A stacks using arrays. ‘OR 02 [a | Explain in brief the different functions of dynamic memory allocation. 2 3 | Write functions in C for the following operations without using built-in functions i) Compare two strings. 3 8 Concatenate two strings. ) _Reverse a string | Write a function to evaluate the postfix expression, Illustrate the same for the given postfix expression: LB 7 ABC-D"+ESF+ and assume A=‘ ‘Module-2 Q03 Ta [Develop @ C program to implement insertion, deletion and display |, , fa ‘operations on Linear queue. & | Write @ program in C to implement a stack of integers using a singly |, 5 7 linked list. OR Q04 Ta | Writea Cprogram to implement insertion, deletion and display operations |, a ona circular queue. b| Write the C function to add two polynomials. Show the linked representation of the below two polynomials and their addition using a circular singly linked list PL: 5x3 + 4x2 +7x +3 3 10 P2: 6x2 +5 Output: add the above two polynomials and represent them using the linked list. Page 01 of 03 BCS304 Module-3 05 Write recursive C functions for inorder, preorder and postorder traversals of a binary tree. Also, find all the traversals for the given tree, ® @® © © OO@® ®O 13 Write C fanctions for the following i) Search an element in the singly linked list. ii) Concatenation of two singly linked list 12 Define Sparse matrix. For the given sparse matrix, give the linked list representation: A-f00304 570 000 600 Soe 13 OR 206 Write C Functions for the following i) Inserting a node at the beginning of a Doubly linked list Deleting a node at the end of the Doubly linked list 3 Define Binary tree. Explain the representation of a binary tree with a suitable example. 12 Define the Threaded binary tree. Construct Threaded binary for the following elements: A, B, C, D, E, F, G, HT LB Module-4 QT Design an algorithm to traverse a graph using Depth First Search (DFS). Apply DFS for the graph given below. QQ O-@ Wadd 3 Construct a binary tree from the Post-order and Tn-order sequence given below In-order: GDHBAEICF Post-order: GHDBIBFCA 12 Define selection tree, Construct min winner tree for the runs of a game given below. Each run consists of values of players. Find the first 5 winners 10[9 [2076 [8 [9 [90] 17 15] 20] 20 [15] 15 [11 | 95 [18 16 | 38 | 30 [25 | 50 | 16 | 99 [20 28 12 Page 02 of 03 BCS304 OR Qos Define Binary Search tree. Construct a binary search tree (BST) for the following elements: 100, 85, 45, 55, 120, 20, 70, 90, 115, 65, 130, 145. Traverse using in-order, pre-order, and post-order traversal techniques. Write recursive C functions for the same, 13 Define Forest. Transform the given forest into a Binary tree and traverse using inorder, preorder and postorder traversal. Q Q GQO® ® O8 © 12 Define the Disjoint set. Consider the tree created by the weighted union function on the sequence of unions: union(0,!), union(2,3), union(4,5), union(6,7), union(0,2), union(4,6), and union(0,4). Process the simple find and collapsing find on eight finds and compare which find is efficient. 12 Module-5 Q09 ‘What is chained hashing? Discuss its pros and cons. Construct the hash table to insert the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table ‘of 9 memory locations. Use h(k) = k mod m. 3 Define the leftist tree. Give its declaration in C. Check whether the given binary tree is a leftist tree or not. Explain your answer. OQ O. x D ay) 6O2Kf OR POO 56 © G00 12 What is dynamic hashing? Explain the following techniques with examples: 4) Dynamic hashing using directories ii) Directory less dynamic hashing 2 ‘OR Q0 ‘What is a Priority queue? Demonstrate functions in C to implement the Max Priority queue with an example. i) Insert into the Max priority queue ii) Delete into the Max priority queue iii) Display Max priority queue L3 Define min Leftist tree. Meld the given min leftist trees. @) 12 Define hashing. Explain different hashing functions with examples. Discuss the properties of a good hash function. 12 Page 03 of 03 1a) Define data structures. With a neat diagram, explain the classification of data structures with examples. Data Structure: It can be defined as a method of storing and organizing the data items in the computer's memory. Mainly deal with Storing data in memory Organizing data in memory Fetching and processing data — Integer > Floatingpoint Primitive — > Character > Double > Pointers Data structure —— = > Linear Stacks ~ Non-primitive > Queues > Linked Lists > Trees Non-Linear | Graphs Primitive data structure The data structures, that are directly operated upon by machine level instructions i. the fundamental data types such as > int, float, > char >double. ‘A data structure that cannot be manipulated directly by machine instructions are called non-premitive data structure . ex: arrays, stacks, queues ,lists Files, trees, graphs. There are two types of-non primitive data structures. Linear and Non-Linear Data Structures: Ina linear data structure, the data items are arranged in a linear order or sequential. For Example: array, Stack, lists, queue. Ina non-linear data structure, the data items that are not in sequence. For Example: trees and graphs. An array is a sequence of data item of homogeneous value(same type). Stacks A stack is a linear data structure in which an element may be inserted or deleted only at one end called the top end of the stack Queue is a linear data structure in which insertion can take place at only one end called rear end and deletion can take place at other end called front end A linked list is a data structure which is collection of zero or more nodes with each node consisting of two field’s data and link A tree is @ nonlinear data structure and is generally defined as a nonempty finite set of elements, called nodes. ‘A graph normally a combination of the set of vertices V and set of edges E. G=(V,E) 1b) what do you mean by pattern matching? Outline the Knuth Morris Pratt (KMP) algorithm and illustrate it to find the occurrences of the following pattern. Pattern: ABCDABD String: ABC ABCDAB ABCDABCDABDE It isa fundamental problem in computer science where the goal is to find all occurrences of a given pattern within a larger text or string. ‘The Knuth-Morris-Pratt (KMP) algorithm is a widely used pattern matching algorithm that efficiently searches for occurrences of a pattem within a text by exploiting the information gathered during the preprocessing phase KMP algorithm works in linear time O(n + m), where n is the length of the text and m is the length of the pattern, Outline of the Knuth-Morris-Pratt algorithm: Preprocessing: Construct an auxiliary array, called the failure function or failure array, Which is used to indicate potential fallback positions in the pattern to avoid redundant comparisons. Search: Use the failure function to guide the search process, avoiding unnecessary comparisons by efficiently moving through the text. DABCDABDE SERUEEEN eae Align the pattern at the beginning of the string. Compare characters until a mismatch is found or the pattern is exhausted ISiring: ABC ABCDAB ABCDABCDABDE ABC Neen CENTERS DLUIE a) RSP SEEN: ABCDABD] ABC ABCDAB ABCDABCDABDE Neon ABCDAB ABCDABCDABDE Renee ner “sTRING abcraabedabaabed avedabe rar = Ab © Ag b “t RES Aahe abedatey abedabey abedabe abedabe abrdabey abodabe abcadav abe dable oe abcdae abedabe abe d abe y Cpattcrn jourd ) Algorithm or function (write 2" lab pgm void string match function) void stringmateh() { while(str{c] !=10°) { if{str[m] = patfi)) { ity mH iffpaifi] = 01) { flag = 1; for(k=0; rep[K]!="0's k++, j++) t ans{j] = replk]: } c=m; 4 } else ans{j]= str{c]; dicts m=c; in0; } } 3 ‘1c)Write a program in C to implement push, pop and display operations for stacks using arrays. Hinclude int stk{10], ch, n, top==1, item, is void push() ‘ iftop>=n-1) : printi("STACK is over flowin"); bee t printf(" Enter a value to be pushed:"); seanf{"%d", &item); top++: stk[top]=item; printf(” pushed successfully Dy void pop) t ifttop = -1) t printf("Stack is under flow\n"); 3 else { printf("The popped elements is %d\n", stk{top]); top } 3 void display() { if top = -1) { printf(" STACK is empty \n"); } else t printf{"The elements in STACK forti-top; i>=0; i--) printf("%d\n" stk); 3 void main() t printf("Enter the size of STACK\n"); scanft"%d", &n); for(;) { printf(" I. PUSH 2.POP 3.DISPLAY \n"); printf("Enter the Choice:\n"); seanf{"%d",&ch); switch(ch) { case I: push(); break; case 2: pop(); break; case 3: display(); break; 2.a) Explain in brief the different functions of dynamic memory allocation Dynamic memory allocation is the process of allocating memory during run time(execution time) Additional storage can be allocated whenever needed. 1. malloc( ): Allocates requested number of bytes and returns a pointer to the first byte of the allocated space. Synta ptr(datatype *)malloc(sizeof{datatype); where, + ptris a pointer variable of type datatype + datatpe can be any of the basic datatype or user define datatype + Size is number of bytes required. Example: int *p; ptr=(int*)malloc(100*sizeof(int)); The calloc Function 5 It stands for contiguous allocation, It is used to allocate multiple blocks of memory. It requires two parameters as number of elements and size of each element, Syntax: ptr-(datatype *)calloc(n , sizeof(Datatype); where, ptr is a pointer variable of type datatype datatype can be any of the basic datatype 1n is number of blocks to be allocated size is number of bytes required int “p; ptr=(int*)calloc(100,sizeof(int)); realloc() It changes the size of block by deleting or extending the memory at end of the block. + Ifmemory is not available it gives complete new block. Synta: ptr=(datatype *)realloc (ptr , sizeof{datatype); where, + ptr is a pointer to a block previously allocated memory either using malloc() or calloc() + Size is new size of the block. ptr=(int*)realloc(ptr,sizeof(int)); free() int*)calloc(100,sizeof(int)); This function is used to de-allocate(or free) the allocated block of memory which is allocated by using functions malloc(), calloc(), realloc(). Syntax: free(ptr); 2b)Write functions in C for the following operations without using built-in functions ‘Compare two strings. il) Concatenate two strings. i) Reverse a string int scomp(char si}, char s2{]) { ‘ini iffslen(s1) != slen(s2)) ‘ return 0; foto; sl[i] != 0's i++) ‘ ifts][i] = s2[i]) ‘ return 0; " 1 return 1; void reverse() { char str{100}, temp: "Enter The String: "; gets(ste) iO; j= strlen(ste)- 1; while ( float compute(char symbol, float op1, float op2) { switch (symbol) { case'#: return opi + op2; case return opi -op2; case'*: return opi * op2; case’: return opt / 0p2; case case: return pow(op1,op2); ) ) void main() { float s{20], res, opt, 0p2; int top, i; char postfx{20], symbol; printf("\nEnter the postfix expression:\n"); scanf ("%s", postfix); icstrlen{postfix) i++) symbol = postfix; if(isdigit{symbol)) res = sltop~]; printf("\nThe result is: %f\n", res); } BC-D*+ESF+ and assume A=6, B=3, C=2, Land F=7. ABC-D*+ESF+ 632.57+157+ Ans :8 Refer class notes for solving the steps you need to write the steps also 3a Develop aC program to implement insertion, deletion and display operations on Linear queue Hinclude int q[10), eh, size, front ‘void enquewe() t Jitem, i; if rear == size - 1) print{("Queue Overflow\n"); else { if(front—=- 1) front = 0; printf("Inset the element in queue\n: "); scanf{"%d", &citem); gfrear++] = item; } 5 void dequeue() printf("Queue Underflow\n"); } else { printf("Element deleted from queue is : %d\n", qlfront)}; front++; } } void display() 1 if{front— -1 || ront>rear) t printf(" Queue is empty\n"); else t printf(” Queue is :\n"); for(i = front; i <= rear; i++) printf("%ed\n", qf } void main() t printf("Enter the size of STACK\n"); scanf("%d", &size); forts) f printf(" insert 2.delete 3. DISPLAY \n"); printf("Enter the Choice:\n"); scant("%d",&ch); switch(ch) { case 1: enqueue(); break; case 2: dequeue();break; case 3: display(); break; 3b Write a program in C to implement a stack of integers using a singly linked Hinclude Hinclude #include struct node { int data ; struct node* next; }*tmp,*ptr,*head=NULL; struct node* createnode() { ptr=(struct node*)malloc(sizeof(struct node)); printf("Enter data \n"); scanf("%d",&ptr->data); ptr->next=NULL; return ptr; } void insertfront() { ptr=createnode(); if(head!=NULL) ptr->next=head; head=ptr; } void insertend() { ptr=createnode(); ptr->next=NULL; tmp=head; if (head!=NULL) { while(tmp->next!=NULL) tmp=tmp->next; tmp->next=ptr; } else head=ptr; } void delfront() { tmp=head; if(head==NULL) printf("THE LINKED LIST IS EMPTY\t else { printf("THE DELETED NODE is %d\n" tmp->data); head=tmp->next; free(tmp); } } void delend() { ptr=head; if (nead==NULL) printf("LINKED LIST IS EMPTY\n"); else { while(ptr->next->next!=NULL) ptr=ptr->next; tmp=ptr->next;, ptr->next=NULL; printf("deleted node is %d" tmp->data); free(tmp); } } void display() { iffhead==NULL) { printf("List is empty while (tmp!=NULL) { printf("%d\n tmp->data); mp->next; printf("THE NO. OF NODES ARE %d\n",c); } } void main() { int ch; for(;;) { printf("Enter your choice\n"); printf("1-create 2-display and count 3-Insert front\n 4.insert end \n 5.Delete frontnsceleton trom enn scanf("%d", &ch); switch(ch) if case 1:create(); break; case 2: display(); break; case 3: insertfront(); break; case 4: insertend(); break; case 5: delfront(); break; case 6: delend(); break; } } } 4a Write aC program to implement insertion, deletion and display operations on a circular queue ( dsa lab pgm 6") #include int cq(10], front = -1, rear = -1,item,ch,i,size; void enQueue() { if(front ==(rear+1)%SIZE) { printf("C Q IS OVERFLOW\n"); } else { rintf("enter element\n"); scanf("%d",&item); cqfrear] printf("Inserted\n"); } } void deQueue() { if(front { printf("CIRCULAR QUEUE IS UNDERFLOW\n"); } else { printf("\n Deleted element -> %d \n", caffront]); if (front == rear) front = rear = -1; else 1) front = (front + 1) % SIZE; } } void display() { if(front == -1) { printf("CIRCULAR QUEUE IS empty\n"); } else { printf("Items are \n"); for (i= front; i != rear; i= (i + 1) % SIZE) { printf("%d\n", calil); } printf("%d\n", cali); } } void main() { for(;;) { printf("enter cq size !!!\n"); scanf("%d” &size); printf(".Insert 2.delete 3.display 4.Exit\n"); printf("Enter your choice:\n"); scanf("%d", &ch); switch(ch) { case 1:enQueue();break; case 2:deQueue();break; case 3:display();break; } } } 4b Write the C function to add two polynomials. Show the linked representation of the below two polynomials and their addition using a circular singly linked list P1: 5x3 + 4x2 +7x + 3 P2: 6x2 +5 Output: add the above two polynomials and represent them using the linked list sie EE oy +i 5a Write recursive C functions for inorder, preorder and postorder traversals of a binary tree. Also, find all the traversals for the given tree. void inorder(struct node *root) { Inorder :D BHEIAFCG (APPLYSHRUA SRW if(root != NULL) { inorder(root->left); Bremer AB DELCES printf("%d\t", root->data); inorder(root->right); } } Post order: DHIEBFGCA void preorder(struct node *root) { iffroot != NULL) { printf("s6a\t", root->data); preorder(root->left); preorder(root->right); } } void postorder(struct node *root) { if(root != NULL) { postorder(root->left); postorder(root->right); printf("36d\t", root->data); yy 5 b) Write C functions for the followit Concatenation of two singly linked list 1) Search an element in the singly linked li ‘Search void search(struct node *head,int key) ( struet node *temp = head; while(temp != NULL) { ifftemp->data — key) printf(*key found”); temp = temp->next; } printf(*key not found”); void Concat(struct Node “first, struct Node *second) { struct Node * first; while (p->next != NULL) { p= p-next; Se Define Sparse matrix. For the given sparse matrix, give the linked list representation: Sparse matrix is a special matrix made of m rows and n columns, therefore having total m x n values with most of its elements are zero. We can also assume that if (m * n) /2 elements are zero then it is a sparse matrix. 6a) Write C Functions for the following i) Inserting a node at the beginning of a Doubly linked list ii) Deleting a node at the end of the Doubly ed list void insertfront() ne if{start—=NULL) {oe start=last=ptr; } else { ptr->left=NULL; ptr>right=start; start->lefi=ptr; start=ptr; } } void deleteend() { temp=start; while(temp->right!=NULL) { temp=temp->right; } last=temp-rleft; last->right=NULL; temp->left=NULL; printf("element is deleted \n"); free(ptr); } 6b Define Binary tree. Explain the representation of a binary tree with a suitable example. Definition: A binary tree T is defined as a finite set of nodes such that, + Tis empty or + Tconsists of a root and two disjoint binary trees called the left sub tree and the right sub tree, Figure: Binary Tree Representation of a binary tree: 1.Array representation 2. Linked representation Array representation: ‘A tree can be represented using an array, which is called sequential representation, ‘* The nodes are numbered from I to n, and one dimensional array can be used to store the nodes. « Position 0 of this array is left empty and the node numbered i is mapped to position i of the array. 2B Root node a[0] joe SQ fa) Left Child node 2i+1 f é . outs Right Child node 2i+2 QO®O® { @ od we Figere 10 Const tyne 6 mb Root node > af0] KH v1 Left Child node > 2i+a Right Child node > 2i+2 Linked representation: Each node has three fields, ‘ LeftChild - which contains the address of left subtree ‘© RightChild - which contains the address of right subtree. ‘* Data - which contains the actual information Figure 1(b) Complete binary tree 6c) Define the Threaded binary tree. Construct Threaded binary for the following elements: A, B, C, D, E, F, G, H, I Ina threaded binary tree, each node has an additional pointer called a thread, which points to either its in-order predecessor or successor. This allows us to efficiently traverse the tree without using recursion or a stack. Right in Threaded binary tree HDIBEAFCG Af there ts no In-order predecessor or in-order successor, then It points to root 7 a Design an algorithm to traverse a graph using Depth First Search (DFS). Apply DFS for the graph given below. Depth First Search: Depth First Search (DFS) algorithm traverses a graph ina depth ward motion and uses a stack to remember to get the next vertex to start a search DFS Algorithm : Step 1 — Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it ina stack Step 2 = If no adjacent vertex is found, pop up a vertex from the stack. (which do not have adjacent vertices.) Step 3 — Repeat Rule | and Rule 2 until the stack is empty 7b) Construct a binary tree from the Post-order and In-order sequence given below In-order: GDHBAEICF Post-order: GHDBIEFCA. Te Define selection tree. Construct min winner tree for the runs of a game given below. Each run consists of values of players. Find the first 5 winners SELECTION TREE + This is also called as a tournament tree. This is such a tree data structure using which the winner (or loser) of a knock out tournament can be selected. + There are two types of selection trees namely: winner tree and loser tree. WINNER TREE + This is a complete binary tree in which each node represents the smaller of its two children. Thus, the root node represents the smallest node in the tree. 689 OY ay @ Ko Ie ol sbu ) TOS 3) 1) lls Oo) my BL Bel Ee 8a Define Binary Search tree. Construct a binary search tree (BST) for the following elements: 100, 85, 45, 55, 120, 20, 70, 90, 115, 65, 130, 145. Traverse using in-order, pre-order, and post-order traversal techniques. Write recursive C functions for the same. Definition of binary search tree: + Every element has a unique key + The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree + The left and right subtrees are also binary search trees Construction of binary search tree Recursive C functions 8b Define Forest. Transform the given forest into a Binary tree and traverse using inorder, preorder and postorder traversal. FORESTS = This Is a set of n>=0 disjoint trees (Figure 5.34), SOo ‘TRANSFORMING A FOREST INTO A BINARY TREE Ty, To: « - .To IS a forest of trees, then the binary tree corresponding to this forest, denoted by ba i, FOREST TRAVERSALS ‘* There are 3 forest traversal techniques namely: preorder, inorder and postorder traversal. + Preorder traversal of forest F can be recursively defined as follows 1) If F is empty then return. 2) Visit the root of the first tree of F. 3) Traverse the subtrees of the first tree in forest preorder. 4) Traverse the remaining trees of Fin forest preorder. + tnorder traversal of forest F can be recursively defined as follows | HinilsafieforLVR, VERLRV 41) IF is empty then return. 2) Traverse the subtrees of the first tree in forest inorder. +3) Visit the root of the first tree of F. 4) Traverse the remaining trees of F in forest inorder. + Postorder traversal of forest F can be recursively defined as follows 1) If Fis empty then return. 2) Traverse the subtrees ofthe fist tre in forest postorder. 3) Traverse the remaining trees of F in forest postorder, 44) Visit the root of the first tree of F. 8c Define the Disjoint set. Consider the tree created by the weighted union function on the sequence of unions: union(0,1), union(2,3), union(4,5), union(6,7), union(0,2), union(4,6), and union(0,4). Process the simple find and collapsing find on eight finds and compare which find is efficient. A set is a collection of elements. Disjoint sets are such sets in which common elements are not present. Examples: a a % Algorithm collapsing_find(i) { while(p[r]>0) return r; 9a What is chained hashing? Discuss its pros and cons. Construct the hash table to insert the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table of 9 memory locations. Use h(k) = k mod m Chained hashing, also known as separate chaining, is a technique used to resolve collisions in hash tables. When two or more keys hash to the same index (known as a collision), chained hashing handles these collisions by maintaining a linked list of all elements that hash to the same index. Pros of Chained Hashing: 1Simple Implementation: Chained hashing is relatively easy to implement. 2.Efficient Insertion and Deletion: Insertion and deletion operations are efficient in chained hashing. 3. Dynamic Data Sets: Chained hashing allows for dynamic resizing of the hash table. 4.Adaptability: Chained hashing can handle a wide range of input data distributions Cons of Chained Hashing: 1 Memory Overhead: Chained hashing can have a higher memory overhead compared to other collision resolution techniques. 2.Cache Inefficiency: cache inefficiency, particularly for large hash tables with long linked lists. 3.Performance Degradation: 4.Poor Worst-Case Performance: Chained hashing does not guarantee constant-time performance for lookup operations in the worst case Solution: 9 b) Define the leftist tree. Give its declaration in C. Check whether the given binary tree is a leftist tree or not. Explain your answer. A leftist tree is a binary tree such that if it is not empty, then shortest (LeftChild (x) >= shortest (RightChild (x ) for every internal node x. C declaration struct node t int data; struct node *left; struct node *right; int rank; hb 9c) What is dynamic hashing? Explain the following techniques with examples: i) Dynamic hashing using directories ii) Directory less dynamic hashing. Dynamic hashing is a mechanism for dynamically adding and removing data buckets on demand, Dynamic hashing using directories: ¥ It isa method of hashing in which the data structure grows and shrinks Y Dynamically as records are added or removed. ¥ Itis also known as extendible hashing. ¥ This method makes hashing dynamic, i.e., it allows insertion or deletion without resulting in poor performance. Buckets: The buckets are used to hash the actual data. Directories: They are the holders of pointers pointing towards these buckets. Each directory has a unique ID. Global Depth: They denote the number of bits which are used by the hash function to categorize the keys. Local Depth: It is associated with the buckets. Working: — Initialize the bucket depths and the global depth of the directories. = Convert data into a binary representation. — Consider the "global depth" number of the least significant bits (LSBs) of data, — Map the data according to the ID of a directory. — Check if'a bucket overflows or not. Example 2: Given AO. ALDO,BI.C1,€2.€3. et wnro Directory less dynamic hashing Also known as linear dynamic hashing. Directory less hashing assume a continuous address space in the memory to hold all the records. Therefore, the directory is not needed. Thus, the hash function must produce the actual address of a page containing the key. Each active bucket has 2 slots. Insert B4, AO, Al, BS, C2 and C3 Overfow 8 lat ae |% wo |” “t oxo | * aL al 8s oo om tea a a a a0 a | a a |? ou a |¢ _ ea 100 wo | 85] newactve 4 Ls) bucket 10 a) What is a Priority queue? Demonstrate functions in C to implement the Max Priority queue with an example. i) Insert into the Max priority queue ii) Delete into the Max priority queue iii) Display Max priority queue It is a collection of ordered elements that provides fast access to the minimum or maximum element. i) Insert into the Max priority ii) Delete into the Max priority. elenert. deleve-naxceaplint*t) { 1* delete eleent vith the highest key fron the heap */ void insertanax-heep(elenent iter, int tn) { ‘insert item into a max heap of current size "int parent, chil int iy element iten, temp; Sf (HEAP-FULL(¢)){ 4f (@Ene_pery("n)) fprintf(stderr, "The heap is full, \nt); fprintE(stderr, *The heap is empty\n); exit); exit } ) salen); * save value of the elenet with the highest key * ) Wt (iten.key > heapti/2}.key!) { ~ heapi/2I; use last elerent in heap to adjust heap « exp = haan] ten; < tn) tk (heapfehild) key < eap[child] key) break; the next Lover Le 1d): Display Max priority queue void display_pqueue() if printf("\nQueue is empty"); return; } for (; front <= rear; front++) { printf" %d " } i_quelfront]); front = 0; 10 b) Define min Leftist tree. Meld the given min leftist trees. Definition: A min-leftist tree (max leftist tree) is a leftist tree in which the key value in each node is no larger (smaller) than the key values in its children. Given: 10 c) Define hashing. Explain different hashing functions with examples. Discuss the properties of a good hash function. A function that converts a given big number to a small integer value. The mapped integer value is used as an index in the hash table. hash Location = KEY % SIZE ‘Types of Hash Functions- = Mid Square Hash Function — Division Hash Function — Folding Hash Function. — Digit analysis — Converting keys to integers Division Method In this method we use modular arithmetic system to divide the key value by some integer divisor m (table size). x=23, m= 10 then : H(x) = (23 % 10) =3 1 The key whose value is 23 is placed in 3rd location 3 2 4 Midsquare Methods In this case, we square the value of a key and take the number of digits required to form an address, from the middle position of squared value. Suppose a key value is 16, then its square is 256. Now if we want address of two digits, then we select the address as 56 (ie. two digits starting from middle of 256). Folding Method The key is actually partitioned into number of parts, each part having the same length as that of the required address. This is done in two ways : Fold-shifting Fold-boundary Fold-shifting: Here actual values of each parts of key are added Example: The key is : 12345678, and the required address is of two digits, Then break the key into: 12, 34, 56, 78 Add these, we get 12 + 34 + 56 +78 = 180 Ignore first 1 we get 80 as location (because its two digits) Fold-boundary: Here the reversed values of outer parts of key are added. Example: The key is : 12345678, and the required address is of two digits, Then break the key into: 12 34 56 78 Reverse the keys (121, 34, 56, 87. Add these, we get 21 + 34 + 56 + 87: 198, ignore first 1 we get 98 as location (Bez digits is two) Digit Analysis Here we make a statistical analysis of digits of the key, and select those digits (of fixed position) which occur quite frequently. For example, if the key is : 9861234. Here third and fifth position digits occur quite frequently, then we choose the digits in these positions from the key. So we get, 62. Reversing it we get 26 as the address

You might also like