### Breadth-first Traversing?

I'm new to binary trees. How would I output the nodes in the trees by levels then from left to right?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include #include #include using namespace std; class Binary_Tree { private: struct tree_node { int data; tree_node* leftChild; tree_node* rightChild; }; tree_node* root; public: Binary_Tree() { root = NULL; } bool is_empty() const {return root == NULL; } void insert(int); }; void Binary_Tree::insert(int a) { tree_node* t = new tree_node; tree_node* parent; t->data = a; t->leftChild = NULL; t->rightChild = NULL; parent = NULL; if(is_empty()) root = t; else { tree_node* current; current = root; while(current) { parent = current; if(t->data > current->data) { current = current->rightChild; }else{ current = current->leftChild; } } if(t->data < parent->data) { parent->leftChild = t; }else{ parent->rightChild = t; } } } void Breadth_First() { } int main() { Binary_Tree trees; int choice; int temp; deque num(15); while(1) { cout << "1. insert" << endl; cout << "2. output" << endl; cout << "3. exit" << endl; cin >> choice; switch(choice) { case 1: for(int i=0; i<15;i++) { cout << "Enter a number" << endl; cin >> temp; num.push_back(temp); trees.insert(temp); } break; case 2: Breadth_First(); break; case 3: return 0; } } }``````
Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` ``````#include #include class Binary_Tree { private: struct node { int data; node* leftChild = nullptr ; node* rightChild = nullptr ; }; node* root = nullptr ; static void recursive_delete( node* n ) { if( n != nullptr ) { recursive_delete( n->leftChild ) ; recursive_delete( n->rightChild ) ; delete(n) ; } } static void insert( node* tree, int value ) // invariant: pointer is not null { // get a reference to the appropriate child substree // note: a reference is required because it should be modified if it is null node*& where = value < tree->data ? tree->leftChild : tree->rightChild ; if( where == nullptr ) where = new node { value } ; else insert( where, value ) ; } static void push_children( std::queue< const node* >& q, const node* n ) { // our breadth first is from left to right; so push the left child first if( n->leftChild ) q.push( n->leftChild ) ; if( n->rightChild ) q.push( n->rightChild ) ; } // non-copyable, non-moveable (move elided for brevity) Binary_Tree( const Binary_Tree& ) = delete ; void operator= ( const Binary_Tree& ) = delete ; public: Binary_Tree() = default ; ~Binary_Tree() { recursive_delete(root) ; } bool is_empty() const { return root == nullptr; } void insert( int value ) { if( is_empty() ) root = new node { value } ; else insert( root, value ) ; } std::ostream& print_breadth_first( std::ostream& stm = std::cout ) const { if( is_empty() ) return stm ; // we need to print the items level by level, top to bottom // so use a queue to hold the items at the next lower level (they go to the back of the queue) // these would be printed only after we are done printing the current level std::queue< const node* > queue ; queue.push(root) ; // start with the root while( !queue.empty() ) // as long as there are no more items to be printed { stm << queue.front()->data << ' ' ; // print the item at the current level push_children( queue, queue.front() ) ; // hold the children for later processing queue.pop() ; // and remove the item that was printed } return stm << '\n' ; } }; int main() { Binary_Tree my_tree ; for( int value : { 12, 78, 23, 32, 44, 11, 92, 64, 81, 10, 19, 17 } ) { my_tree.insert(value) ; my_tree.print_breadth_first() ; } }``````

http://coliru.stacked-crooked.com/a/8a2ea1b7b9bf2e01
How would I change this so that it accepts user input instead of using numbers that are already input into the program?
 ``12345`` ``````for( int value : { 12, 78, 23, 32, 44, 11, 92, 64, 81, 10, 19, 17 } ) { my_tree.insert(value) ; my_tree.print_breadth_first() ; }``````
Topic archived. No new replies allowed.