944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11746
  • C++ RSS
Aug 12th, 2007
0

Tree Traversal

Expand Post »
In order to traverse a tree you could use something like this:

For inorder traversal:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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....
Similar Threads
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Aug 12th, 2007
0

Re: Tree Traversal

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 ...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 4th, 2008
0

Re: Tree Traversal

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Calvin'N'Hobbs is offline Offline
1 posts
since Dec 2008
Aug 19th, 2010
1
Re: Tree Traversal
C++ Syntax (Toggle Plain Text)
  1. #
  2. inorder(TreeNode* currentNode)
  3. #
  4. {
  5. #
  6. if (currentNode) {
  7. #
  8. inorder(currentNode->LeftChild);
  9. #
  10. cout << currentNode->data;
  11. #
  12. inorder(currentNode->RightChild);
  13. #}
  14. #
  15. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jbel is offline Offline
2 posts
since Aug 2010
Aug 19th, 2010
0
Re: Tree Traversal
1.
#
2.
inorder(TreeNode* currentNode)
3.
#
4.
{
5.
#
6.
if (currentNode) {
7.
#
8.
inorder(currentNode->LeftChild);
9.
#
10.
cout << currentNode->data;
11.
#
12.
inorder(currentNode->RightChild);
13.
#}
14.
#
15.
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jbel is offline Offline
2 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trying to obtain a number with a decimal
Next Thread in C++ Forum Timeline: nested this?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC