1 Replies - 2045 Views - Last Post: 03 October 2012 - 06:13 AM Rate Topic: -----

#1 saurabhpradeep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-February 12

how we count left and right child of a node in binary tree using php

Posted 03 October 2012 - 03:57 AM

$link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db('system_db') or die('Could not select database');
  include'config.php';  // Connect database

  function leftcount($node)   //Function to calculate leftcount
  {
    $sql = "SELECT lchild,rchild FROM tree WHERE parent = 'NULL'";
    $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;

This post has been edited by CTphpnwb: 03 October 2012 - 05:15 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how we count left and right child of a node in binary tree using php

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: how we count left and right child of a node in binary tree using php

Posted 03 October 2012 - 06:13 AM

So did you just copy/paste this code, or were you given it by your instructor?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1