| | |
how to print null subtree of a null subtree?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
•
•
thanks a lot for everything iamthwee, but I am almost out of mind. I could not make it. I think I should 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*
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
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:
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 : (
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:
c Syntax (Toggle Plain Text)
if(p->left!=NULL) myQ.Qinsert(p->left); if(p->right!=NULL) myQ.Qinsert(p->right);
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
C++ Syntax (Toggle Plain Text)
23 / \ 1 45 \ / \ 2 31 52 \ \ 3 234 \ 5
First of all we need to find out how much space to allocate for our array. This is simply
C++ Syntax (Toggle Plain Text)
int array[32]
Now we initialise all those to
-1. Remember we have defined -1 to be null. C++ Syntax (Toggle Plain Text)
for (int i=0; i <32; i++) { array[i] = -1; }
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]=23Now we move on to 1. From our tree 1 is to the left of 23. So
2i+1 = 2*0+1 = 1Therefore
array[1]=1Now we move onto 45. From our tree 45 is on the right of 23. So
2i+2 = 2* 0+2 = 2Therefore
array[2] =45Now move onto 2. From our tree 2 is on the right of 1. So
2i+2 = 2*1+2 =4Therefore
array[4]=2Now move onto 31. From our tree 31 is to the left of 45. So
2i+1=2*2+1=5Therefore
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*
•
•
Join Date: Dec 2006
Posts: 49
Reputation:
Solved Threads: 0
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:
My code is below:
c Syntax (Toggle Plain Text)
void BST::BreadthFirstTraversal(){ BSTNode *p=root; int myArray[32]; for(int i = 0; i<32;i++){ myArray[i]=-1; } int m; if(root!=NULL){ myArray[0] = root->item; while(m<sizeof(myArray)){ if(p->left!=NULL){ myArray[2*m+1] = p->left->item; } if(p->right!=NULL){ myArray[2*m+2] = p->right->item; } } } for(int t = 0; t<32;t++){ cout<<myArray[t]; } }
Last edited by WaltP; Jan 7th, 2007 at 2:18 pm. Reason: Please start using CODE tags. See BBCODEs post in the Announcements
![]() |
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++
add api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib library linkedlist linker linux list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive return simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







Thanks a lot 