How do I find the entire tree given a node of a tree?
Example of tree:
100
101 102
1010 1011 1020 1021
select level, employee_id, last_name, manager_id ,
connect_by_root employee_id as root_id
from employees
connect by prior employee_id = manager_id
start with employee_id = 101
;
The root in table is (parent,child) example (100,101) there is no (null,100) row in table.
The above query only gives the children starting from 101. But lets say I want everything from the start of the root?
When given '101' as the node, you won't know which is the root.
The query should be usable when the root is the given node.
In general, there are two ways to get the path to a node in a tree. Either we start from the root and move to the child that is an ancestor to the target node. Or, we start from the target node and keep moving up to its parent until we reach the root. Both approaches are perfectly fine.
Techopedia Explains Root Node In this regard, any node can be a root node in relation to itself and its children if that section of the tree is objectively selected.
If binary tree has height h, maximum number of nodes will be when all levels are completely full. Total number of nodes will be 2^0 + 2^1 + …. 2^h = 2^(h+1)-1.
The root node is the highest node in the tree structure, and has no parent. This node is a global element and represents the entire message. It may have one or more child nodes, but can never have sibling nodes or be repeating.
You need to first traverse up the tree to get all managers then traverse down to fetch all employees:
select level, employee_id, last_name, manager_id ,
connect_by_root employee_id as root_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 101
)
;
See http://www.sqlfiddle.com/#!4/d15e7/18
If the given node might also be the root node, extend the query to include the given node in the list of parent nodes:
Example for non-root node:
select distinct employee_id, last_name, manager_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 101
union
select manager_id -- in case we are the root node
from employees
where manager_id = 101
)
;
Example for root node:
select distinct employee_id, last_name, manager_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 100
union
select manager_id -- in case we are the root node
from employees
where manager_id = 100
)
;
Fiddle at http://www.sqlfiddle.com/#!4/d15e7/32
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With