Program to generate recursion tree for generic recursive programWhat is tail recursion?How to generate a random alpha-numeric string?How do I generate random integers within a specific range in Java?Cycles in family tree softwareC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionRecursive generators in PHPReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsRecursion tree with Fibonacci -Python-Does the C++ standard allow for an uninitialized bool to crash a program?

250 Floor Tower

Closed-form expression for certain product

Why electric field inside a cavity of a non-conducting sphere not zero?

Is there any references on the tensor product of presentable (1-)categories?

Freedom of speech and where it applies

Yosemite Fire Rings - What to Expect?

What does chmod -u do?

What should you do if you miss a job interview (deliberately)?

Why is it that I can sometimes guess the next note?

Where did Edmond Malone place the Tempest in the chronology of Shakespeare's plays?

Did arcade monitors have same pixel aspect ratio as TV sets?

Is the U.S. Code copyrighted by the Government?

What percentage of fillings performed today are done with mercury amalgam?

Lowest total scrabble score

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

On a tidally locked planet, would time be quantized?

Is it better practice to read straight from sheet music rather than memorize it?

Is it safe to use olive oil to clean the ear wax?

When were female captains banned from Starfleet?

How do I color the graph in datavisualization?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

Added a new user on Ubuntu, set password not working?

Symbol used to indicate indivisibility

Can Legal Documents Be Siged In Non-Standard Pen Colors?



Program to generate recursion tree for generic recursive program


What is tail recursion?How to generate a random alpha-numeric string?How do I generate random integers within a specific range in Java?Cycles in family tree softwareC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionRecursive generators in PHPReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsRecursion tree with Fibonacci -Python-Does the C++ standard allow for an uninitialized bool to crash a program?













0















Often when solving a recursive or dynamic programming problem, I find myself drawing a recursion tree to help simplify the question for me. However, for some questions which are complicated I have access to the solution but no idea how to draw the tree.



What I have tried so far is printing out the calling function and it's parameters, and this has proved helpful in some examples. However, I saw this tree for fibonacci(5) here generated by mathematica in this answer: https://mathematica.stackexchange.com/questions/116344/how-do-i-create-a-recursive-tree-plot-for-the-fibonacci-sequence



enter image description here



I was wondering if I could generate the same kind of tree in a mainstream high level language like Python, Java, or C++? The tree could just have the nodes as the function name and parameters like in the image.










share|improve this question
























  • Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

    – Petr
    Mar 7 at 7:45











  • @Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

    – user3586940
    Mar 7 at 7:59
















0















Often when solving a recursive or dynamic programming problem, I find myself drawing a recursion tree to help simplify the question for me. However, for some questions which are complicated I have access to the solution but no idea how to draw the tree.



What I have tried so far is printing out the calling function and it's parameters, and this has proved helpful in some examples. However, I saw this tree for fibonacci(5) here generated by mathematica in this answer: https://mathematica.stackexchange.com/questions/116344/how-do-i-create-a-recursive-tree-plot-for-the-fibonacci-sequence



enter image description here



I was wondering if I could generate the same kind of tree in a mainstream high level language like Python, Java, or C++? The tree could just have the nodes as the function name and parameters like in the image.










share|improve this question
























  • Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

    – Petr
    Mar 7 at 7:45











  • @Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

    – user3586940
    Mar 7 at 7:59














0












0








0








Often when solving a recursive or dynamic programming problem, I find myself drawing a recursion tree to help simplify the question for me. However, for some questions which are complicated I have access to the solution but no idea how to draw the tree.



What I have tried so far is printing out the calling function and it's parameters, and this has proved helpful in some examples. However, I saw this tree for fibonacci(5) here generated by mathematica in this answer: https://mathematica.stackexchange.com/questions/116344/how-do-i-create-a-recursive-tree-plot-for-the-fibonacci-sequence



enter image description here



I was wondering if I could generate the same kind of tree in a mainstream high level language like Python, Java, or C++? The tree could just have the nodes as the function name and parameters like in the image.










share|improve this question
















Often when solving a recursive or dynamic programming problem, I find myself drawing a recursion tree to help simplify the question for me. However, for some questions which are complicated I have access to the solution but no idea how to draw the tree.



What I have tried so far is printing out the calling function and it's parameters, and this has proved helpful in some examples. However, I saw this tree for fibonacci(5) here generated by mathematica in this answer: https://mathematica.stackexchange.com/questions/116344/how-do-i-create-a-recursive-tree-plot-for-the-fibonacci-sequence



enter image description here



I was wondering if I could generate the same kind of tree in a mainstream high level language like Python, Java, or C++? The tree could just have the nodes as the function name and parameters like in the image.







java c++ recursion visualization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 8:55









c0der

9,26651946




9,26651946










asked Mar 7 at 7:29









user3586940user3586940

1339




1339












  • Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

    – Petr
    Mar 7 at 7:45











  • @Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

    – user3586940
    Mar 7 at 7:59


















  • Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

    – Petr
    Mar 7 at 7:45











  • @Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

    – user3586940
    Mar 7 at 7:59

















Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

– Petr
Mar 7 at 7:45





Are you asking how to write a program that generates a tree, or are you asking for an existing such a program?

– Petr
Mar 7 at 7:45













@Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

– user3586940
Mar 7 at 7:59






@Petr If somebody had a program that generates a recursion tree that would be great, I could read the source and figure it out from there. If not, pointers on where to start would be helpful.

– user3586940
Mar 7 at 7:59













1 Answer
1






active

oldest

votes


















1














Have a look at graphviz and the examples of usage.



Here is an example:



long long fib(int n)

if (n <= 1) return 1;
std::cout << "fib" << n << " -> fib" << n-2 << 'n';
std::cout << "fib" << n << " -> fib" << n-1 << 'n';
return fib(n-2) + fib(n-1);


int main()

std::cout << "digraph n";
fib(5);
std::cout << "n";



Running



program > t.dot 
dot -Tpng t.dot


produced:



enter image description here



This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).



To get a tree one has to maintain unique IDs for each call. Here is an example for that:



static unsigned id = 0;
long long fib(int n)

auto call_id = id++;
std::cout << "fib" << call_id << " [label="fib(" << n << ")"]n";
if (n <= 1) return 1;
std::cout << "fib" << call_id << " -> fib" << id << 'n';
auto fib_n_minus_2 = fib(n-2);
std::cout << "fib" << call_id << " -> fib" << id << 'n';
auto fib_n_minus_1 = fib(n-1);
return fib_n_minus_2 + fib_n_minus_1;


int main()

std::cout << "digraph n";
fib(5);
std::cout << "n";



And the graph is:
enter image description here






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038268%2fprogram-to-generate-recursion-tree-for-generic-recursive-program%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Have a look at graphviz and the examples of usage.



    Here is an example:



    long long fib(int n)

    if (n <= 1) return 1;
    std::cout << "fib" << n << " -> fib" << n-2 << 'n';
    std::cout << "fib" << n << " -> fib" << n-1 << 'n';
    return fib(n-2) + fib(n-1);


    int main()

    std::cout << "digraph n";
    fib(5);
    std::cout << "n";



    Running



    program > t.dot 
    dot -Tpng t.dot


    produced:



    enter image description here



    This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).



    To get a tree one has to maintain unique IDs for each call. Here is an example for that:



    static unsigned id = 0;
    long long fib(int n)

    auto call_id = id++;
    std::cout << "fib" << call_id << " [label="fib(" << n << ")"]n";
    if (n <= 1) return 1;
    std::cout << "fib" << call_id << " -> fib" << id << 'n';
    auto fib_n_minus_2 = fib(n-2);
    std::cout << "fib" << call_id << " -> fib" << id << 'n';
    auto fib_n_minus_1 = fib(n-1);
    return fib_n_minus_2 + fib_n_minus_1;


    int main()

    std::cout << "digraph n";
    fib(5);
    std::cout << "n";



    And the graph is:
    enter image description here






    share|improve this answer



























      1














      Have a look at graphviz and the examples of usage.



      Here is an example:



      long long fib(int n)

      if (n <= 1) return 1;
      std::cout << "fib" << n << " -> fib" << n-2 << 'n';
      std::cout << "fib" << n << " -> fib" << n-1 << 'n';
      return fib(n-2) + fib(n-1);


      int main()

      std::cout << "digraph n";
      fib(5);
      std::cout << "n";



      Running



      program > t.dot 
      dot -Tpng t.dot


      produced:



      enter image description here



      This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).



      To get a tree one has to maintain unique IDs for each call. Here is an example for that:



      static unsigned id = 0;
      long long fib(int n)

      auto call_id = id++;
      std::cout << "fib" << call_id << " [label="fib(" << n << ")"]n";
      if (n <= 1) return 1;
      std::cout << "fib" << call_id << " -> fib" << id << 'n';
      auto fib_n_minus_2 = fib(n-2);
      std::cout << "fib" << call_id << " -> fib" << id << 'n';
      auto fib_n_minus_1 = fib(n-1);
      return fib_n_minus_2 + fib_n_minus_1;


      int main()

      std::cout << "digraph n";
      fib(5);
      std::cout << "n";



      And the graph is:
      enter image description here






      share|improve this answer

























        1












        1








        1







        Have a look at graphviz and the examples of usage.



        Here is an example:



        long long fib(int n)

        if (n <= 1) return 1;
        std::cout << "fib" << n << " -> fib" << n-2 << 'n';
        std::cout << "fib" << n << " -> fib" << n-1 << 'n';
        return fib(n-2) + fib(n-1);


        int main()

        std::cout << "digraph n";
        fib(5);
        std::cout << "n";



        Running



        program > t.dot 
        dot -Tpng t.dot


        produced:



        enter image description here



        This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).



        To get a tree one has to maintain unique IDs for each call. Here is an example for that:



        static unsigned id = 0;
        long long fib(int n)

        auto call_id = id++;
        std::cout << "fib" << call_id << " [label="fib(" << n << ")"]n";
        if (n <= 1) return 1;
        std::cout << "fib" << call_id << " -> fib" << id << 'n';
        auto fib_n_minus_2 = fib(n-2);
        std::cout << "fib" << call_id << " -> fib" << id << 'n';
        auto fib_n_minus_1 = fib(n-1);
        return fib_n_minus_2 + fib_n_minus_1;


        int main()

        std::cout << "digraph n";
        fib(5);
        std::cout << "n";



        And the graph is:
        enter image description here






        share|improve this answer













        Have a look at graphviz and the examples of usage.



        Here is an example:



        long long fib(int n)

        if (n <= 1) return 1;
        std::cout << "fib" << n << " -> fib" << n-2 << 'n';
        std::cout << "fib" << n << " -> fib" << n-1 << 'n';
        return fib(n-2) + fib(n-1);


        int main()

        std::cout << "digraph n";
        fib(5);
        std::cout << "n";



        Running



        program > t.dot 
        dot -Tpng t.dot


        produced:



        enter image description here



        This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).



        To get a tree one has to maintain unique IDs for each call. Here is an example for that:



        static unsigned id = 0;
        long long fib(int n)

        auto call_id = id++;
        std::cout << "fib" << call_id << " [label="fib(" << n << ")"]n";
        if (n <= 1) return 1;
        std::cout << "fib" << call_id << " -> fib" << id << 'n';
        auto fib_n_minus_2 = fib(n-2);
        std::cout << "fib" << call_id << " -> fib" << id << 'n';
        auto fib_n_minus_1 = fib(n-1);
        return fib_n_minus_2 + fib_n_minus_1;


        int main()

        std::cout << "digraph n";
        fib(5);
        std::cout << "n";



        And the graph is:
        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 13:13









        Michael VekslerMichael Veksler

        4,9371624




        4,9371624





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038268%2fprogram-to-generate-recursion-tree-for-generic-recursive-program%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович