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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:24:34+00:00 2026-06-12T14:24:34+00:00

I would like to display the tree structure level by level. My current code

  • 0

I would like to display the tree structure level by level. My current code does a BFS or Level Order Traversal but I cannot get the output to display the tree structure like a tree
See current output and Expected output.

My idea was to use some kind of count to iterate over elements from the same level in the queue.

How can I do so.

Original Code without this function can be found in the link below in case someone needs the entire implementation else just look at the displayBFS function below.

Level Order traversal of a generic tree(n-ary tree) in java

Thanks!

   void displayBFS(NaryTreeNode n)
   {
      Queue<NaryTreeNode> q  = new LinkedList<NaryTreeNode>();
      System.out.println(n.data);

       while(n!=null)
     {   
         for(NaryTreeNode x:n.nary_list)
         {
             q.add(x);
             System.out.print(x.data + " ");
         }
         n=q.poll();
         System.out.println();
      } 
  }

Current Tree Structure for reference:

         root(100)
        /      |       \
      90       50       70
      /        \
    20 30   200  300


    Current Output:
        100
        90 50 70
        20 30
        200 300

    Expected Output
        100
        90 50 70
        20 30 200 300    

Also, I had posted a logic issue with the same function earlier, as that was answered and the current question realtes to a different problem, I posted a new question, is this approach okay or should I make edits to the earlier question and not open a new one?

  • 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-12T14:24:36+00:00Added an answer on June 12, 2026 at 2:24 pm

    only need to keep track of current level and next level.

    static void displayBFS(NaryTreeNode root) {
        int curlevel = 1;
        int nextlevel = 0;
    
        LinkedList<NaryTreeNode> queue = new LinkedList<NaryTreeNode>();
        queue.add(root);
    
        while(!queue.isEmpty()) { 
            NaryTreeNode node = queue.remove(0);
    
            if (curlevel == 0) {
                System.out.println();
                curlevel = nextlevel;
                nextlevel = 0;
            }
    
            for(NaryTreeNode n : node.nary_list) {
                queue.addLast(n);
                nextlevel++;
            }
    
            curlevel--;
            System.out.print(node.data + " ");
        } 
    }
    

    when you switch levels, swap nextlevel for currentlevel and reset nextlevel. i prefer the simplicity of this over keeping a whole separate queue.

    i had this question for a microsoft interview last week… it didn’t go so well for me over the phone. good on you for studying it.

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

Sidebar

Related Questions

I have a deep structure that I would like to display as a tree
I would like to display other we pages in my mobile webapp, but some
In flex 3 I have a hierarchical data structure. I would like to display
I would like to display a tree without using the tree component, since I
I'm implementing a B-Tree, and would like to display it in a simple UI
I would like to add animation effect to following code when showing tree items.
I would like to display a gallery of photos and how many times each
I would like to display a loading bar before the entire page is loaded.
I would like to display only posts that are members of BOTH category 33,
I would like to display the first and last name of the authenticated user.

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.