askbapi Posted March 2, 2010 Share Posted March 2, 2010 I can create a Binary tree with help of PHP and MYSQL by create a recursive function. AND it work fine. BUT it is very slow. I create a tree with 9000+ nodes and it took 46 seconds only to create tree. Than I need to find the pairs on both side left and right. So I was think it to create with array. How can i do it only with array. MYSQL Table structure >member_id > parent_id Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/ Share on other sites More sharing options...
Mchl Posted March 2, 2010 Share Posted March 2, 2010 See here for some hints about trees in MySQL Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020556 Share on other sites More sharing options...
askbapi Posted March 3, 2010 Author Share Posted March 3, 2010 Thank for ur reply. I want to create a BINARY TREE with array, not with MYSQL. Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020752 Share on other sites More sharing options...
Mchl Posted March 3, 2010 Share Posted March 3, 2010 I doubt that implementation using arrays will be fast either. You can also try to create a class like class BinaryTreeNode { private $left; private $right; public function setLeft(BinaryTreeNode $node) { $this->left = $node; } public function setRight(BinaryTreeNode $node) { $this->right = $node; } public function getLeft() { return $this->left; } public function setRight() { return $this->right; } } Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020791 Share on other sites More sharing options...
askbapi Posted March 3, 2010 Author Share Posted March 3, 2010 Thanks again for ur help. Here is the a code that I created while($gaRow = mysql_fetch_assoc($rResult)) { $aMemCore[$i][1] = $gaRow['mem_idx'] ; // Member ID $aMemCore[$i][2] = $gaRow['mem_pntidx'] ; // Parent ID $i++; } unset($gaRow); function getNodes($preantid,$arr) { for($j=0; $j <= 8473; $j++) {// echo $j; if($arr[$j][2]== $preantid) { echo "===<br>--".$arr[$j][1]; getNodes($arr[$j][1],$arr); } } } getNodes(0,$aMemCore); unset($amemCore); After this it brought the time from 42 to 22 secs. It ok but not gud. Thanks again for ur help. Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020818 Share on other sites More sharing options...
Mchl Posted March 3, 2010 Share Posted March 3, 2010 echo takes time. Try like this: function getNodes($preantid,$arr) { $result = ''; for($j=0; $j <= 8473; $j++) {// echo $j; if($arr[$j][2]== $preantid) { $result .= "===<br>--".$arr[$j][1]; $result .= getNodes($arr[$j][1],$arr); } } return $result; } echo getNodes(0,$aMemCore); unset($amemCore); Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020820 Share on other sites More sharing options...
askbapi Posted March 3, 2010 Author Share Posted March 3, 2010 Yes, it worked. Thanks for improving my code Link to comment https://forums.phpfreaks.com/topic/193927-efficient-binary-tree/#findComment-1020948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.