PHP Binary Tree Recursion Algorithm?

If I understand correctly, this is what you want: function tree(array $data, &$tree = array(), $level = 0) { // init if (!isset($tree$level)) $tree$level = array(); foreach ($data as $key => $value) { // if value is an array, push the key and recurse through the array if (is_array($value)) { $tree$level = $key; tree($value, $tree, $level+1); } // otherwise, push the value else { $tree$level = $value; } } } Use it like this: $binary_tree = array(1 => array(2 => array(4,5),4=>array(5,6))); tree($binary_tree, $output); var_dump($output) This gives you: array(3) { 0=> array(1) { 0=> int(1) } 1=> array(2) { 0=> int(2) 1=> int(4) } 2=> array(4) { 0=> int(4) 1=> int(5) 2=> int(5) 3=> int(6) } }.

If I understand correctly, this is what you want: function tree(array $data, &$tree = array(), $level = 0) { // init if (!isset($tree$level)) $tree$level = array(); foreach ($data as $key => $value) { // if value is an array, push the key and recurse through the array if (is_array($value)) { $tree$level = $key; tree($value, $tree, $level+1); } // otherwise, push the value else { $tree$level = $value; } } } Use it like this: $binary_tree = array(1 => array(2 => array(4,5),4=>array(5,6))); tree($binary_tree, $output); var_dump($output); This gives you: array(3) { 0=> array(1) { 0=> int(1) } 1=> array(2) { 0=> int(2) 1=> int(4) } 2=> array(4) { 0=> int(4) 1=> int(5) 2=> int(5) 3=> int(6) } }.

Thanks! This actually is precisely what i'm looking for. – Jonathan Kushner Feb 17 at 0:19.

This function in class Tree view you have got a return of array which has all node value then you can arrange on below tree view . Function getTreeDataFromReg($setid) { if(!empty($setid)) { for($in=0 ;$in0) { $result=$this->selectQuery("tbl_registration"," * "," fl_reg_id ='". $setid$in."'"," fl_placment_side ASC "); $setar=mysql_fetch_array($result); $leftid=$setar'fl_left_id'; $rightid=$setar'fl_right_id'; }else { $leftid=0; $rightid=0; } switch($in) { case 0: $setid1=$leftid; $setid2=$rightid; break; case 1: $setid3=$leftid; $setid4=$rightid; break; case 2: $setid5=$leftid; $setid6=$rightid; break; case 3: $setid7=$leftid; $setid8=$rightid; break; case 4: $setid9=$leftid; $setid10=$rightid; break; case 5: $setid11=$leftid; $setid12=$rightid; break; case 6: $setid13=$leftid; $setid14=$rightid; break; } } } return $setid; } function printTreeView($parentid) { $setid=array($parentid); $setarra=$this->getTreeDataFromReg($setid); return $setarra; } 0 / \ 1 2 / \ / \ 3 4 5 6 and on /\ / \ /\ /\ Try it .

If any problem then post it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions