I'm basically trying to build an html ul/li nested list from a multidimensional array representing a tree structure.
The following code works fine but I want to improve it:
I need a way to keep track of the recursion level so I can apply different classes to different levels, add indenting to the generated output, etc.
function buildTree($tree_array, $display_field, $children_field, $class='', $id='') {
echo "<ul>\n";
foreach ($tree_array as $row) {
echo "<li>\n";
echo $row[$display_field] . "\n";
if (isset($row[$children_field])) {
$this->buildTree($row[$children_field]);
}
echo "</li>\n";
}
echo "</ul>\n";
}
The $tree_array looks like this:
Array
(
[0] => Array
(
[category_id] => 1
[category_name] => calculatoare
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 4
[category_name] => placi de baza
[parent_id] => 1
)
[1] => Array
(
[category_id] => 5
[category_name] => carcase
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 6
[category_name] => midi-tower
[parent_id] => 5
)
)
)
)
)
[1] => Array
(
[category_id] => 2
[category_name] => electronice
[parent_id] => 0
)
[2] => Array
(
[category_id] => 3
[category_name] => carti
[parent_id] => 0
)
)
I've tagged this as homework because I'd like to use this as an opportunity to improve on my (poor) understanding of recursion so, I'd appreciate answers that would guide me to the solution rather than provide a complete working example :)
Recursive preorder traversal of binary tree In recursive preorder traversal, we first process the root node, then process all nodes in the left subtree, and finally, we process all nodes in the right subtree. Suppose we use a function preorder(root) with root as an input parameter.
Depth First Search (DFS)The DFS algorithm is a recursive algorithm that uses the idea of backtracking. It involves exhaustive searches of all the nodes by going ahead, if possible, else by backtracking.
3.1. Binary Tree as a Recursive Data Structure. A recursive data structure is a data structure that is partially composed of smaller or simpler instances of the same data structure. For example, linked lists and binary trees can be viewed as recursive data structures.
Simple examples of a recursive function include the factorial, where an integer is multiplied by itself while being incrementally lowered. Many other self-referencing functions in a loop could be called recursive functions, for example, where n = n + 1 given an operating range.
However, the binary tree is a nonlinear structure, and each node may have two trees. So, we need to find rules that can make all nodes of a binary tree are arranged on a linear queue. So binary tree traversal of each node is in accordance with a path to access binary tree, and each note can be visited only once.
Hence, if a tree has n nodes, then each node is visited only once in inorder traversal and hence the complexity of the inorder traversal of the binary tree is O ( n ) O(n) O(n) . Space Complexity Analysis: The space complexity of the recursive inorder traversal is O ( h ) O(h) O(h) for a tree of height h.
Quick'n'dirty approach (see "spoiler" block below for the implementation):
Add an additional variable $recursionDepth
to your function declaration, make it 0
by default.
On each subsequent recursion, call your function with $recursionDepth + 1
.
Since the function variables are only "visible" (scoped) for the respective instance of the function, you'll end up with an indicator of the current iteration depth.
Also, line 12 of your function
$this->buildTree();
doesn't look to me as if it would work – the reason being that you're not passing your variables on to the next instance of buildTree
.
It probably should look like this:
$this->buildTree($row[$children_field], $display_field, $children_field, $class, $id)
Here's the changes I'd make to your code to achieve what you want:
function buildTree($tree_array, $display_field, $children_field, $class='', $id='', $recursionDepth = 0, $maxDepth = false)
{
if ($maxDepth && ($recursionDepth == $maxDepth)) return;
echo "<ul>\n";
foreach ($tree_array as $row)
{
echo "<li>\n";
echo $row[$display_field] . "\n";
if (isset($row[$children_field]))
$this->buildTree($row[$children_field], $display_field, $children_field, $class, $id, $recursionDepth + 1, $maxDepth);
echo "</li>\n";
}
echo "</ul>\n";
}
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