Practise Numericals

Here are few examples that will help you to prepare for any Data Structure Examination

Take an array of numbers ” 5 1 4 2 8“, after first pass the sequence will be “1 4 2 5 8

Explanation as follows:   Sequence of First Pass can be found as follows

( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), compares the first two elements, and swaps since 5 > 1.

( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5).

358416129

write only the state of an array after each pass (make sure that algorithms behaves in adaptive manner)

Solution : the sorting is shown in following steps (click for video explanation)

Array358416129
1st Pass354168912
2nd Pass341568912
3rd Pass314568912
4th Pass134568912
5th Pass134568912
6th Pass
7th Pass

Note : Since algorithm is adaptive, therefore it exits after 5th pass once the array becomes sorted. For non-adaptive algorithm it will iterate all 7 passes in which pass 6 and pass 7 will have same ordering of data as pass 5.

10176220
21761020
26171020
26101720
26101720
26101720

Problems on Linked List

Solution  Following are the key statements (main logic) to get the desired list.

  • cur = Last1->next;      ( curr now point to node with data value 3)
  • Last1->next = Last2->next;( node value 9 in list1 will point to node value 33 in list 2)
  • Last2->next = cur;      (last node of list 2 now connect to first node of list 1, hence making circular list)

Alternatively, we can also use the following

  • Node *next1 = last1->next;   Node *next2 = last2->next;
  • last1->next = next2;      last2->next = next1;

  • last1= Start;
  • while(last1 ->next!=NULL)  { last1 = last1->next; }
  • last1->next = head;   head->prev = last1;

Note : the time complexity is O(n) due to while loop that goes n-times, where n is the number of nodes in first list.

Problems on Stack

STACK: AAA , DDD, EEE, FFF, GGG ( this is present status of stack, AAA is the top element)

Describe the stack as the following operations take place:

  PUSH(KKK), POP( ), PUSH(LLL), PUSH(SSS), POP( ), PUSH(TTT)

Operation     STACKComment (if any)
 AAA , DDD, EEE, FFF, GGG  
PUSH(KKK)  KKK,  AAA , DDD, EEE, FFF, GGG  
POP( )  AAA , DDD, EEE, FFF, GGG  
PUSH(LLL)  LLL,  AAA , DDD, EEE, FFF, GGG  
PUSH(SSS)  LLL,  AAA , DDD, EEE, FFF, GGG OVERFLOW
POP( )  AAA , DDD, EEE, FFF, GGG  
PUSH(TTT)  TTT,  AAA , DDD, EEE, FFF, GGG    
  • Q1 – (A * B) + (C / D) – (D + E)
  • Q2 – (A – B) + D / ((E + F) * G)
  • Q3 – (A – 2 * (B + C) / D * E) + F
Using Method-1 ((A * B) + (C / D) – (D + E)    
CharStackExpression
)), ) 
E), )E
+), ), +E
D), ), +ED
()ED +
), –ED +
)), -, )ED +
D), -, )ED + D
/), -, ), /ED + D
C), -, ), /ED + DC
(), –ED + DC /
+), -, +ED + DC /
)), -, +, )ED + DC /
B), -, +, )ED + DC / B
*), -, +, ), *ED + DC / B
A), -, +, ), *ED + DC / BA
(), -, +ED + DC / BA *
( – + * A B / C D + D E
(on reversing)
Using Method-1((A – B) + D / ((E + F) * G)
CharStackExpression
)), ) 
G), )G
*), ), *G
)), ), *, )G
F), ), *, )GF
+), ), *, ), +GF
E), ), *, ), +GFE
(), ), *GFE +
()GFE + *
/), /GFE + *
D), /GFE + * D
+), +GFE + * D /
)), +, )GFE + * D /
B), +, )GFE + * D / B
), +, ), –GFE + * D / B
A), +, ), –GFE + * D / BA
(), +GFE + * D / BA –
 ( GFE + * D / BA – +
Ans on reversing+ – A B / D * + E F G
((A – 2 * (B + C) / D * E) + F
CharStackExpression
F)F
+), +F
)), +, )F
E), +, )FE
*), +, ), *FE
D), +, ), *FED
/), +, ), *, /FED
)), +, ), *, /, )FED
C), +, ), *, /, )FEDC
+), +, ), *, /, ), +FEDC
B), +, ), *, /, ), +FEDCB
(), +, ), *, /FEDCB +
*), +, ), *, /, *FEDCB +
2), +, ), *, /, *FEDCB + 2
), +, ), –FEDCB + 2 * / *
A), +, ), –FEDCB + 2 * / * A
(), +FEDCB + 2 * / * A –
( + – A * / * 2 + BCDEF

Question : Evaluate the PREFIX expressions [ – + * A B / C D + D E ] using stack 

[assume A=2,B=3, C=2, D=4, E=3, F=2, G=3]

Solution-1:– step by step using STACK (click for video explanation)

[- + * A B / C D + D E] è   – + * 2 3 / 2 4 + 4 3
CharStackOperation
33 
43, 4 
+74 + 3 = 7
47, 4 
27, 4, 2 
/7, 02 / 4 = 0
37, 0, 3 
27, 0, 3, 2 
*7, 0, 62*3=6
+7, 66+0
-16-7=-1
   

Solution-2 step by step using STACK

[+ – A B / D * + E F G] è   + – 2 3 / 4 * + 3 2 3
CharStackoperation
33 
23, 2 
33, 2, 3 
+3, 53 + 2 = 5
*155 * 3 = 15
415, 4 
/04 / 15 = 0
30, 3 
20, 3, 2 
0, -12 – 3 = -1
+-1-1 + 0 = -1
   

Question : Convert the following INFIX expressions to their POSTFIX equivalents

Evaluate the POSTFIX expression 2 3 * 2 4 / + 4 3 + –

Problems on Queues

Question : Demomstrate the ENQUEUE/DEQUEUE operations on a Circular queue with size (N = 5). Show the status of Rear & Front after every operation

Question : Demomstrate the ENQUEUE/DEQUEUE operations on a Circular queue with size (N = 5). Show the status of Rear & Front after every operation

Perform Operations on DOUBLE ENDED QUEUE

Problems on Binary Trees

Preorder        25 15 10 4 12 22 18 24 50 35 31 44 70 66 90

Postorder       4 12 10 18 24 22 15 31 44 35 66 90 70 50 25

Question : Construct an Expression tree for Postfix expression 

Question : Construct the Expression tree for Infix expression 

(a + b) – (c * d) % (f ^g) / h – i

Problems on Huffman Encoding

Question : For the string aaaapppsssappss, find the number of bits required if encoded using

  • Default Encoding
  • Uniform length encoding   
  • Variable length encoding
  1. Default Encoding:15*8 = 120bits
  2. Uniform length encoding:(8*3 + 3*2) + (15*2) = 30 + 30 = 60bits
  3. Variable length encoding:(8*3 + 5) + (5*1 + 5*2 + 5*2) = 29 + 15 = 54bits

Problems on Threaded Binary Tree

Question : write necessary statements required to perform the operations in given threaded tree

 =>  Delete 30

 =>  Delete 10

Solution : ( click for video explanation)

Delete 30 (have no child – 1st case)

  • parent->r_thread = true;
  • parent->right = ptr->right;
  • free ptr;

Delete 10 (have two child – can use 1st case)

  • s = inSucc(ptr);
  • ptr->data = s->data;
  • parent->l_thread =true;
  • parent->left = s->left;
  • free s;

Problems on B – Trees

Question : Insert the keys in B Tree of order 3   

[  45, 89, 90, 10, 35, 82, 15, 40, 80 ]

Question : Insert keys 2, 6, 10, 20, 78, 5, 90, 25, 4, 80, 85, 82 in B Tree of order 4

Question : Delete the keys in B Tree of order 3  

Delete

  • Node 85
  • Node 20
  • Node 10

Question : Delete the keys 82, 20, 78, 6, 85, 10 in given B Tree of order 4

  Question :   Delete 25, 15, 32, 64, 12 from the given Btree of order 3 

Problems on B + Trees

Question : Insert  2, 6, 10, 20, 78, 5, 90 in B+ tree of Order 3

Question : Delete  20, 10, 6 in a given B+ tree of Order 3