Deacon's Blog Deacon's Blog
Front Page
  • UBC
  • Algorithm
  • My plan
  • Game Related
  • About this Site
  • About me

Deacon

UBC CSπŸ’» ’27
Front Page
  • UBC
  • Algorithm
  • My plan
  • Game Related
  • About this Site
  • About me
  • UBC

  • Algorithm

    • Trees Related

      • Three types of Traversal
        • Missing Traversals
    • Plan

    • Study
    • Algorithm
    • Trees Related
    Deacon
    2025-02-27
    Content

    Three types of Traversal

    # Three types of Traversal

    ζœ¬εΊ”θ―₯εœ¨θΏ™ι‡Œηš„ε›Ύη‰‡

    # Inorder Traversal

    Let's look at the code first

    // Java program for inorder traversals
    import java.util.*;
    
    // Structure of a Binary Tree Node
    class Node {
        int data;
        Node left, right;
    
        Node(int v)
        {
            data = v;
            left = right = null;
        }
    }
    
    // Main class
    class GFG {
    
        // Function to print inorder traversal
        public static void printInorder(Node node)
        {
            if (node == null)
                return;
    
            // First recur on left subtree
            printInorder(node.left);
    
            // Now deal with the node
            System.out.print(node.data + " ");
    
            // Then recur on right subtree
            printInorder(node.right);
        }
    
    

    The traversal order is left child, then root, and finally right child. You can think of it as following a left-root-right sequence recursively upwards. In the context of a Binary Search Tree, inorder traversal will print the BST node values in ascending order on the console.


    # PostOrder Traversal

    Let's look at the code first

    class GFG {
    
        // Function to print inorder traversal
        public static void printInorder(Node node)
        {
            if (node == null)
                return;
    
            // First recur on left subtree
            printInorder(node.left);  
    
            // Then recur on right subtree
            printInorder(node.right);
          
          // Now deal with the node
            System.out.print(node.data + " ");
        }
    
    

    It’s clear that the left child is processed first, then the right child, and finally the root. This gives us an important hint: ==the final element in a PostOrder Traversal is always the root of the entire tree==. We will use this insight in a later problem to reconstruct a tree given two types of traversals.

    You can approach writing the PostOrder Traversal for a given tree as follows:

    First, find the leftmost node of the entire tree (if there is no left child, look for the leftmost right child). Then traverse in left β†’ right β†’ root order. If you discover a right subtree or right node that hasn’t been traversed yet, you cannot go back up to the higher-level root.

    Essentially, move downward: first look to the left, if there’s no left child then look to the right, and finally return to the root.


    # PreOrder Traversal

    Let's look at the code first:

    class GFG {
    
        // Function to print inorder traversal
        public static void printInorder(Node node)
        {
            if (node == null)
                return;
    
           // First deal with the node
            System.out.print(node.data + " ");
          
            // Then recur on left subtree
            printInorder(node.left);  
    
            // Then recur on right subtree
            printInorder(node.right);
          
        }
    

    Unlike PostOrder Traversal, PreOrder Traversal prints the root node first, then the left subtree, and finally the right subtree. Clearly, the first printed element is the root of the entire tree. Using this concept, if a PreOrder sequence is given, you can quickly identify the root of the entire tree.

    It also starts from the bottom, going first to the left, then to the right, and finally returning to the root node.

    # Some Examples

              (1)
             /   \
           (2)   (3)
          /  \   /  \
        (4)  (5)(6) (7)
       /  \       \
     (8)  (9)     (10)
                  /
                (11)
    

    Please write down the PreOrder / InOrder / PostOrder Traversal of this tree

    # Answer for this question
    Click to See!
    PreOrder: 1 β†’ 2 β†’ 4 β†’ 8 β†’ 9 β†’ 5 β†’ 3 β†’ 6 β†’ 10 β†’ 11 β†’ 7
    PostOrder: 8 β†’ 9 β†’ 4 β†’ 5 β†’ 2 β†’ 11 β†’ 10 β†’ 6 β†’ 7 β†’ 3 β†’ 1
    InOrder: 8 β†’ 4 β†’ 9 β†’ 2 β†’ 5 β†’ 1 β†’ 6 β†’ 11 β†’ 10 β†’ 3 β†’ 7
    
             (A)
           /     \
         (B)     (C)
        /  \       \
      (D)  (E)     (F)
          /       /   \
        (G)     (H)   (I)
                   \
                   (J)
    

    Please write down the PreOrder / InOrder / PostOrder Traversal of this tree

    # The answer for this question
    Click to See!
    PreOrder: A β†’ B β†’ D β†’ E β†’ G β†’ C β†’ F β†’ H β†’ J β†’ I
    PostOrder:  D β†’ G β†’ E β†’ B β†’ J β†’ H β†’ I β†’ F β†’ C β†’ A
    InOrder: D β†’ B β†’ G β†’ E β†’ A β†’ C β†’ H β†’ J β†’ F β†’ I
    
    last updated: 3/11/2025, 4:12:24 AM
    Second Year 2nd term
    Missing Traversals

    ← Second Year 2nd term Missing Traversalsβ†’

    Recently Updated
    01
    Second Year 2nd term
    05-04
    02
    About me
    03-10
    03
    About this site
    03-10
    More articles
    Theme by Vdoing | Copyright Β© 2024-2026 Deacon | All rights reserved.
    • Follow System
    • Light Mode
    • Dark Mode
    • Reading Mode