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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:23:45+00:00 2026-06-10T00:23:45+00:00

I was looking at the question being asked here, but found only answers regarding

  • 0

I was looking at the question being asked here, but found only answers regarding binary trees.
I want to print in level order a tree that has 0 – n children.
I know the number of children, but my code isn’t working

The algorithm I thought is:

  • print root
  • print all of the children data
  • for each child
    • print the data

but the problem is that I don’t know where to stop, and when I try recursively, I fail.

This is the function that I wrote:

void printBFS(myStruct s)
   {
      int i = 0;
      printAllChildrenData(s);
      for (i = 0; i < s->number_of_children; i++)
        {
            myStruct childCurr = getChildAtIndex(s, i);
            printBFS(chilCurr);
        }
   }

I’m messing here something.
I hope the functions are clear:
the printAllChildrenData prints all the data of all the children of S; it goes over the children list and prints it.

Editing
If I have this tree for example:

           1
     2        7        8
  3    6            9     12
 4 5              10 11

it should print:

1 2 7 8 3 6 4 5 9 12 10 11

instead of:

1 2 7 8 3 6 9 12 4 5 10 11
  • 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-10T00:23:45+00:00Added an answer on June 10, 2026 at 12:23 am

    This code, which is closely based on your code (but expanded into a SSCCE), produces the output:

     1 2 7 8 3 6 4 5 9 12 10 11
    

    The code uses the designated initializer feature of C99 (one of the most useful additions to C99, IMNSHO). I’ve chosen to use a ‘better’ name than myStruct for the structure; it represents a tree, so that’s what it is called. I’ve also not hidden the pointer in the typedef, and made the printing code const-correct (printing code should not normally modify the data structure it is operating on). It also uses the C99 option to declare a variable in the first clause of a for loop. I introduced an extra function, printTree(), which prints the data from the root node, calls your printBFS() to print the body of the tree, and prints a newline to mark the end of the output; the printTree() function is called to print a tree. Note the systematic use of printData() to print the data for a node. If the data was more complex than a single integer, this would allow you to write the printing code once.

    Careful study of the code will show that the printBFS() below is isomorphic with what you show, which in turn suggests that your problem is not in the code you show. That means it is probably in the code you use to build the tree, rather than in the code used to print it. Since you’ve not shown us the tree-building code, it makes it hard for us to predict what the problem is.

    #include <stdio.h>
    #include <assert.h>
    
    enum { MAX_CHILDREN = 3 };
    typedef struct Tree Tree;
    
    struct Tree
    {
        int data;
        int number_of_children;
        Tree *children[MAX_CHILDREN];
    };
    
    static void printData(const Tree *s)
    {
        printf(" %d", s->data);
    }
    
    static void printAllChildrenData(const Tree *s)
    {
        for (int i = 0; i < s->number_of_children; i++)
            printData(s->children[i]);
    }
    
    static const Tree *getChildAtIndex(const Tree *s, int i)
    {
        assert(s != 0 && i >= 0 && i < s->number_of_children);
        return(s->children[i]);
    }
    
    static void printBFS(const Tree *s)
    {
        printAllChildrenData(s);
        for (int i = 0; i < s->number_of_children; i++)
        {
            const Tree *childCurr = getChildAtIndex(s, i);
            printBFS(childCurr);
        }
    }
    
    static void printTree(const Tree *s)
    {
        printData(s);
        printBFS(s);
        putchar('\n');
    }
    
    /*
    **             1
    **       2        7        8
    **    3    6            9     12
    **   4 5              10 11
    */
    
    static Tree nodes[] =
    {
        [ 1] = {  1, 3, { &nodes[ 2], &nodes[ 7], &nodes[ 8] } },
        [ 2] = {  2, 2, { &nodes[ 3], &nodes[ 6], 0          } },
        [ 3] = {  3, 2, { &nodes[ 4], &nodes[ 5], 0          } },
        [ 4] = {  4, 0, { 0,          0,          0          } },
        [ 5] = {  5, 0, { 0,          0,          0          } },
        [ 6] = {  6, 0, { 0,          0,          0          } },
        [ 7] = {  7, 0, { 0,          0,          0          } },
        [ 8] = {  8, 2, { &nodes[ 9], &nodes[12], 0          } },
        [ 9] = {  9, 2, { &nodes[10], &nodes[11], 0          } },
        [10] = { 10, 0, { 0,          0,          0          } },
        [11] = { 11, 0, { 0,          0,          0          } },
        [12] = { 12, 0, { 0,          0,          0          } },
    };
    
    int main(void)
    {
        printTree(&nodes[1]);
        return(0);
    }
    

    You can easily revise the testing to print each node in turn:

    enum { NUM_NODES = sizeof(nodes) / sizeof(nodes[0]) } ;
    
    int main(void)
    {
        for (int i = 1; i < NUM_NODES; i++)
            printTree(&nodes[i]);
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question previously but the answers weren't what I was looking for.
I've asked a related question here: Show unmatched html tags in Notepad++ Having only
Doing a search around I've found this kind of question being asked in the
Forgive this question for being a little subjective but if you're just looking at
I know this is probably a question that is asked a ton on here
I know this question has been asked before and I read all the answers
A similar question was asked here in storing information in a given html element.
Sorry for being to vague last time I asked this question. I have this
I know this question has been asked, but I can't find more than one
i recently asked this question and i got a lot of great answers breaking

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.