Preordered tree traversal: conversion from adjency list

Debate over usage of agency list or preordered tree traversal are numerous,
we don't enter in pros and cons of each one here, we just provide a recursive procedure
to move from agency list to PTT representation and a query using the MODEL clause providing the same functionality.
Here is a recursive procedure to convert an agency list model to a PTT one,
assuming we will add the left, right, level and sibling index information (adapt to your needs)

CREATE OR REPLACE PROCEDURE INIT_PTT
IS
r NUMBER(10,0) := 0 ;
l NUMBER(10,0) := 1 ;
PROCEDURE branches(lvl IN NUMBER, p_id IN NUMBER, lft IN NUMBER, rgt OUT NUMBER)
IS
l1 NUMBER(10,0) := lft;
lvl1 NUMBER(10,0) := lvl+1;
idx NUMBER(10,0) := 0 ;
BEGIN
rgt := lft + 1 ;
FOR rec IN (SELECT id, parent_id, name FROM t_data WHERE parent_id = p_id ORDER BY name)
LOOP
branches(lvl1, rec.id, l1+1, rgt) ;
UPDATE t_data set lvl = lvl1, left = (l1+1), right = rgt, sibling_idx = idx WHERE id = rec.id ;
idx := idx + 1;
l1 := rgt ;
rgt := rgt + 1;
END LOOP ;
END ;
BEGIN

FOR rec IN (SELECT id FROM t_data WHERE parent_id is NULL ORDER BY name)
LOOP
branches(0, rec.id, l, r) ;
UPDATE t_data set lvl = 0, left = l, right = r, sibling_idx = 0 WHERE id = rec.id ;
l := r + 1 ;
END LOOP ;

COMMIT ;
END ;
Now the MODEL-based query returning the left, right, level and sibling index values.