| | |
how to print null subtree of a null subtree?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
Yes iamthwee it would be super
!!! Could you please explain your method on my example:
My tree is below:

And the output have to be like this:
23
1,45
-,2 | 31,52
-,- | -,3 | -,- | -,234
-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-
So sory
but I could not understand how can I print "-" for example in the last line "-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-" properly for all null subtrees by using your method.
Thanks a lot for your concern
!!! Could you please explain your method on my example:My tree is below:
And the output have to be like this:
23
1,45
-,2 | 31,52
-,- | -,3 | -,- | -,234
-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-
So sory
but I could not understand how can I print "-" for example in the last line "-,- | -,- | -,- | -,5 | -,- | -,- | -,- | -,-" properly for all null subtrees by using your method.Thanks a lot for your concern
Another example.
Let us consider the node
. We know
is on the left hand side of node
.
So by our rule
will occupy the array of index
where
will obviously be
since that where
was as an index position in our array.
Thus
occupies position
. Which it does.
C++ Syntax (Toggle Plain Text)
A / B / \ E C \ / \ F G D
{A, B, _, E, C, _, _, _, F, G, D}
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9 10 <-index positionsLet us consider the node
So by our rule
Thus
Last edited by iamthwee; Jan 7th, 2007 at 8:57 am.
*Voted best profile in the world*
Well once you have populated your array you could use the following code to print it.
In my array the number
represents null.
My output
I've just tried it with your example and it works!
In my array the number
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int array[]={23,1,45,-1,2,31,52,-1,-1,-1,3,-1,-1,-1,234,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,-1,-1,-1,-1}; for ( int i = 0; i < 31; i++ ) //31 = 2^5 -1 (array max size) { if (i > 3) { if (i % 2 !=0) { cout<<"|"; } } if ( i > 1 ) { if (i % 2 ==0) { cout << ","; } } //i.e 2^i (you could make this part less hard coded if(i==1-1 || i ==2-1 || i==4-1 || i==8-1 || i==16-1 || i==32-1) { cout << "\n"; } if (array[i] == -1 ) { cout << "-"; } else { cout << array[i]; } } cin.get(); }
My output
C++ Syntax (Toggle Plain Text)
23 1,45 -,2|31,52| -,-|-,3|-,-|-,234| -,-|-,-|-,-|-,5|-,-|-,-|-,-|-,-
I've just tried it with your example and it works!
Last edited by iamthwee; Jan 7th, 2007 at 10:03 am.
*Voted best profile in the world*
A huge part of programming is researching things you don't fully understand. Not knowing anything isn't an excuse not to try, especially with the beautiful resource of the internet at your fingertips. 
If it makes the concept easier, just think of a matrix as a 2D array. Now you know a lot about matrices, and if you don't, you're probably not ready for binary trees.

If it makes the concept easier, just think of a matrix as a 2D array. Now you know a lot about matrices, and if you don't, you're probably not ready for binary trees.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
hi iamthwee, my function to print is below:
this will produce this output:
23 1 45 2 31 52 3 234 5
when I insert the code below to , it exits the program without any output or error.
How can I insert -1 to form an array like yours..
c Syntax (Toggle Plain Text)
void BST::BreadthFirstTraversal(){ Queue myQ; BSTNode *p; int myDepth = 0; cout<<endl; if(root!=NULL){ myQ.Qinsert(root); while(!myQ.QEmpty()){ p = myQ.QDelete(); cout<<p->item<<" "; if(p->left!=NULL) myQ.Qinsert(p->left); if(p->right!=NULL) myQ.Qinsert(p->right); } } }
23 1 45 2 31 52 3 234 5
when I insert the code below to , it exits the program without any output or error.
c Syntax (Toggle Plain Text)
if(p->left==NULL){ int y = -1; BSTNode * temp = new BSTNode(y); myQ.Qinsert(temp); } if(p->right==NULL){ int y = -1; BSTNode * temp = new BSTNode(y); myQ.Qinsert(temp); }
Last edited by WaltP; Jan 7th, 2007 at 2:17 pm. Reason: Please start using CODE tags. See BBCODEs post in the Announcements
•
•
•
•
How can I insert -1 to form an array like yours..
It's good that you have your level order traversal algo working though because that will be useful.
C++ Syntax (Toggle Plain Text)
23 1 45 2 31 52 3 234 5
So you need to first add
Now establish what the parent node of
Carry on untill you have populated your array.
You might need a little time to think about this. Keep going you've almost got it, just keep reading over my examples.
Last edited by iamthwee; Jan 7th, 2007 at 10:47 am.
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: Wow good book.
- Next Thread: graphics in c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






(