• recategorized by
33,162 views
74 74 votes

Consider the pseudocode given below. The function $DoSomething()$ takes as argument a pointer to the root of an arbitrary tree represented by the $leftMostChild-rightSibling$ representation. Each node of the tree is of type $treeNode$.

typedef struct treeNode* treeptr; 

struct treeNode 
{ 
    treeptr leftMostChild, rightSibling; 
}; 

int DoSomething (treeptr tree) 
{ 
    int value=0; 
    if (tree != NULL) { 
        if (tree->leftMostChild == NULL) 
            value = 1; 
        else 
        value = DoSomething(tree->leftMostChild); 
        value = value + DoSomething(tree->rightSibling); 
    } 
    return(value); 
} 

When the pointer to the root of a tree is passed as the argument to $DoSomething$, the value returned by the function corresponds to the 

  1. number of internal nodes in the tree.
  2. height of the tree.
  3. number of nodes without a right sibling in the tree.
  4. number of leaf nodes in the tree

12 Answers

Best answer
58 58 votes

Here, the condition for count value $= 1$ is

if ($tree \rightarrow  leftMostchild == Null)$

  • so, if there is no left-most child of the tree (or the sub-tree or the current node called in recursion)
  •  Which means there is no child to that particular node (since if there is no left-most child, there is no child at all as per the tree representation given).
  • $\therefore$ the node under consideration is a leaf node.
  • The function recursively counts, and adds to value, whenever a leaf node is encountered.

So, The function returns the number of leaf nodes in the tree. Answer is $D$

• edited by
19 19 votes
The given code is wrong.It is not doing any thing
1 flag:
✌ Low quality (mauryaG “Not much explanation about why code is wrong, if he is claiming that.”)
9 9 votes

Even after getting the fact, that  “value = value + DoSomething(tree->rightSibling);”  this is outside the else, I was getting none as answer.

But the catch is “leftMostChild−rightSibling” this means it is a CBT,  the nodes gets filled from left to right

and hence if there is no left child node, obviously there is no right child node.

We can see the recursion is counting no. of nodes with no left child, so basically it is counting the no of leaves. 

So the answer is: (d) number of leaf nodes in the tree

 

3 3 votes

I want to say a thing that its a C code and in C in if or else block if you give more than 1 statement its only the first statement that is considered to be in that block...some people posted here as if there is 2 statements in thw ekse block but actually there is only 1.

here after else word  2 statements r there..

only first will get executed in else block.That is "value=DoSomething(tree->lestMostChild)"

this line=>> "value=value+DoSomething(tree->RightSibiling)" is not in else block...and its always executed in the very first " if " block..

 

THIS IS HOW YOU SHOULD SEE THE CODE:::::>>>

typedef struct treeNode* treeptr; 

struct treeNode 
{ 
    treeptr leftMostChild, rightSibling; 
}; 

int DoSomething (treeptr tree) 
{ 
    int value=0; 
    if (tree != NULL) { 
        if (tree->leftMostChild == NULL) 
              value = 1; 
        else 
              value = DoSomething(tree->leftMostChild);
        value = value + DoSomething(tree->rightSibling); 
    } 
    return(value); 
} 

 

 

 

so in one line if given pointer is null make value 0 and return value; if its not null but leftmostchild is null that means its a leaf so make value=1 and go for rightsibilings adding if they r leaf too...and if given pointer has a leftmostchild that call that function for the leftmostchild...

 

umtimately whole tree is traversed...only value gets to be 1 if nide is leaf and all such 1's are added...

3 3 votes
all the option are wrong i have code this

#include<stdlib.h>
using namespace std;
struct node
{
    int key;
    struct node *left, *right;
};

struct node *newNode(int item)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d \n", root->key);
        inorder(root->right);
    }
}

struct node* insert(struct node* node, int key)
{
    
    if (node == NULL) return newNode(key);

    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);

    return node;
}
int DoSomething (node* tree)
{
    int value=0;
    if (tree != NULL) {
        if (tree->left == NULL)
            value = 1;
        else
        value = DoSomething(tree->left);
        value = value + DoSomething(tree->right);
    }
    return(value);
}

int main()
{

    struct node *root = NULL;
    root = insert(root, 10);
    insert(root, 5);
    insert(root, 4);
    insert(root, 15);
    insert(root, 13);
    insert(root, 14);
    insert(root, 16);

    int a=DoSomething (root);
    printf("%d",a);
    return 0;
}
it is returing 4 but the leaf are 3 only rest all option before are eliminated
2 2 votes
Based on the given pseudocode, the function `DoSomething` calculates and returns a value based on the structure of the tree. Let's understand how the function works with an example tree.

Suppose we have the following tree structure:

        A
       / \
      B   C
     / \
    D   E
   /
  F
 

In this tree, each node has a `leftMostChild` pointer pointing to its leftmost child, and a `rightSibling` pointer pointing to its right sibling (a node at the same level).

Let's go through the execution of the `DoSomething` function with the given tree.

1. Initially, we pass the root node "A" to the function: `DoSomething(A)`.

2. The function checks if the tree pointer is not `NULL`. Since the root node is not `NULL`, it proceeds to the next steps.

3. The function checks if the leftMostChild pointer of the root node is `NULL`. In this case, it is not `NULL`, so it moves to the else block.

4. It recursively calls the `DoSomething` function with the leftMostChild of the root node: `DoSomething(B)`.

5. For the node "B," the function again checks if the leftMostChild pointer is `NULL`. In this case, it is also not `NULL`, so it moves to the else block.

6. It recursively calls the `DoSomething` function with the leftMostChild of node "B": `DoSomething(D)`.

7. For the node "D," the function checks if the leftMostChild pointer is `NULL`. In this case, it is `NULL`, so it sets `value = 1`.

8. It returns `value` (which is 1) back to the previous call, which was `DoSomething(B)`.

9. Now, the function calculates `value = value + DoSomething(tree->rightSibling)`. Since node "B" has a right sibling "C," it calls `DoSomething(C)`.

10. For node "C," the function checks if the leftMostChild pointer is `NULL`. In this case, it is `NULL`, so it sets `value = 1` for node "C."

11. It returns `value` (which is 1) back to the previous call, which was `DoSomething(A)`.

12. Finally, for node "A," the function calculates `value = value + DoSomething(tree->rightSibling)`. Since node "A" has a right sibling `NULL`, it returns 0 for the right sibling.

        A
       / \
      B   C
     / \
    D   E
   /
  F
 

13. The function returns the final `value`, which is `1 + 0 + 1 = 2`.

So, in this example, when the root node "A" is passed to the `DoSomething` function, the returned value is 2.

Please note that the pseudocode doesn't explicitly mention the purpose or meaning of the returned value, so its interpretation would depend on the specific context or intention of the code.
• edited by
Answer:
Position:
Show:

Related questions

66 66 votes
9 answers 9 answers
26.9k
26.9k views
go_editor asked Sep 28, 2014
26,921 views
Consider the following rooted tree with the vertex labeled $P$ as the root:The order in which the nodes are visited during an in-order traversal of the tree is$\text{SQPT...
9 9 votes
5 answers 5 answers
9.0k
9.0k views
go_editor asked Sep 28, 2014
8,970 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
78 78 votes
5 answers 5 answers
32.6k
32.6k views
go_editor asked Sep 28, 2014
32,647 views
Consider a hash table with $100$ slots. Collisions are resolved using chaining. Assuming simple uniform hashing, what is the probability that the first $3$ slots are unfi...
191 191 votes
11 answers 11 answers
52.6k
52.6k views
go_editor asked Sep 28, 2014
52,570 views
Suppose we have a balanced binary search tree $T$ holding $n$ numbers. We are given two numbers $L$ and $H$ and wish to sum up all the numbers in $T$ that lie between $L$...