Nuv Posted April 25, 2011 Share Posted April 25, 2011 I am trying to count left node of a binary tree.I made a function but my result is always 3 . Code:- <?php include'config.php'; // Connect database function leftcount($node) //Function to calculate leftcount { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild'])) { $count++; leftcount($array['lchild']); } if(!empty($array['rchild'])) { $count++; leftcount($array['rchild']); } $totalcount = 1 + $count; return $totalcount; } $parent = "2"; $left = leftcount($parent); echo $left; ?> Can someone please point the error in my logic ? I searched google but i only got scripts with classes and i don't want to use classes. Quote Link to comment https://forums.phpfreaks.com/topic/234642-counting-binary-tree/ Share on other sites More sharing options...
samona Posted April 25, 2011 Share Posted April 25, 2011 I'm thinking that your variable $count is local to the function that's executing. Have you tried making it a global variable? Quote Link to comment https://forums.phpfreaks.com/topic/234642-counting-binary-tree/#findComment-1205872 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2011 Share Posted April 25, 2011 You're returning the totalcount from the function, but not doing anything with it when you call the function recursively. Try something like: <?php include 'config.php'; // Connect database function leftcount($node) //Function to calculate leftcount { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild'])) { $count += leftcount($array['lchild']); } if(!empty($array['rchild'])) { $count += leftcount($array['rchild']); } $totalcount = 1 + $count; return $totalcount; } $parent = "2"; $left = leftcount($parent); echo $left; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234642-counting-binary-tree/#findComment-1205878 Share on other sites More sharing options...
Nuv Posted April 25, 2011 Author Share Posted April 25, 2011 You're returning the totalcount from the function, but not doing anything with it when you call the function recursively. Try something like: <?php include 'config.php'; // Connect database function leftcount($node) //Function to calculate leftcount { $sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'"; $execsql = mysql_query($sql); $array = mysql_fetch_array($execsql); if(!empty($array['lchild'])) { $count += leftcount($array['lchild']); } if(!empty($array['rchild'])) { $count += leftcount($array['rchild']); } $totalcount = 1 + $count; return $totalcount; } $parent = "2"; $left = leftcount($parent); echo $left; ?> Ken Ah how can i miss that Thankyou though. It worked. Quote Link to comment https://forums.phpfreaks.com/topic/234642-counting-binary-tree/#findComment-1205953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.