Homechevron_rightEngineeringchevron_rightComputer Sciencechevron_rightData Structurechevron_rightWhich of the following data structures is used in implementing recursi...

Which of the following data structures is used in implementing recursi...

  • Q. Which of the following data structures is used in implementing recursive calls ?
  • filter_dramaExplanation
    Answer is : B

    Answer: Option 2

    Explanation

    - For implementing recursive calls, Stack (LIFO) is required. Because for each call to the function, the caller function's temporary variables need to be pushed into the stack so that at the time of backtracking variables are easily accessible.

    for example: 

    Consider the following Fibonacci example Where fib5 make a call to fib4 then fib5 will be pushed into the stack and then fib4 make a call to fib3, fib4 will be pushed into the stack and so on. and when fib1 and fib0 complete then backtracks to fib3 reusing the variable of fib3 which are stored in the stack.

    Additional Information

    Hash Table

    A hash table is a data structure that is used to store keys/value pairs. It uses a hash function to compute an index into an array in which an element will be inserted or searched. By using a good hash function, hashing can work well. Under reasonable assumptions, the average time required to search for an element in a hash table is O(1).

    Queue

    The queue is FIFO (First in First out) data structure i.e. the first element that is added to the queue is the first one to be removed. Elements are always added to the back (Rear) and removed from the front.

    Binary Tree:

    A Binary tree consists of nodes which are having data part and a pointer pointing to the left subtree and a right pointer pointing to the right subtree.

    If a tree is empty, it is represented by a null pointer.

Discussion

    No one started the discussion yet. Break the ice and start the conversation.
    Please Login to be part of the discussion.

Similar Questions

  • 1. A recursive problem like tower of hanoi can be rewritten without recursion using:
  • filter_dramaExplanation
    Answer is : A

    Concept:

    • A stack is an ordered list in which insertion and deletion are done at one end, called a top.
    • The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list.

    Explanation:

    A recursive problem like the Tower of Hanoi can be rewritten using system stack or user-defined stack

    Recurrence relation of tower of Hanoi:  T(n) = 2T(n - 1) + 1 

    Additional Information

    Number of moves required for n disc in a Tower of Hanoi is 2– 1 = 27 – 1 = 127. 

    Stack underflow happens when one tries to pop (remove) an item from the stack when nothing is actually there to remove.

  • 2. The data structure needed to convert infix notation to prefix notation is
  • filter_dramaExplanation
    Answer is : B

    The stack is an abstract data type that follows an order LIFO (last in first out) to evaluate any expression.

    The element that is inserted at the last into the stack will be the first to get out of the stack.

    Application of the stack:

    1) Converting infix to postfix/prefix expression

    2) parenthesis matching

    3) Expression evaluation etc.

    Explanation:

    Only one stack is enough to evaluate any expression or to convert one form to another form of expression.

    Suppose we have a postfix expression: 15 3 * 10 – 5 /

    For evaluating this:

    1) Push 15 in the stack, push 3 in the stack

    2) when * operator comes, pop 15 and 3 from the stack

    3) Push 15 * 3 = 45 in the stack

    4) push 10 in the stack

    5) when – operator occurs, pop 45 and 10 from the stack

    6) push 45 – 10 = 35 in the stack

    7) push 5 in the stack

    8) when / operator comes, pop 35 and 5 from the stack

    9) Push 35 / 5 = 7 in the stack

  • 3. Which of the following data structures is used in implementing recursive calls ?
  • filter_dramaExplanation
    Answer is : B

    Answer: Option 2

    Explanation

    - For implementing recursive calls, Stack (LIFO) is required. Because for each call to the function, the caller function's temporary variables need to be pushed into the stack so that at the time of backtracking variables are easily accessible.

    for example: 

    Consider the following Fibonacci example Where fib5 make a call to fib4 then fib5 will be pushed into the stack and then fib4 make a call to fib3, fib4 will be pushed into the stack and so on. and when fib1 and fib0 complete then backtracks to fib3 reusing the variable of fib3 which are stored in the stack.

    Additional Information

    Hash Table

    A hash table is a data structure that is used to store keys/value pairs. It uses a hash function to compute an index into an array in which an element will be inserted or searched. By using a good hash function, hashing can work well. Under reasonable assumptions, the average time required to search for an element in a hash table is O(1).

    Queue

    The queue is FIFO (First in First out) data structure i.e. the first element that is added to the queue is the first one to be removed. Elements are always added to the back (Rear) and removed from the front.

    Binary Tree:

    A Binary tree consists of nodes which are having data part and a pointer pointing to the left subtree and a right pointer pointing to the right subtree.

    If a tree is empty, it is represented by a null pointer.

  • 4.

    The Preorder traversal of a tree given below is:

  • filter_dramaExplanation
    Answer is : A

    The correct solution is 'option 1'.

    Key Points

      Algo Preorder(tree root)

      {

    1. Visit the root node.
    2. Traverse the left subtree ( call Algo Preorder(left-subtree) ).
    3. Traverse the right subtree ( call Algo Preorder(right-subtree) ).

      }

    • Start with a root node 'A' move towards it's left subtree i.e 'B'. And again the node 'B' becomes the root node. Recursively calling by the above function prints the pre-order of the above Tree. 

    Thus, the correct answer is: A B D F E C G I H J K L

    Additional Information

    • To build a copy of the tree, a preorder traverse is used. Preorder traversal is often used to get the prefix on an expression tree.
    •  Here, some tree traversal techniques and flow.
    • In-order     -  F D B E A I G C J H L K
    • Pre-order  -  A B D F E C G I H J K L
    • Post-order - F D E B I G J L K H C A
    • Converse Inorder - K L H J C G I A E B D F
    • Converse Preorder- A C H K L J G I B E D F
    • Converse  Postorder- L K J H I G C E F D B A
    Tree traversal

     

    Method flow

    Inorder preorder postorder

    Converse Inorder

    Converse Preorder Converse  Postorder
    1. Left
    2. Root
    3. Right
    1. Root
    2. Left
    3. Right
    1. Left
    2. Right
    3. Root 
    1. Right
    2. Root
    3. Left
    1. Root
    2. Right
    3. Left
    1. Right
    2. Left
    3. Root
  • 5.

    Consider the following recursive function.

    Int function (int x, int y) {

                    If (y <= 0) return x;

                    return function (y, x%y);

                    }

    The above recursive function computes ______.

  • filter_dramaExplanation
    Answer is : B

    Consider x = 10 and y = 25

    CASE1:

    If (y <= 0) return x;                           // 25<=0 (false)

                    return function (y, x%y);            return function(25, 10)

    CASE 2:

    x = 25, y= 10

    If (y <= 0) return x;                          // 10<=0 (False)

                    return function (y, x%y);        // return function (10, 5)

    CASE 3:

    x =10, y =5

    If (y <= 0) return x;                         // 5<=0 (false)

                    return function (y, x%y);             //return function(5, 0)

    CASE 4:

    x = 5, y =0

    If (y <= 0) return x;                         // condition true, return 5

                    return function (y, x%y);

    Output = 5

    Which is greatest common divisor of 10 and 25.

    So, given program computes GCD of x and y

Data StructureTopics

leaderboardLeaderboard
  • Rahul Kumar

    191 Points

  • VIKRAM JEET

    54 Points

  • GEETHIKA CHOWDARY

    53 Points

  • sunita saini

    52 Points

  • Zain

    49 Points