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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:12:38+00:00 2026-05-31T20:12:38+00:00

I was just trying out some code, not a experienced coder. I implemented(at least

  • 0

I was just trying out some code, not a experienced coder. I implemented(at least think) Level Order traversal and it sorta got done pretty easily. Want to know if its the right approach.

#include<iostream>
#include<queue>
using namespace std;

class node {
public :
  node *right;
  node *left;
  int info;
}*root;

queue<int> inf;

void bfs(node *t)
{
if(t!=NULL)
{
    if(t->right!=NULL)
        inf.push(t->right->info);

    if(t->left!=NULL)
        inf.push(t->left->info);

    bfs(t->right);
    bfs(t->left);
}
}

int main()
{
node *temp = new node();
temp->info = 1;

root = temp;

root->right = new node();
root->right->info = 2;
root->right->right = root->right->left = NULL;

root->left = new node();
root->left->info = 3;
root->left->right = root->left->left = NULL;

node *tmp = root;
root=root->right;

root->right = new node();
root->right->info = 4;
root->right->right = root->right->left = NULL;

root->left = new node();
root->left->info = 5;
root->left->right = root->left->left = NULL;

root = tmp;
root = root->left;

root->right = new node();
root->right->info = 6;
root->right->right = root->right->left = NULL;

root->left = new node();
root->left->info = 7;
root->left->right = root->left->left = NULL;

root = temp;


node *it;
it = root;

inf.push(root->info);


bfs(root);
for(;inf.size()!=0;)
{
    cout<<inf.front()<<" : ";
    inf.pop();
}

return 0;

}

  • 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-31T20:12:39+00:00Added an answer on May 31, 2026 at 8:12 pm

    This uses a std::queue<int> to print the nodes in DFS order! Just using a queue somewhere doesn’t make the traversal order BFS. What you want is a std:: queue<node*> where you put the root node. Then you iterate while the queue isn’t empty and in each iteration you take the next node out of the queue and put its children into the queue:

    for (std::queue<node*> inf(1, root); !inf.empty(); inf.pop()) {
        n = inf.front();
        process(n->info);
        if (n->left) inf.push(n->left);
        if (n->right) inf.push(n->right);
    }
    

    Just a few notes:

    1. Don’t use size() on a container to determine if there are elements: it may e.g. walk through the elements to count them (although none of the standard containers does) and empty() says what you actually want to know ( although it should have been called is_empty()).
    2. Do not use global variables.
    3. Your program seems to leak all node objects.
    4. You should give your node class a constructor to initialize the pointers for the children and the info.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to ping up some experienced Thread gurus out there...trying to learn more
I have just started learning Erlang and am trying out some Project Euler problems
I'm just trying out the allegro library, and here is the code which I've
I'm just trying to work out why the following code is leaking memory and
I am not an experienced Java programmer and i'm trying to write some text
I'm trying to figure out some C code so that I can port it
I'm writing some simple point code while trying out Visual Studio 10 (Beta 2),
I am trying to figure out how to port some .Net code that parsed
I was trying out some dummy application just to test binding modes. So, just
There is the following in some code I'm trying to figure out: For I&

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.