Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find leaf nodes in hierarchical tree

Tags:

sql

database

tree

I have a table in my database which stores a tree structure. Here are the relevant fields:

mytree (id, parentid, otherfields...)

I want to find all the leaf nodes (that is, any record whose id is not another record's parentid)

I've tried this:

SELECT * FROM mytree WHERE `id` NOT IN (SELECT DISTINCT `parentid` FROM `mytree`)

But that returned an empty set. Strangely, removing the "NOT" returns the set of all the non-leaf nodes.

Can anyone see where I'm going wrong?

Update: Thanks for the answers folks, they all have been correct and worked for me. I've accepted Daniel's since it also explains why my query didn't work (the NULL thing).

like image 533
nickf Avatar asked Oct 15 '08 00:10

nickf


People also ask

How do you calculate leaf nodes in a tree?

Algorithm to count leaf nodes in a binary treeIf root is a leaf node, return 1. To determine a leaf node check if both left and right children's are NULL. Recursively, calculate the count of leaf nodes in left and right sub tree. Return the sum of leaf node count of left and right sub tree.

How do you check if a node is a leaf node?

The logic to check if a node is a leaf or not is simple, if both left and right children of that node are null then it's a leaf node.

What are the leaf nodes?

Plant leaf nodes are small bumps or swelling where new leaves or stems emerge from a plant. These are the sites where new growth occurs. Knowing how to identify them, will easily enable you to Propagate Your Plants , and also help you with other tricks, such as helping your plant branch.

What is leaf node in SQL Server?

In Sql Server an index is made up of a set of pages (index nodes) that are organized in a B+ tree structure. This structure is hierarchical in nature. The top node is called the ROOT node (Root Page). The bottom level of nodes in the index are called the leaf nodes (leaf Pages).


4 Answers

Your query didn't work because the sub-query includes NULL. The following slight modification works for me:

SELECT * FROM `mytree` WHERE `id` NOT IN (
    SELECT DISTINCT `parentid` FROM `mytree` WHERE `parentid` IS NOT NULL)
like image 116
Daniel Spiewak Avatar answered Sep 28 '22 09:09

Daniel Spiewak


No clue why your query didn't work. Here's the identical thing in left outer join syntax - try it this way?

select a.*
from mytree a left outer join
     mytree b on a.id = b.parentid
where b.parentid is null
like image 29
TheSoftwareJedi Avatar answered Sep 28 '22 09:09

TheSoftwareJedi


SELECT * FROM mytree AS t1
LEFT JOIN mytree AS t2 ON t1.id=t2.parentid
WHERE t2.parentid IS NULL
like image 23
Alexander Kojevnikov Avatar answered Sep 28 '22 08:09

Alexander Kojevnikov


Select * from mytree where id not in (Select distinct parentid from mytree where parentid is not null)

http://archives.postgresql.org/pgsql-sql/2005-10/msg00228.php

like image 22
fatbuddha Avatar answered Sep 28 '22 10:09

fatbuddha