Question 4107 – Software-design
May 16, 2024
Question 6212 – Operating-Systems
May 16, 2024
Question 4107 – Software-design
May 16, 2024
Question 6212 – Operating-Systems
May 16, 2024

Question 9270 – Data-Structures

Consider the following C program segment where CellNode represents a node in a binary tree:

  struct CellNode {
      struct CellNOde *leftChild;
        int element;
      struct CellNode *rightChild;
  };
 
  int GetValue(struct CellNode *ptr) {
      int value = 0;
      if (ptr != NULL)            {
        if ((ptr->leftChild == NULL) &&
              (ptr->rightChild == NULL))
           value = 1;
  else
           value = value + GetValue(ptr->leftChild)
                         + GetValue(ptr->rightChild);
    }
    return(value);
  }

The value returned by GetValue() when a pointer to the root of a binary tree is passed as its argument is:

Correct Answer: C

Question 76 Explanation: 
Let take example,

So from applying algorithm to above tree we got the final value v=3 which is nothing but no. of leaf nodes.
Note that height of the tree is also 3 but it is not correct because in algorithm the part
if ((ptr → leafchild = = NULL) && (ptr → rightchild = = NULL)) value = 1;
Says that if there is only one node the value is 1 which cannot be height of the tree because the tree with one node has height ‘0’.
A
the number of nodes in the tree
B
the number of internal nodes in the tree
C
the number of leaf nodes in the tree
D
the height of the tree
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!