Binary Tree Nodes

  • + 0 comments
    SELECT
        n,
        CASE
            WHEN p IS NULL THEN 'Root'
            WHEN n IN (SELECT DISTINCT p FROM bst) THEN 'Inner'
            WHEN n NOT IN (SELECT DISTINCT p FROM bst WHERE p IS NOT NULL) THEN 'Leaf'
        END AS conditionTable
    FROM bst
    ORDER BY n;
    

    -- Why condition for 'Leaf' must have WHERE p IS NOT NULL b/c NOT IN is entangle with NULL. If you're not write WHERE clause, the result are returned with NULL value

    -- On the other hand why condition for 'Inner' no need to have WHERE clause like 'Leaf' b/c IN is not entangle with NULL. It disregard NULL