In-order Traversal: Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains. In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself. If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be − D B E A F C G → → → → → → Algorithm all nodes are traversed − Step 1 Recursively traverse left subtree. − Step 2 root node. − Step 3 Recursively traverse right subtree. − Pre-order Traversal In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. We from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be − A B D E C F G → → → → → → Algorithm all nodes are traversed −
In-order Traversal: Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains. In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself. If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order. The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be − D → B → E → A → F → C → G Algorithm all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − root node. Step 3 − Recursively traverse right subtree. Pre-order Traversal In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. We from A, and following pre-order traversal, we first visit A itself and then move to its left subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of pre-order traversal of this tree will be − A → B → D → E → C → F → G Algorithm all nodes are traversed −
trees_and_graph.docx