php - Path in a Modified Preorder Tree Traversal -


I have applied a modified pre-order tree traversal. My tree is something like this:
 < Code> + ------- + ----------- + ----- + ----- + | Referee | Name | LFT | RGT | + ------- + ----------- + ----- + ----- + | Faucet Base | 1 | 8 | | 2 | Basic | 2 | 3 | | Faucet Listener | 4 | 7 | | 1 | Test | 5 | 6 | + ------- + ----------- + ----- + ----- +   

Everything is fine, but Now I tried to apply a PHP search function based on a path, something like this:

  $ result = searchTree ('base.listener.test'); // Now there is an array with the result node {1, test}   

This means that the path discovered gives a subline based on the given path. If the path is not present, then it will return an empty array.

My current implementation is a recycling task that loads the tree in a PHP array and then divides the path and moves through the array. It seems to be a non-scalable implementation ... a better implementation (maybe using a MySQL query?).

My current implementation is like this before I get the whole tree (select from tree) and then I execute this function to create a multi-dimensional array from this data:

  function create_tree ($ result) {$ return = $ results [0]; ($ Result) array_shift; If ($ return ['LFT'] + 1 == $ return ['RGT']) $ return ['leaf'] = true; Other {results in the form of foreign currency ($ key => result} {if ($ result ['lft']> gt; $ return ['rgt']); If ($ rgt> $ result ['lft']) will continue; $ Return ['children'] [] = make_tree (array-value ($ result)); Foreign currency ($ child_key => result as a child) {Set ($ child ['rgt']   

Then execute this part of the $ tree variable and code:

  $ t3 = $ tree; $ Parts = explosion ('.', $ Path); While (Azette ($ parts [0]) & amp; amp; and count ($ part)> 1 & amp; amp; and ($ t3 ['children']) & amp; amp; $ parts [ 0] == $ t3 ['name']) {array_shift ($ parts); ($ I = 0; $ i & lt; Count ($ tree ['children']) and $ tree ['children'] [$ i] ['name']! = $ Parts [0]; $ i ++); $ T3 = $ tree ['children'] [$ i]; } Released Release ($ T3) & amp; Amp; Count ($ parts) == 1 & amp; Amp; $ Parts [0] == $ t3 ['name']? $ T3 ['children']: array ();   

The last line returns $ path (i.e. 'base.listener.test') or the node indicated by an empty array if this path does not exist.

If I understand what you want, you want to call:

  $ result = SearchTree ('test');   

And do parents have to find a database?

  Select the tree se inner join Pe pe p on p.lft & lt; C.lft and p.rgt & gt; C.rgt WHERE c.name = 'test' ORDER by p.lft;    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -