Binary Tree Nodes

  • + 4 comments

    As we are supposed to display specific text for each node:

    • Root: If node is root node.
    • Leaf: If node is leaf node.
    • Inner: If node is neither root nor leaf node.

    in T-SQL we should use conditional CASE expression. Hence, if the parent (P) of the node (N) is NULL we print 'Root' since it means that the node (N) doesn't have a parent. Else if a node is a parent node to any other nodes it means that such a node cannot be a leaf node, so it must be an inner node. Finally, if the above conditions weren't met it means, that the node (N) must be a leaf node, so we output 'Leaf'. Obviously we order the result according to the column N. Hope this answers your question.