| | |
Tree Traversal
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
In order to traverse a tree you could use something like this:
For inorder traversal:
For preorder traversal:
For post order traversal:
For level order traversal:
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....
For inorder traversal:
C++ Syntax (Toggle Plain Text)
inorder(TreeNode* currentNode) { if (currentNode) { inorder(currentNode->LeftChild); cout << currentNode->data; inorder(currentNode->RightChild); } }
For preorder traversal:
C++ Syntax (Toggle Plain Text)
preorder(TreeNode* currentNode) { if (currentNode) { cout << currentNode->data; preorder(currentNode->LeftChild); preorder(currentNode->RightChild); } }
For post order traversal:
C++ Syntax (Toggle Plain Text)
postorder(TreeNode* currentNode) { if (currentNode) { postorder(currentNode->LeftChild); postorder(currentNode->RightChild); cout << currentNode->data; } }
For level order traversal:
C++ Syntax (Toggle Plain Text)
LevelOrder(TreeNode* root) { Queue q<TreeNode*>; TreeNode* currentNode = root; while (currentNode) { cout << currentNode->data; if (currentNode->LeftChild) q.Add(currentNode->LeftChild); if (currentNode->RightChild) q.Add(currentNode->RightChild); currentNode = q.Delete(); //q.Delete returns a node pointer } }
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
- Tree traversal (Computer Science)
- binary tree traversal .. (C++)
- Binary Tree Traversal (C)
- C++ complete binary tree using an array. Unexpected end file (C++)
Other Threads in the C++ Forum
- Previous Thread: Generic Data Types Help!
- Next Thread: Please help me with C++!
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






