简体   繁体   中英

Is this recursive or not?

I have 2 methods in a class, both methods have the same purpose, but one is recursive, the other is not.

I want to know if the following method called nonRecursivePrintFancyTree() is recursive or not, it does not call itself back.:

PrintFancyTree() {
    String outputString= "";
    outputString += printFancyTree( 2*index, _prefix);
    outputString += printFancyTree( 2*index + 1, _prefix);
}

So I know PrintFancyTree is recursive, it calls itself back, now here is the 'non recursive' one:

nonRecursivePrintFancyTree() {
    String outputString= "";
    outputString += this.printFancyTree( 2*index, _prefix);
    outputString += this.printFancyTree( 2*index + 1, _prefix);
}

Is it non recursive or not? It calls another method from the class.

It's not recursive as it doesn't call itself (or other method that calls the original), the fact that it does call a recursive method doesn't make the original method recursive. This has to do with abstraction, how "printFancyTree" works doesn't matter for "nonRecursivePrintFancyTree", what matters is what "printFancyTree" DOES.

You printFancyTree is defined with no arguments. That is why it is calling other method.

Redefine your method with an argument and call, it will become recursive.

A method that calls itself is called recursive method. The method nonRecursivePrintFancyTree doesn't call itself, therefore it's not a recursive method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM