Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What (x)
If (X=null)
Return -1

Else
L=what(left(x))+1
If (right(x))=null)
Return L

Else return L + what(right(x))

What I have tried:

Everything, following
What (x)
    If (X=null)
       Return -1

Else
      L=what(left(x))+1
      If (right(x))=null)
           Return L

Else return L + what(right(x))
Posted
Updated 15-Jul-19 6:10am
Comments
Dave Kreskowiak 15-Jul-19 11:25am    
What the hell is that? It's not C/C++, sooooo.....
Stefan_Lang 15-Jul-19 11:30am    
"What the hell is that?"
It's weird. Apparently '=' can mean both a comparison and an assignment - good luck for the compiler writers! ;-p

No. I have no idea what language that is written in, but it probably won't compile in it - if nothing else compilers are picky about the number of open brackets matching the number of close brackets, so this:
C++
If (right(x))=null)
Will not compile in any language.
At a guess, it's some kind of pseudocode, and you'll need to implement it in C++ in order to test it properly.

And testing it is part of your task, along with fixing any problems that come up.

Me? I'd start with paper and pencil, drawing a small sample of a tree to check, and manually running through the pseudocode to check all possibilities before I started coding.
 
Share this answer
 
I have no idea what language this is supposed to be, but I'm going to hazard a guess what this code might do:

1. "What" appears to be the name of a function that takes one argument and returns some value

2. Going by the title of this thread, x is probably a binary tree.

3. "If (X=null)" probably refers to the lowercase function parameter x - either this is a typo, or the language is case-agnostic.

4. The indentation is misleading - this is a double nested if/else, the last else should be indented;

5. there are helper functions left(..) and right(..) that probably deliver the left and right branch of x, respectively.

6. There are three possible paths:
a) if x is null, the result is -1
b) if x is not null, and there is no right branch the result is recursively calculated from What(left_branch_of_x), and adding +1. This raises the question: what is the +1 for?
c) if x is not null, and there is a right branch the result is recursively calculated as the sum of What() from both branches. Plus 1.

7. The interesting bit about item 6 is that the case of the left branch being null is not considered: when that happens, What() will be called with a null argument, resulting in a return value of -1. One level up the call stack, this -1 will neutralize the +1 from the summation, and the return value will be the result of What() applied to the right branch.

8. Now let's apply the insights from 6. and 7. backwards, from the bottom of a tree:
a) at the bottom level, both the left and right branch are null, so the result will be What(null)+1 = -1+1 = 0
b) one level up, we have one of three cases: left branch = null, right branch = null, or both branches are not null. Branches that are not null deliver 0 as result, so the possible results from level 1 are What(null)+1+0 = 0 if the left branch is null, 0+1 = 1 if the right branch is null, and 0+1+0 = 1 if both branches are not null.
So far the thesis that the result is the number of left branches holds.
c) at level N, we have, again, three cases:
(1) left branch=null -> result is -1+1+What(right branch) = What(right branch)
(2) left branch is not null and right branch is null -> result is What(left branch)+1
(3) both not null -> result is What(left branch)+1+What(right branch)
In all three cases, if the recursive call to What() delivers the number of left branches, the result at this level is, again, the number of left branches.

Result: yes, given the assumptions from 1. through 5., this function calculates the number of left branches of a binary tree.
 
Share this answer
 
You need to learn about binary tree at first to understand the task correct, than you need to visit some Learn C++ tutorial.

You must traverse the whole key but only sum up the left nodes. And not only the leftest ribs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900