how to print null subtree of a null subtree?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to print null subtree of a null subtree?

 
0
  #11
Jan 7th, 2007
Incorrect, my example is complete and without error.

Do you want me to go through anther example?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

Re: how to print null subtree of a null subtree?

 
0
  #12
Jan 7th, 2007
Yes iamthwee it would be super !!! Could you please explain your method on my example:
My tree is below:

Name:  tree.JPG
Views: 9
Size:  2.3 KB

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to print null subtree of a null subtree?

 
0
  #13
Jan 7th, 2007
Another example.

  1. A
  2. /
  3. B
  4. / \
  5. E C
  6. \ / \
  7. F G D

{A, B, _, E, C, _, _, _, F, G, D} 
 |  |  |  |  |  |  |  |  |  |  |
 0  1  2  3  4  5  6  7  8  9  10 <-index positions

Let us consider the node E. We know E is on the left hand side of node B.

So by our rule E will occupy the array of index 2i+1 where i will obviously be 1 since that where B was as an index position in our array.

Thus  2*1+1 = 3

E occupies position 3. Which it does.
Last edited by iamthwee; Jan 7th, 2007 at 8:57 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

Re: how to print null subtree of a null subtree?

 
0
  #14
Jan 7th, 2007
Yes I understood in this time thanks but I actually want to know how to print all null nodes as "-" under the right subtree of root A. But in your example just there are 4 subtree showed. Actually I could not understand this point
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to print null subtree of a null subtree?

 
0
  #15
Jan 7th, 2007
Well once you have populated your array you could use the following code to print it.

In my array the number -1 represents null.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. 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};
  8.  
  9.  
  10. for ( int i = 0; i < 31; i++ )
  11. //31 = 2^5 -1 (array max size)
  12. {
  13. if (i > 3)
  14. {
  15. if (i % 2 !=0)
  16. {
  17. cout<<"|";
  18. }
  19. }
  20.  
  21. if ( i > 1 )
  22. {
  23. if (i % 2 ==0)
  24. {
  25. cout << ",";
  26. }
  27. }
  28. //i.e 2^i (you could make this part less hard coded
  29. if(i==1-1 || i ==2-1 || i==4-1 || i==8-1 || i==16-1 || i==32-1)
  30. {
  31. cout << "\n";
  32. }
  33. if (array[i] == -1 )
  34. {
  35. cout << "-";
  36. }
  37. else
  38. {
  39. cout << array[i];
  40. }
  41. }
  42. cin.get();
  43. }

My output
  1.  
  2. 23
  3. 1,45
  4. -,2|31,52|
  5. -,-|-,3|-,-|-,234|
  6. -,-|-,-|-,-|-,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*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: how to print null subtree of a null subtree?

 
0
  #16
Jan 7th, 2007
Originally Posted by sivaslieko++ View Post
Thanks Ravalon but I do not know anything about matrix (
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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

Re: how to print null subtree of a null subtree?

 
0
  #17
Jan 7th, 2007
hi iamthwee, my function to print is below:
  1. void BST::BreadthFirstTraversal(){
  2. Queue myQ;
  3. BSTNode *p;
  4. int myDepth = 0;
  5. cout<<endl;
  6. if(root!=NULL){
  7. myQ.Qinsert(root);
  8. while(!myQ.QEmpty()){
  9.  
  10. p = myQ.QDelete();
  11. cout<<p->item<<" ";
  12.  
  13. if(p->left!=NULL)
  14. myQ.Qinsert(p->left);
  15. if(p->right!=NULL)
  16. myQ.Qinsert(p->right);
  17. }
  18. }
  19. }
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.
  1.  
  2. if(p->left==NULL){
  3. int y = -1;
  4. BSTNode * temp = new BSTNode(y);
  5. myQ.Qinsert(temp);
  6. }
  7. if(p->right==NULL){
  8. int y = -1;
  9. BSTNode * temp = new BSTNode(y);
  10. myQ.Qinsert(temp);
  11. }
How can I insert -1 to form an array like yours..
Last edited by WaltP; Jan 7th, 2007 at 2:17 pm. Reason: Please start using CODE tags. See BBCODEs post in the Announcements
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to print null subtree of a null subtree?

 
1
  #18
Jan 7th, 2007
How can I insert -1 to form an array like yours..
You have to use the method I showed you before to insert your numbers in the array.

It's good that you have your level order traversal algo working though because that will be useful.

  1. 23 1 45 2 31 52 3 234 5

So you need to first add 23 to your array at index position [0]

Now establish what the parent node of 1 is and whether 1 is on the left hand side or right hand side of the parent.

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*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: how to print null subtree of a null subtree?

 
0
  #19
Jan 7th, 2007
Oh you don't actually insert -1 into the array at all.

You initialise your entire array to -1 and then just overwrite each valid number you encounter.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 49
Reputation: sivaslieko++ is an unknown quantity at this point 
Solved Threads: 0
sivaslieko++ sivaslieko++ is offline Offline
Light Poster

Re: how to print null subtree of a null subtree?

 
0
  #20
Jan 7th, 2007
thanks a lot for everything iamthwee, but I am almost out of mind. I could not make it. I think I should give up
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC