Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So I have been around the net trying to grasp the concept of recursion, and just when I thought i had understood it.

I am not quiet able to understand how the piece of code below works. It is used to show all the elements in a binary tree (Infix order).

Can someone please break it down and explain each step.

Thanks in advance.

What I have tried:

C++
void Infix(struct BinaryNode *root){
         if(root){
            Infix((root) -> left);
            printf("%3d", (root) -> data);
            Infix((root) -> right);
        }
    }
Posted
Updated 23-Mar-17 10:10am
v2
Comments
Maciej Los 23-Mar-17 15:32pm    
You have to use debugger.
PIEBALDconsult 23-Mar-17 16:49pm    
I recommend drawing a diagram and following it with your finger.
CPallini 23-Mar-17 16:56pm    
I recommend following Piebald recommandation.

1 solution

Quote:
Can someone please break it down and explain each step.

No, use the debugger to see what the program is doing, you will learn much more and faster.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
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