473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Level order traversal,Binary Tree

33
would you please help me?
I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree are printed on a separate line.
my codes are below,( for printing inorder, preorder and post order)
I have no Idea how I can print them in , level-order traversal
I think I should use a Queue, but how? do you have any code that can help me?
would you please help me?
thanks.

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. void Tree<T> :: printInOrder (std::ostream& out, TreeNode<T>* rootNode) {
  3.     if (rootNode != NULL) {
  4.         printInOrder (out, rootNode->left);
  5.         out << (rootNode->data) << " ";
  6.         printInOrder (out, rootNode->right);
  7.     }
  8.     return;
  9. }
  10.  
  11. template <typename T>
  12. void Tree<T> :: printPostOrder (std::ostream& out, TreeNode<T>* rootNode) {
  13.     if (rootNode != NULL) {
  14.         printPostOrder (out, rootNode->left);
  15.         printPostOrder (out, rootNode->right);
  16.         out << (rootNode->data) << " ";
  17.     }
  18.     return;
  19. }
  20.  
  21. template <typename T>
  22. void Tree<T> :: printPreOrder (std::ostream& out, TreeNode<T>* rootNode) {
  23.     if (rootNode != NULL) {
  24.         out << (rootNode->data) << " ";
  25.         printPreOrder (out, rootNode->left);
  26.         printPreOrder (out, rootNode->right);
  27.     }
  28.     return;
Feb 27 '08 #1
6 19988
Ganon11
3,652 Expert 2GB
You are right; you need to use a queue. Remember that a queue is a FIFO data structure - what you put in first must come out first. Keeping that in mind, when you traverse a tree, which nodes to you usually encounter first? The ones at the top, or the ones at the bottom?
Feb 27 '08 #2
APEJMAN
33
Thanks
so, I came up with this code, but it dosent work,
would you please help me?
thanks

Expand|Select|Wrap|Line Numbers
  1. //level-order traversal 
  2. void Tree<T>::breadthFirst()
  3. {
  4.     /* Temporary queue. */
  5.     Queue queue;
  6.     Tree<T> *traverse;
  7.  
  8.     /*
  9.     * Gotta put something in the queue initially,
  10.     * so that we enter the body of the loop.
  11.     */
  12.     queue.insert(root);
  13.     while (!queue.isEmpty()) {
  14.         traverse = queue.leave();
  15.  
  16.         //Visit the node pointed to by traverse.
  17.         /*
  18.         * If there is a left child, add it
  19.         * for later processing.
  20.         */
  21.         if (traverse->left!= NULL)
  22.             queue.insert(traverse->left);
  23.         /*
  24.         * If there is a right child, add it
  25.         * for later processing.
  26.         */
  27.         if (traverse->right != NULL)
  28.             queue.insert(traverse->right);
  29.     }
  30.  
  31. }
Feb 28 '08 #3
APEJMAN
33
I actually was able to write the code.
I am wondering how I can use the setw function in the iomanip library to print out appropriately spaced values so that the tree looks more "tree-like" rather than left-adjusted.
Would you please help me?

Expand|Select|Wrap|Line Numbers
  1. //level-order traversal 
  2. template<typename T> 
  3. void Tree<T> :: level_order_traversal(std::ostream& out, TreeNode<T>* rootNode)
  4. {
  5.     /* Temporary queue. */
  6.     Queue <TreeNode<T>*> queue;
  7.     Tree<T> *traverse;
  8.  
  9.     /*
  10.     * Gotta put something in the queue initially,
  11.     * so that we enter the body of the loop.
  12.     */
  13.     int dep; //depth of the node
  14.     int temp=0;
  15.     queue.enter(root);
  16.     while (!queue.isEmpty()) {
  17.         rootNode = queue.leave();
  18.         if( depth(rootNode)!=temp){
  19.         temp=depth(rootNode);
  20.         out<<"\n";
  21.         }
  22.  
  23.         out<<rootNode->data<<" ";
  24.  
  25.         if (rootNode->left!= NULL)
  26.             queue.enter(rootNode->left);
  27.  
  28.         if (rootNode->right != NULL){
  29.             queue.enter(rootNode->right);
  30.         }
  31.     }
  32.  
  33.  
  34. }
Feb 28 '08 #4
Ganon11
3,652 Expert 2GB
Hmm...printing trees nicely is very tough. That's actually something I don't know how to do (that is, I don't have any solution fast at hand).

Maybe you can find out how many levels are in your tree (the height of the tree). Let's say the height is 5. So eventually you may have to print 2^(5-1) = 16 numbers in one line (the bottom line). Assuming 2 digit numbers, you want to use setw(3) to print the numbers. 3 spots * 16 numbers = 48 spaces. So your bottom line will take 2^(height-1) * (spaces_per_number) spaces (a.k.a. let:

max_size = pow(2, height-1) * spaces_per_number; )

Now, your root should be directly in the center of this value (it'll be at the center of the tree). So you move (max_size - spaces_per_number) / 2 spaces, print the root, then print a newline. Each time, you need to halve the amount you skip between numbers.

After this, it should just take a little experimentation to figure out where to print numbers, when to print extra spaces, etc. etc.
Feb 28 '08 #5
APEJMAN
33
Thanks,
but I'm not able to write the code, I have no Idea how to make this work
would you please guid me more? or maybe you have a sample code,
thanks again
Feb 28 '08 #6
What is your depth function?
I think your depth function is not property working.
Feb 29 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
3
by: Amit | last post by:
Greetings All. One of the usages that I need is accessing the contents of the container that I am using in level order( which would mean breadth- first search approach for a binary tree) as...
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
7
by: GrispernMix | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <iostream> #include <string.h> using namespace std; #define TRUE 1 #define FALSE 0
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
0
by: phanipep | last post by:
Construct the binary tree whose pre-order traversal is ABCDEFGH and Inorder traversal is CBDAFEGH, where each letter stands for the information stored in some node of the tree. show all the steps...
4
by: APEJMAN | last post by:
would you please help me with this question? I know that a binary tree can be recovered from its pre-order traversal. That is, a tree built from the pre-order traversal should always be the same as...
8
by: APEJMAN | last post by:
hI Would you please help me with this question? Using Big O Notation, what is the running time of the level-order traversal ( the code below) in terms of the number of nodes N and the tree height...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.