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
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 Correct Answer number of leaf nodes in the tree
Example:
[ alt="F1 R.S M.P 30.07.19 D 6" src="//storage.googleapis.com/tb-img/production/19/07/F1_R.S_M.P_30.07.19_D%206.PNG" style="width: 105px; height: 116px;">
height of the tree = 2
number of internal nodes = 2
number of nodes without a right sibling = 3
number of leaf nodes in the tree = 1
|
Node |
Address |
left child |
right child |
|
A (Root) |
(1000) |
B |
null |
|
B |
(1002) |
C |
null |
|
C |
(1004) |
null |
null |
DoSomething(1000)
value = DoSomething(1000->leftMostChild) = DoSomething(1002);
value = DoSomething(1002->leftMostChild) = DoSomething(1004);
value = DoSomething(1004->leftMostChild) = DoSomething(null) → return 1
value = value + DoSomething(tree->rightSibling) = 1+ DoSomething(null) → return
value = value + DoSomething(tree->rightSibling) = 1+ DoSomething(null) → return
value = value + DoSomething(tree->rightSibling) = 1+ DoSomething(null) → return
Output: 1
The value returned by the function corresponds to the number of leaf nodes in the tree