Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:31:57+00:00 2026-05-16T00:31:57+00:00

I have written a recursive Tree Function in pascal ( or delphi ) but

  • 0

I have written a recursive Tree Function in pascal ( or delphi ) but i had an ‘Out of Memory’ message when I ran it.
I need to turn the Calculate recursive function in this code to non-recursive function, can you tell me how please :

program testing(input, output);
type
  ptr = ^tr;
  tr = record
    age: byte;
    left, right: ptr;
  end; 

var
  topper: ptr;
  total, day: longint;

  procedure mycreate(var t: ptr);
  var 
    temp:ptr;

  begin
    new(temp);
    temp^.age := 1;
    temp^.left := nil;
    temp^.right := nil;
    t := temp;

  end;

  procedure gooneday(var t: ptr);
  begin
    if t^.age <> 5 then
    begin
      if t^.age = 2 then
        mycreate(t^.left)
      else if t^.age = 3 then
        mycreate(t^.right);
      
      t^.age := t^.age + 1;
      total := total + 1;
    end;

  end;

  procedure calculate(var crnt: ptr);
  begin
    if crnt <> nil then
    begin
      gooneday(crnt);
      calculate(crnt^.left);
      calculate(crnt^.right);
    end;

  end;

begin
  total := 0;
  mycreate(topper);
  day := 0;

  while total < 1000000000000 do
  begin
    total := 0;
    day := day + 1;
    calculate(topper);
  end;

  writeln(day);
  writeln(total);

end.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:31:57+00:00Added an answer on May 16, 2026 at 12:31 am

    Recursive functions use a stack to keep the state of the recursion.

    When converting to a loop, you must actually create an explicit stack. You must push and pop elements off the stack within the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a recursive function and depending on the output I need to
I have written a recursive function in PHP to crop text. The cropped text
I have written a simple depth-first search in Scala with a recursive function like
I have written a recursive function. For some reason it just won't call itself
i seem to have written a recursive closure :) difficult enough but i am
I would like to write a recursive function in Python. I have written one
I'm writing a sine function that has to be recursive. I have written a
How do recursive ascent parsers work? I have written a recursive descent parser myself
I have a simple recursive method written in both Java and C# Java public
I have written a function that sorts a big scale of data. To test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.