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,266
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #21
Jan 7th, 2007
Originally Posted by sivaslieko++ View Post
thanks a lot for everything iamthwee, but I am almost out of mind. I could not make it. I think I should give up
Don't give up!

I have a complete working version right here which I have written myself using my method.

So I CAN guarantee you it does work.

I don't just want to post it however, because you're sooo close.

Like I said read over my examples and apply it to your example.

:p
*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
  #22
Jan 7th, 2007
Should I continue to use Queue method or should I completely change it with array method?

While using Queue I stored the pointers to values stored in the tree into the queue and called these values like "p->item" and store each subnode into queue with below code:
  1. if(p->left!=NULL)
  2. myQ.Qinsert(p->left);
  3. if(p->right!=NULL)
  4. myQ.Qinsert(p->right);
But in array method, you store values not pointers into array so I could not call the left subtrees as Queue method? What should I do....

Sory I tire you a bit iamthwee : (
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,266
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #23
Jan 7th, 2007
Use my array example. Yes. You don't need the queue anymore.

Do you want me to explain how to use it with your 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
  #24
Jan 7th, 2007
pleaseee
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #25
Jan 7th, 2007
  1. 23
  2. / \
  3. 1 45
  4. \ / \
  5. 2 31 52
  6. \ \
  7. 3 234
  8. \
  9. 5

First of all we need to find out how much space to allocate for our array. This is simply 2^h - 1 Where h is the tree height or number of levels. In your case it is 2^5-1 = 31.

  1. int array[32]

Now we initialise all those to -1. Remember we have defined -1 to be null.

  1. for (int i=0; i <32; i++)
  2. {
  3. array[i] = -1;
  4. }


Now we use 23 1 45 2 31 52 3 234 5 and start of from left to right inserting each number into our array.

For example 23 is the root so say array[0]=23

Now we move on to 1. From our tree 1 is to the left of 23. So 2i+1 = 2*0+1 = 1

Therefore array[1]=1

Now we move onto 45. From our tree 45 is on the right of 23. So 2i+2 = 2* 0+2 = 2
Therefore array[2] =45

Now move onto 2. From our tree 2 is on the right of 1. So 2i+2 = 2*1+2 =4

Therefore array[4]=2

Now move onto 31. From our tree 31 is to the left of 45. So 2i+1=2*2+1=5

Therefore array[5]=31.

And so on.

Once you have finished filling the entire array. Print it out using my code I have provided before.
Last edited by iamthwee; Jan 7th, 2007 at 12:12 pm.
*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
  #26
Jan 7th, 2007
hi iamthwee at this point I could not achieve tree traversal I think there is something necessary related with recursion. How did you travers the tree? Could you please explain?

My code is below:
  1. void BST::BreadthFirstTraversal(){
  2.  
  3. BSTNode *p=root;
  4. int myArray[32];
  5.  
  6. for(int i = 0; i<32;i++){
  7. myArray[i]=-1;
  8. }
  9.  
  10. int m;
  11.  
  12. if(root!=NULL){
  13.  
  14. myArray[0] = root->item;
  15.  
  16. while(m<sizeof(myArray)){
  17.  
  18. if(p->left!=NULL){
  19. myArray[2*m+1] = p->left->item;
  20. }
  21.  
  22. if(p->right!=NULL){
  23. myArray[2*m+2] = p->right->item;
  24. }
  25.  
  26. }
  27. }
  28.  
  29. for(int t = 0; t<32;t++){
  30. cout<<myArray[t];
  31. }
  32. }
Last edited by WaltP; Jan 7th, 2007 at 2:18 pm. Reason: Please start using CODE tags. See BBCODEs post in the Announcements
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
  #27
Jan 7th, 2007
I make it Thanks a lot
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