Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the entire tree from the root giving any node

Tags:

oracle

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.

like image 319
seesee Avatar asked Jul 24 '12 05:07

seesee


People also ask

Is way to get to any node in the tree from its root?

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.

Can any node be a root in a tree?

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.

How do you calculate nodes in a tree?

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.

Which node is the root of the tree?

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.


1 Answers

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

Edit:

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

like image 96
GWu Avatar answered Oct 27 '22 05:10

GWu