{
  "slug": "bst-search",
  "title": "BST — Search",
  "category": "Trees",
  "difficulty": "easy",
  "complexity": {
    "time": "O(h)",
    "space": "O(1)"
  },
  "idea": "Searching a BST is binary search on a tree: compare with the node, then go left or right. Each step discards an entire subtree, so a balanced tree finds any key in about log n steps.",
  "code": [
    "cur = root",
    "while cur != null:",
    "    if key == cur: found",
    "    cur = key < cur ? cur.left : cur.right",
    "not found"
  ],
  "example": {
    "array": [
      50,
      30,
      70,
      20,
      45,
      60,
      85
    ],
    "target": 45
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 4,
  "steps": [
    {
      "i": 0,
      "op": "tree",
      "caption": "Search for 45, starting at the root.",
      "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": "45 vs 50 — go left.",
      "note": "the whole right subtree is ruled out",
      "codeLine": 4,
      "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": "45 vs 30 — go right.",
      "note": "the whole left subtree is ruled out",
      "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": "compare",
          "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 equals 45 — found it.",
      "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": "target",
          "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
        }
      ]
    }
  ]
}