Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C program segment
    struct Cell Node {
        struct Cell Node *left Child;
      int element;
       struct Cell Node *right Child;
       }
    int Dosomething (struct Cell Node *ptr)
    {
       int value = 0;
       if (ptr! = NULL)
        { if (ptr-> left Child! = NULL)
         value = 1 + Dosomething (ptr -> left Child);
         if(ptr-> rightChild! = NULL)
         value = max (value, 1 + Dosomething (ptr- > rightChild));
         }
      return (value);
    }
    The value returned by the function Dosomething when a pointer to the root of a non-empty tree is passed as argument is
    1. the number of leaf nodes in the tree
    2. the number of nodes in the tree
    3. the number of internal nodes in the tree
    4. the height of the tree
Correct Option: D

Value initialized by 0. If any root node has left child then it adds 1 to the value & move to left child & if any mode has right child also then also calculated the value using recursion & take maximum of both left & right value is taken.
So we know that height is the largest distance between root node & leaf. Therefore, this program calculates heights.
Hence (d) is correct option.



Your comments will be displayed only after manual approval.