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 5974319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:55:35+00:00 2026-05-22T20:55:35+00:00

Hey Folks, I have a tree and using proper fields and variables, I want

  • 0

Hey Folks,
I have a tree and using proper fields and variables, I want to calculate total traveling time of a tree.

Here is my code:

private double time = 0;

..............
..............
..............

public static double time(TreeNode<City> tree{
    City A = tree.getCity();
    if (tree.hasLeftChild()){
        time += tree.getLeftChild().getCity().distance(A);
        time(tree.getLeftChild());
    }
    if (tree.hasNextSibling()){
        time += tree.getNextSibling().getCity().distance(A);
        time(tree.getNextSibling());
    }
    return time;

}

I want to calculate the total time of travelling a tree. Yet, there is one big issue: the field named time has to be static so that I can use it in my static methods. However, when it is static, the program always return 0. How should I make a change to calculate total traveling time of a tree???

Cheers.

  • 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-22T20:55:36+00:00Added an answer on May 22, 2026 at 8:55 pm

    Without going into the details of variable attributes, I will simply note that you wrote a recursive function which is returning a variable. I suggest you actually use that return and do whatever is necessary to assign the value at the end. Something like

    public static double time(TreeNode<City> tree{
        double thistime = 0.0;
        City A = tree.getCity();
        if (tree.hasLeftChild()){
            thistime += tree.getLeftChild().getCity().distance(A);
            thistime += time(tree.getLeftChild());
        }
        if (tree.hasNextSibling()){
            thistime += tree.getNextSibling().getCity().distance(A);
            thistime += time(tree.getNextSibling());
        }
        return thistime;
    }
    

    Then when you call it;

    time = time(root);
    printf("Time is %f, actual time should be %f\n",time,time(root));
    

    The printf at the end is just to help figure out if there is some kind of attribute assignment problem vs bug in the function vs something else.

    Code should try to avoid updating variables outside the scope of the code when possible. Makes for better safety in case of conversion to threading, signal handling, debugging, and generally prevents spooky action at a distance.

    I’ll note in your sample code that you are apparently missing a close-parent in the function definition.

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

Sidebar

Related Questions

Hey Folks,I have pasted my code here. Dialog.inform contains a 'long' value which is
Hey folks, I have the following piece of code from C++. for (int i=0;
Hey folks, I have to apologize for my inexperience here, I've been working on
Hey folks, I have a field within a table that I need to do
Hey folks, every once in a while I have the need to automate data
Hey folks, I'm pretty new to flash, so maybe I'm missing something. I'm using
Hey folks, ok so this is my problem, I need to display HTML code
Hey folks. I am using a little bit of jQuery to do a live
Hey folks, I have a question which feels stupid but I can't quite say
Hey folks, diving into jQuery and loving it. I have a set of links

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.