in Education by
I want to understand the following code: struct element{ element *a1; element *a2; int value; }; void main(){ element e6 = { NULL, NULL, 6 }; element e2 = { NULL, NULL, 2 }; element e4 = { NULL, NULL, 4 }; element e7 = { &e6, NULL, 7 }; element e9 = { NULL, NULL, 9 }; element e3 = { &e2, &e4, 3 }; element e8 = { &e7, &e9, 8 }; element e5 = { &e3, &e8, 5 }; cout << CountList(&e5) << endl; return;} int CountList(element *e){ int c=1; if(e){ c=c+CountList(e->a1); c=c+CountList(e->a2); return c;} return 0; } the count is 8. But how can I understand the recursion line ?? my Idea was, that the Count must be 6, because the recursion function are called only 4 times. The Compiler says 8, which is already the correct solution. But why?? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is a tree e5 / \ e3 e8 / \ / \ e2 e4 e7 e9 | e6 The recursive function starts from e5, calls itself with e3 first, which in turn calls with e2, another call with NULL which returns (to e2). From e2 a call on the other side which is NULL also, then returns (to e3). From e3, a call to e4, which has only NULL children and returns (to e3). From there, e3 returns to e5, which calls the other side (e8)... Each non-NULL element counts itself as 1, added to the non-NULL children. Which gives 8 total. The number of elements in the tree. The number of calls to the recursive function is at least 8, to visit all children. If you count the calls with a NULL element (9), that makes a total number of calls of 17.

Related questions

0 votes
    What is recursion in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    What is true about array 1. Can have combination of data types in a single array list 2. Must have same data types for array list...
asked Feb 24, 2021 in Technology by JackTerrance
0 votes
    What is true about array, Can have combination of data types in a single array list. Must have same data types for array list?...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    I have function of "endgame" and here's how the function goes function endgame() { setScreen("scorescreen" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Write a program in python to insert ten friends name in a list, then print 3rd, 5th and 9th value from the list (using data structure) Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Is it possible to add the numbers 1 to n recursively in Java with one return statement? How would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Given the following F# snippet: type A(children: A list) = member val P1 = "" member val P2 = " ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
0 votes
    I am working on spark project using Scala. I need to print each element of a list named 'c' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a list in Python e.g. letters = ["t", "h", "e"] I want to print the array in a single line ... It must be in a single row. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    >>> list(range(0, -9,-1)) will print [-1, -2, -3, -4,-5, -6, -7, -8) [0,-1, -2, -3, -4,-5, -6, -7, -8 ... [0,-1, -2, -3, -4,-5, -6, -7] Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
...