Write inorder, preorder, and postorder traversing of the tree?
Answer: – Traversing means visiting each node exactly once. A full traversal of a binary tree T produces a linear order of elements exiting in T. there are three basic ways for traversing a binary tree. They are called preorder, inorder and postorder.
Preorder: – In this technique first of all we process the root R of the binary tree T. them we traverse the left sub tree T, of R in preorder, which means that we traverse root of the sub tree T, first and then its left sub tree. After traversing left sub tree of R, than we take over right sub tree of R and process all the nodes in preorder. An example of preorder tree is: –
Inorder: – In the inorder traversing technique first of all we process left sub tree T, of the root R in the inorder. Then process the root R and at the last we process the right sub tree T1 of R. in the given example, A is root and its left sub tree T1 can be illustrated. The root of T1 is B and its left sub tree D is a terminal node. We first process node D then root of D and the last the right sub tree of B. The left sub tree of E is F that is terminal node. We first process F and then right sub tree of E. there is no right sub tree, hence we process E thus all the nodes of left sub tree is processed and resulting list is as follows: –
Postorder: – In this technique first of all, we process the left sub tree T1 of R postorder, then right sub tree T2, in the postorder and at the last root R The left sub tree of R is T1, root of T1 is B and its left sub tree is D that is terminal node. We process D first. Now we consider right sub tree of E we process E and at the last root of the sub tree T1, that is B. The resulting list of the elements after traversing the left sub tree in postorder is as follow: –