{
  "slug": "bst-delete",
  "title": "BST — Delete",
  "category": "Trees",
  "difficulty": "hard",
  "complexity": {
    "time": "O(h)",
    "space": "O(1)"
  },
  "idea": "Deleting from a BST has three cases. A leaf just detaches. A node with one child is replaced by that child. A node with two children is trickier: copy in its in-order successor (the smallest key on its right) and delete that instead.",
  "code": [
    "find the node (and its parent)",
    "no children      → detach it",
    "one child        → the child takes its place",
    "two children     → copy in the in-order successor",
    "                   then delete the successor"
  ],
  "example": {
    "array": [
      50,
      30,
      70,
      20,
      45,
      60,
      85
    ],
    "target": 30
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 7,
  "steps": [
    {
      "i": 0,
      "op": "tree",
      "caption": "Delete 30 from the tree.",
      "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": "30 vs 50 — go left.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": "compare",
          "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": "Found 30. Now remove it.",
      "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": "remove",
          "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": "45 is the in-order successor — the smallest key bigger than 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": null,
          "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": "candidate",
          "badges": [
            "succ"
          ]
        },
        {
          "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": "Copy 45 into the node we're deleting.",
      "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": 45,
          "label": null,
          "text": null,
          "parent": "t0",
          "side": "left",
          "focus": "write",
          "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": [
            "succ"
          ]
        },
        {
          "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": 5,
      "op": "tree",
      "caption": "Now remove the old successor node — it had no left child, so that's easy.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 45,
          "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": "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": 6,
      "op": "tree",
      "caption": "Done — the tree is still a valid BST.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "nodes": [
        {
          "id": "t0",
          "value": 50,
          "label": null,
          "text": null,
          "parent": null,
          "side": null,
          "focus": null,
          "badges": null
        },
        {
          "id": "t1",
          "value": 45,
          "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": "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
        }
      ]
    }
  ]
}