Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Recursion, Need help confused

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Recursion, Need help confused

    I am working on a Red Black tree for a data structures class.

    I have the code:

    void printInorder(Node node)    //Input is initially the root node 
    { 
         if (node == null) 
         { 
               return; 
         } 
         printInorder(node.left); 
         System.out.println(node.data); 
         printInorder(node.right); 
    }

    Let's use this Binary tree as an example:

                 50 
               /    \ 
             40     60 
           /    \       \ 
          20    45      70 
             /   \      / 
              43  47  65

    The output of the code is correct and is : 20 40 43 45 47 50 60 65 70

    My understanding of the code is that it goes from 50 --> 40 --> 20 and then sees that it is pointing to null. It returns to the printInorder(node.left) that passed in the null. It prints out the 20. Then, it checks the right and sees that is also null and returns to the printInorder(node.right). Now that it has returned to the printInorder(node.right), it will continue to the next line of code, but since there is no more code it will cease running the method.

    The output is correct, but from my understanding of the code it should stop after printing "20".

    Can someone explain the process of this recursive loop, step-by-step for me? Thank you.

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Java Recursion, Need help confused

    This is tricky because the location of the print statement is such that it does not reflect where the processing is at any given time. The first call is looking at the "50" node, but the first print statement is "20" because it calls down the left side multiple times first before printing anything. Move the print statement to the beginning of the function if you want to follow where it actually goes in the execution. Hopefully that'll clean up some of the noise and help it make more sense.

Similar Threads

  1. Very Confused Over Java Problem
    By Loremaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 30th, 2017, 06:20 AM
  2. java question confused on:
    By rickster31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2012, 09:06 AM
  3. Newbie: seem confused with Java
    By boyscout in forum Java Theory & Questions
    Replies: 1
    Last Post: April 8th, 2011, 09:12 AM
  4. [SOLVED] Java beginner is confused....
    By truebluecougarman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 27th, 2011, 08:50 AM
  5. Java File IO question :confused:
    By byebyebye in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 17th, 2010, 06:45 AM