Generating Balanced Tree Using SQL Hierarchical Queries
BilalMay 12 2011 — edited May 13 2011Hi All,
I have the following hierarchical data having different sub-tree levels:
A0
-A001
--A00101
A1
-A101
A2
-A201
--A20101
---A201010001
A0's sub-tree has 3 levels, A1's sub-tree has 2 levels, and A3's sub-tree has 4 level. I want to generate a balanced tree out of the given data having all sub-tree levels equal to the maximum number of levels available in the whole tree which in this particular case is 4.
I dont know it would be possible with SQL. The script to generate the above mentioned data is as below:
CREATE TABLE codes_tree
(node_id VARCHAR2(10),
parent_node_id VARCHAR2(10)
);
INSERT INTO codes_tree VALUES('A0',NULL);
INSERT INTO codes_tree VALUES('A001','A0');
INSERT INTO codes_tree VALUES('A00101','A001');
---
INSERT INTO codes_tree VALUES('A1',NULL);
INSERT INTO codes_tree VALUES('A101','A1');
---
INSERT INTO codes_tree VALUES('A2',NULL);
INSERT INTO codes_tree VALUES('A201','A2');
INSERT INTO codes_tree VALUES('A20101','A201');
INSERT INTO codes_tree VALUES('A201010001','A20101');
Any help will be highly appreciated.
Thanks ... Best Regards
Edited by: naive2Oracle on May 12, 2011 7:40 PM
Edited by: naive2Oracle on May 12, 2011 7:41 PM