Skip to main content
Format code in narrative with inline code spans, and use code fences to use preformatted style for table rows
Source Link

This solution will work only on the dataset that you've mentioned. If you apply this code to another dataset it will throw up erroneous results. Here is why:

  1. Condition b_node IS NULL and b_Parent is NULLb_node IS NULL and b_Parent is NULL will always give correct result i.e. 'Root'
  2. The conditions for 'Leaf' and 'Inner' will change depending on the dataset. let's assume 5 is not the parent root, instead it is an inner node and there is a parent node 6 above it and 6 also has a child node 7

So the table becomes

  1. a_node a_parent b_node b_parent

  2. 1 2 2 5

  3. 2 5 5 6

  4. 3 5 5 6

  5. 4 3 3 5

  6. 5 6 6 NULL

  7. 6 NULL NULL NULL

  8. 7 6 6 NULL

 1. a_node  a_parent b_node b_parent




 1. 1        2       2       5
 2. 2        5       5       6
 3. 3        5       5       6
 4. 4        3       3       5
 5. 5        6       6       NULL
 6. 6        NULL    NULL    NULL
 7. 7        6       6       NULL

here 2 and 3 are not nulls and will be marked as 'leaf' erroneously. A

A better solution is this case is to use CASECASE to identify those nodes in a_nodea_node which can never be a parent in a_parenta_parent since they are leaf nodes.

This solution will work only on the dataset that you've mentioned. If you apply this code to another dataset it will throw up erroneous results. Here is why:

  1. Condition b_node IS NULL and b_Parent is NULL will always give correct result i.e. 'Root'
  2. The conditions for 'Leaf' and 'Inner' will change depending on the dataset. let's assume 5 is not the parent root, instead it is an inner node and there is a parent node 6 above it and 6 also has a child node 7

So the table becomes

  1. a_node a_parent b_node b_parent

  2. 1 2 2 5

  3. 2 5 5 6

  4. 3 5 5 6

  5. 4 3 3 5

  6. 5 6 6 NULL

  7. 6 NULL NULL NULL

  8. 7 6 6 NULL

here 2 and 3 are not nulls and will be marked as 'leaf' erroneously. A better solution is this case is to use CASE to identify those nodes in a_node which can never be a parent in a_parent since they are leaf nodes.

This solution will work only on the dataset that you've mentioned. If you apply this code to another dataset it will throw up erroneous results. Here is why:

  1. Condition b_node IS NULL and b_Parent is NULL will always give correct result i.e. 'Root'
  2. The conditions for 'Leaf' and 'Inner' will change depending on the dataset. let's assume 5 is not the parent root, instead it is an inner node and there is a parent node 6 above it and 6 also has a child node 7

So the table becomes

 1. a_node  a_parent b_node b_parent




 1. 1        2       2       5
 2. 2        5       5       6
 3. 3        5       5       6
 4. 4        3       3       5
 5. 5        6       6       NULL
 6. 6        NULL    NULL    NULL
 7. 7        6       6       NULL

here 2 and 3 are not nulls and will be marked as 'leaf' erroneously.

A better solution is this case is to use CASE to identify those nodes in a_node which can never be a parent in a_parent since they are leaf nodes.

Source Link

This solution will work only on the dataset that you've mentioned. If you apply this code to another dataset it will throw up erroneous results. Here is why:

  1. Condition b_node IS NULL and b_Parent is NULL will always give correct result i.e. 'Root'
  2. The conditions for 'Leaf' and 'Inner' will change depending on the dataset. let's assume 5 is not the parent root, instead it is an inner node and there is a parent node 6 above it and 6 also has a child node 7

So the table becomes

  1. a_node a_parent b_node b_parent

  2. 1 2 2 5

  3. 2 5 5 6

  4. 3 5 5 6

  5. 4 3 3 5

  6. 5 6 6 NULL

  7. 6 NULL NULL NULL

  8. 7 6 6 NULL

here 2 and 3 are not nulls and will be marked as 'leaf' erroneously. A better solution is this case is to use CASE to identify those nodes in a_node which can never be a parent in a_parent since they are leaf nodes.