Tree Traversal

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Tree Traversal

 
0
  #1
Aug 12th, 2007
In order to traverse a tree you could use something like this:

For inorder traversal:
  1.  
  2. inorder(TreeNode* currentNode)
  3. {
  4. if (currentNode) {
  5. inorder(currentNode->LeftChild);
  6. cout << currentNode->data;
  7. inorder(currentNode->RightChild);
  8. }
  9. }

For preorder traversal:
  1. preorder(TreeNode* currentNode)
  2. {
  3. if (currentNode) {
  4. cout << currentNode->data;
  5. preorder(currentNode->LeftChild);
  6. preorder(currentNode->RightChild);
  7. }
  8. }

For post order traversal:
  1. postorder(TreeNode* currentNode)
  2. {
  3. if (currentNode) {
  4. postorder(currentNode->LeftChild);
  5. postorder(currentNode->RightChild);
  6. cout << currentNode->data;
  7. }
  8. }

For level order traversal:
  1. LevelOrder(TreeNode* root)
  2. {
  3. Queue q<TreeNode*>;
  4. TreeNode* currentNode = root;
  5.  
  6. while (currentNode) {
  7. cout << currentNode->data;
  8. if (currentNode->LeftChild) q.Add(currentNode->LeftChild);
  9. if (currentNode->RightChild) q.Add(currentNode->RightChild);
  10. currentNode = q.Delete(); //q.Delete returns a node pointer
  11. }
  12. }

The question is:

How can i make it a bit more graphical......
i mean that with the above methods the output is a list of numbers... is there anyway to modify this code so that i can have a nicer output?

PS: if you give example for one kind of traversal i will find the others by myself


Thanks again for your help....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: Tree Traversal

 
0
  #2
Aug 12th, 2007
I don't think this is easy, the ones that I have seen that look half ok are the ones that plot the tree on its side.

The ones that work the best use a gdi. Drawing that in the console window is always going to be a pain in the ...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1
Reputation: Calvin'N'Hobbs is an unknown quantity at this point 
Solved Threads: 0
Calvin'N'Hobbs Calvin'N'Hobbs is offline Offline
Newbie Poster

Re: Tree Traversal

 
0
  #3
Dec 4th, 2008
I am currently working on a printout for level order for my data structures class. I will post it up here for you.
Last edited by Calvin'N'Hobbs; Dec 4th, 2008 at 5:27 pm. Reason: Typo
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC