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

  • Home
  • SEARCH
  • 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 9128785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:35:48+00:00 2026-06-17T07:35:48+00:00

Ok I have got this code: public void TraverseTree(TreeNode node) { TraverseTree(node.getLeftChild()); System.out.println(node.getKey()); TraverseTree(node.getRightChild());

  • 0

Ok I have got this code:

public void TraverseTree(TreeNode node) {

            TraverseTree(node.getLeftChild());
            System.out.println(node.getKey());
            TraverseTree(node.getRightChild());         
    }

This actually traverses through the nodes of a tree and print them. I was about to write a test case when it dawned on me that how can you unit test a function with no return type?

After changing the code to make it ‘unit test’ worthy, it looks something like this:

public ArrayList<Object> TraverseTree(TreeNode node, ArrayList<Object> array) {

                if(array == null)
                      array = new ArrayList<Object>();
                traverseTree(node.getLeftChild(), array);
                array.add(node.getKey())
                traverseTree(node.getRightChild(), array); 

return array;       
        }

Now I am thinking, is this the right way to do this? what about the fact that with every recursive call, there is an ArrayList object pushed up the stack? is this a good approach? what if the tree contains thousands of keys?

I really want to know is there a better way to do this? and is it possible to unit test my first code sample (the one which does not return anything)? my test cases are simply 1) is it returning in right order? what happens if the tree is null? etc

  • 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-06-17T07:35:49+00:00Added an answer on June 17, 2026 at 7:35 am

    It’s only a reference (basically a pointer) that’s pushed on the stack.

    An alternative strategy is to pass an abstract Stream object into your method, and have it print to that, rather than hardcoding it to System.out. Then for testing, you can pass in a mock object for capturing the output.

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

Sidebar

Related Questions

i have got this code: class father{ public: virtual void f() { cout<<1;} };
I have got this piece of code: public class ThreadInteraction { public static void
i just have a little design question. If i got this code public Interface
I have got this Runtime Error when running this code: void AlienShipManager::Update(float timeDelta, BulletManager*
I have this code: public static final String PREFS_NAME = MyPrefsFile; public void onCreate(Bundle
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have got this code: $subject=<<<EOD <object height=400 width=500><param name=allowfullscreen value=false> <param name=AllowScriptAccess value=always>
I have got this code for detection of mobile device. <?xml version=1.0 encoding=UTF-8?> <configuration>
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got this code and I am trying to understand the convention followed,

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.