{
  "slug": "bst-inorder",
  "title": "In-order Traversal",
  "category": "Trees",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(h)"
  },
  "idea": "In-order traversal visits the whole left subtree, then the node, then the right subtree. Because a BST stores smaller keys on the left, this order is exactly sorted order.",
  "code": [
    "inorder(n):",
    "    if n == null: return",
    "    inorder(n.left)",
    "    visit(n)",
    "    inorder(n.right)"
  ],
  "example": {
    "array": [
      50,
      30,
      70,
      20,
      45,
      60,
      85
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 16,
  "steps": [
    {
      "i": 0,
      "op": "tree",
      "caption": "In-order: explore the left subtree, visit the node, then the right subtree.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ]
    },
    {
      "i": 1,
      "op": "tree",
      "caption": "At 50 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ]
    },
    {
      "i": 2,
      "op": "tree",
      "caption": "At 30 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ]
    },
    {
      "i": 3,
      "op": "tree",
      "caption": "At 20 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ]
    },
    {
      "i": 4,
      "op": "tree",
      "caption": "Left side done — visit 20.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 5,
      "op": "tree",
      "caption": "Left side done — visit 30.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 6,
      "op": "tree",
      "caption": "At 45 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 7,
      "op": "tree",
      "caption": "Left side done — visit 45.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 8,
      "op": "tree",
      "caption": "Left side done — visit 50.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 9,
      "op": "tree",
      "caption": "At 70 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 10,
      "op": "tree",
      "caption": "At 60 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": "read",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 11,
      "op": "tree",
      "caption": "Left side done — visit 60.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50,
        60
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 12,
      "op": "tree",
      "caption": "Left side done — visit 70.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50,
        60,
        70
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 13,
      "op": "tree",
      "caption": "At 85 — its left subtree must come first.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": "read",
          "badges": [
            "cur"
          ]
        }
      ],
      "output": [
        20,
        30,
        45,
        50,
        60,
        70
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 14,
      "op": "tree",
      "caption": "Left side done — visit 85.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": "settled",
          "badges": [
            "cur"
          ]
        }
      ],
      "output": [
        20,
        30,
        45,
        50,
        60,
        70,
        85
      ],
      "outputLabel": "visit order"
    },
    {
      "i": 15,
      "op": "tree",
      "caption": "Every node visited — and the output is in sorted order.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 30,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t3",
          "value": 20,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t4",
          "value": 45,
          "label": null,
          "text": null,
          "parent": "t1",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t2",
          "value": 70,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "right",
          "focus": null,
          "badges": null
        },
        {
          "id": "t5",
          "value": 60,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "left",
          "focus": null,
          "badges": null
        },
        {
          "id": "t6",
          "value": 85,
          "label": null,
          "text": null,
          "parent": "t2",
          "side": "right",
          "focus": null,
          "badges": null
        }
      ],
      "output": [
        20,
        30,
        45,
        50,
        60,
        70,
        85
      ],
      "outputLabel": "visit order"
    }
  ]
}