Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneral tree traversal(infinite) in breadth-first search manner
    primarykey
    data
    text
    <p>I have a Tree Structure where each node has 5 child nodes and more than that are not allowed. I wish to traverse this tree in breadth-first search manner.</p> <p><img src="https://i.stack.imgur.com/KdMjj.png" alt="enter image description here"></p> <p>Now I wish to calculate empty node from selected parent using breadth-first search manner.</p> <p>e.g.</p> <ol> <li>if given parent is 1, then function must return node 4 because it has positions available. </li> <li>If given parent is 2, then it must return node 7</li> </ol> <p>I am using PHP(codeigniter) + Mysql for this.</p> <p><strong>My controler</strong></p> <pre><code>public function addmember() { $current_node = $this-&gt;input-&gt;post('member'); $empty_location = $this-&gt;tree_model-&gt;GetEmptyPositions($current_node); if($empty_location != 0) { echo "Position available"; } else { $next_nodes = $this-&gt;tree_model-&gt;GetAllChilds($current_node); $i=0; for($i=0;$i&lt;5;$i++){ $result = $this-&gt;tree_model-&gt;GetEmptyPositions($next_nodes[$i]); if($result != 0 ) { $current_node = $next_nodes[$i]; goto exe; } } } exe: echo $current_node; } </code></pre> <p><strong>and my model</strong> </p> <pre><code>//get number of empty nodes of current member public function GetEmptyPositions($id) { $this-&gt;db-&gt;select('empty_position'); $this-&gt;db-&gt;from('member'); $this-&gt;db-&gt;where('member_id',$id); $result = $this-&gt;db-&gt;get(); if ($result-&gt;num_rows() &gt; 0) foreach($result-&gt;result() as $empty_pos) return $empty_pos-&gt;empty_position; } //get all childs of current node public function GetAllChilds($id) { $this-&gt;db-&gt;select('member_id'); $this-&gt;db-&gt;from('member'); $this-&gt;db-&gt;where('tree_parent_id',$id); $result = $this-&gt;db-&gt;get(); if ($result-&gt;num_rows() &gt; 0) { $i = 0; foreach($result-&gt;result_array() as $member_id) { $member_array[$i] = $member_id['member_id']; $i++; } return $member_array; } } </code></pre> <p><strong>Database</strong></p> <pre><code>CREATE TABLE IF NOT EXISTS `member` ( `member_id` int(11) NOT NULL AUTO_INCREMENT, `datetime` datetime DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, `tree_parent_id` int(11) DEFAULT NULL, `empty_position` tinyint(4) DEFAULT '5', // stores how many empty positions remain if zero move to next node `name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`member_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; </code></pre> <p><strong>Where I stuck up!</strong></p> <p>I am able to traverse till node 6 by above code. but in next iteration i have need to check @ node 7 since nodes will be in order 5 rase to n and it is not finite tree structure.</p> <p>next tree traversal order 7 8 9 10 11 12 13 14 16 ......</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload