Tree Node
You can write a MySQL query to identify the type of each node in the given tree structure. Here’s the query:
|
|
Explanation:
- We use the
CASE
expression to determine the type of each node. - If
p_id
isNULL
, the node is the root of the tree. - If the
id
is not present in thep_id
column (excludingNULL
), the node is a leaf node, meaning it has no children. - If none of the above conditions are met, the node is an inner node, meaning it has both a parent and child nodes.