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 5 of 5

Thread: If a method calls a recursive method, does that make the method recursive as well?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default If a method calls a recursive method, does that make the method recursive as well?

    I think the answer is yes, but I'm not 100% sure. For example:

    	public E last() {
    		return root.getRightMostData();
    	}
     
    	  public E getRightMostData() {
    	  	   if (right == null) return data;
    	  	   else return right.getRightMostData();
    	  }

    Does this mean last() follows the recursive model?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: If a method calls a recursive method, does that make the method recursive as well?

    I wouldn't think so. last() does not call itself.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    303
    My Mood
    Bored
    Thanks
    44
    Thanked 84 Times in 70 Posts
    Blog Entries
    4

    Default Re: If a method calls a recursive method, does that make the method recursive as well?

    I like to think of recursion as a property of an algorithm instead of a property of a method. So I would argue that .last() is recursive because functionally that is what it is doing.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,712
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: If a method calls a recursive method, does that make the method recursive as well?

    @ChristopherLowe

    I think your perspective is reasonable and even helpful to forming an answer to a more general question. I think your perspective requires one to define the boundaries of an algorithm. At some point an algorithm is separate from the enclosing program. I would think the boundary of an algorithm is the point at which the algorithm can stand alone: it accepts input according to its documented interface, and from those inputs provides output(s) according to its function. In the OP's example, I would consider last() outside the boundary of the algorithm. last() is not required for the algorithm to function, and last() adds no value. last() is a call to the algorithm which could instead call another method that calls another method that . . . calls another method that calls getRightMostData().

    Further, the OP asked a specific question about recursive methods in the thread title which I believe Norm answered correctly according to the definition of a recursive method. In fact, the Wikipedia article classifies last() as a wrapper function, a term I haven't heard before in this context, but the explanation makes sense.

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    303
    My Mood
    Bored
    Thanks
    44
    Thanked 84 Times in 70 Posts
    Blog Entries
    4

    Default Re: If a method calls a recursive method, does that make the method recursive as well?

    I came across something in my copy of "Ada Plus Data Structures: An Object Oriented Approach" (1996) that deserves a slight necro-post. In the front cover I've written a tonne of exam notes including:

    • Self recursion
    • Mutually recursive
    • Recursive chain


    However this wasn't mentioned in the 30 odd pages on the subject (really good book btw). I dug through my lectures notes and definitely wrote them down while going over Sets and Lists but it wasn't much.

    GregBrannon makes a good counter case and I can't find anything more authoritative that my own 12 year old lecture notes. This simply comes down to my own opinion but I still think that .last() should be treated as recursive. Given OP's code fragment and the spirit of the question I am assuming that nothing is being overloaded and 'root' is a type of this class. .getRightMostData() has a base call, a call on itself and a recursive memory profile and by my previous assumption .last() does as well. The designers of this class would have considered what would happen if 'root.right' points to itself or forms a daisy chain and chosen recursion as the most elegant solution.

    I think Norm's answer is correct in the broadest and most practical sense. I think my answer is correct but from a specific, design orientated perspective. It's the less correct answer because it's rare to consider these kinds of data structure implementations in modern languages like Java.

Similar Threads

  1. How to create this recursive method.
    By exodus2041 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 10:26 AM
  2. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  3. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 04:44 PM
  4. How do I connect 2 method headers to make it recursive???
    By tripline in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 05:27 AM
  5. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 03:05 PM

Tags for this Thread