Everything you need to know about tree traversal algorithms. Learn the theories around the algorithms and how to implement them through code
JOBS Vv COMPANIES ARTICLES Vv SALARIES Vv COURSES my ITEMS Pree bagivy public TreeNode(int data) { this.data = data; this.left = this.right = null; 1. INORDER TRAVERSAL Inorder traversal is the one the most used variant of DFS traversal of the tree. As DFS suggests, we will first focus on the depth of the chosen node and then go to the breadth at that level. Therefore, we will start from the root node of the tree and go deeper-and-deeper into the left subtree in a recursive manner. When we reach the left-most node with the above steps, then we will visit that current node and go to the left-most node of its right subtree, if it exists. Same steps should be followed in a recursive manner to complete the inorder traversal. The order of those steps will be similar, in recursive function: 1. Go to the left subtree. 2. Visit node. 3. Go to the right subtree.
id: 05715dea188400d0d6a4a3479217cd8b - page: 7
7/19 4/23/24, 1:45 PM 4 Types of Tree Traversal Algorithms (with Animations) | Built In [rornmover ] JOBS Vv COMPANIES ARTICLES Vv SALARIES Vv COURSES my ITEMS Sy See Vue are VUE eu 9 inorderTraversal(root.right) ; An indoor traversal illustration. | Image: Anand Parmar Inorder traversal of a binary search tree will always give you nodes in a sorted manner. 2. PREORDER TRAVERSAL Preorder traversal is another variant of DFS. The atomic operations in a recursive function are the same as inorder traversal but in a different order. 8/19 4 Types of Tree Traversal Algorithms (with Animations) | Built In
id: 173f2690b76b4e4fb040dd4283da7ba2 - page: 7
[rornmover ] 4/23/24, 1:45 PM JOBS v COMPANIES ARTICLES Vv SALARIES V COURSES my items Order of the steps will be: 1. Visit node. 2. Go to the left subtree. 3. Go to the right subtree. public void preorderTraversal(TreeNode root) { if (root != null) { System.out.print(root.data + " "); preorderTraversal(root.left); preorderTraversal(root.right) ; 9/19 4/23/24, 1:45 PM 4 Types of Tree Traversal Algorithms (with Animations) | Built In [rornmover ] JOBS Vv COMPANIES ARTICLES Vv SALARIES Vv COURSES my ITEMS Preorder traversal illustration. | Image: Anand Parmar 3. POSTORDER TRAVERSAL A similar process goes for the postorder traversal, where we visit the left subtree and the right subtree before visiting the current node in recursion. So, the sequence of the steps will be: 1. Go to the left subtree. 2. Go to the right subtree. 3. Visit the node.
id: 3ef717281817538bb8182beccc470cda - page: 9
10/19 4/23/24, 1:45 PM 4 Types of Tree Traversal Algorithms (with Animations) | Built In [rornmover ] JOBS Vv COMPANIES ARTICLES Vv SALARIES Vv COURSES my ITEMS PVYOSVi Mer rbaversaayruvyeesuereyy postorderTraversal(root.right) ; System.out.print(root.data + " "); Postorder traversal illustration. | Image: Anand Parmar 4. LEVEL ORDER TRAVERSAL This is a different traversal than what we have covered above. Level order traversal follows breadth-first search to visit/modify every node of the tree. As BFS suggests, the breadth of the tree takes priority first and then moves to depth. In simple words, we will visit all the nodes present at the same level one-by-one from left 11/19 4/23/24, 1:45 PM 4 Types of Tree Traversal Algorithms (with Animations) | Built In [rornmover ]
id: aafb8dc74d895f6378af99f27debaecd - page: 10