Jump to content

Adding a node - Balanced B - tree


Nuv

Recommended Posts

I would like to adding a node to a tree. I want it to be a balanced b-tree. The logic/algorithm i can think of is

 

<?php
function add_node($root, $node) // Where $root is the parent to which node should be added and $node is the node that is to be added.
{
     Traverse the tree level wise, level 1 being the lchild and rchild of $root and calculate the number of node in that level
     if(number of nodes in a level < 2[sup]$level[/sup])  // Since number of nodes a level can accommodate  = 2[sup]$level[/sup] 
     {
          Add node in that level where ever it gets an empty spot.
     }
}
?>

 

Tree table:-

CREATE TABLE IF NOT EXISTS `tree` (
  `parent` int(7) NOT NULL,
  `rchild` int(7) NOT NULL,
  `lchild` int(7) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

Is their any better logic to do so ?

Link to comment
https://forums.phpfreaks.com/topic/235291-adding-a-node-balanced-b-tree/
Share on other sites

I made this function but it won't work. My php editor crashes without giving errors.Am i doing it right ?

 

function add_node($root, $node)
{
    $sql = "SELECT lchild,rchild FROM tree WHERE parent='".$root."'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    if((empty($array['lchild'])) && (empty($array['rchild'])))
    {
       
   $insert = "INSERT INTO tree (lchild, parent) VALUES ('".$node."', '".$root."')";
    $exec_insert = mysql_query($insert);    
    }
    elseif(empty($array['lchild']) && !empty($array['rchild']))
    {
      $update = "UPDATE tree SET lchild='".$node."' WHERE parent='".$root."' AND rchild='".$array['rchild']."'"; 
      $exec_update = mysql_query($update);
       
    }
    elseif(empty($array['rchild']) && !empty($array['lchild']))
    {
      $update = "UPDATE tree SET rchild='".$node."' WHERE parent='".$root."' AND lchild='".$array['lchild']."'"; 
      $exec_update = mysql_query($update); 
    }
     elseif(!empty($array['rchild']) && !empty($array['lchild']))
    {
        echo "this";
     add_node($array['lchild'],$node);
     add_node($array['rchild'],$node);  
    } 
}

Hey Nuv,

  When you do a a mysql_query, you should check for a result.  The problem could be that you have a mysql_error.  See the link in my signature below about this.

 

The other issue with your code, is that for an empty result set mysql_fetch_array() will return false.  So what would be better for your first condition would be to call mysql_num_rows() and check if its return value is > 0. 

 

 

Wow gizmola.Not only  i sorted my error in this script i also checked my other script which wasnt workingand i got this error.

 

Error

Could not successfully run query (INSERT INTO member (member_id, id_sponsor, doj, toj, position, title, fname, mname, lname, date_birth, father_name, status, gender, address, country, state, city, pin, rnumber, onumber, mobile, email, nominee_title, nominee_name, relationship, bank_name, branch_name, account_number, cheque_name, pan, ifsc_code, micr_code, pin_serial, pin_number, pin_type, pin_value, balance_amount, password, security_question, answer) VALUES (8475921, 234234, '2011-05-03', '14:20:20', 'left', 'mr', 'Nuv', '', 'Sharma', '1987-10-31', 'Soemthing', 'status', 'male', 'Palace', 'India', 'Dadar and Nagar Haveli', 'King city', 411011, , , 345435534, '[email protected]', 'mr', 'Noone', 'father', 'HSBC', 'sdfsdfq', '4354654', '', '', '', '', '', '', '', '', '', 'f757382eeb95b07b792bb2e7195468b8', 'something ', 'nothing')) from DB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , 345435534, ' at line 37

 

Code

$insert = "INSERT INTO member (member_id, id_sponsor, doj, toj, position, title, fname, mname, lname, date_birth, father_name, status, gender, address, country, state, city, pin, rnumber, onumber, mobile, email,
                     nominee_title,
                     nominee_name,
                     relationship,
                     bank_name,
                     branch_name,
                     account_number,
                     cheque_name,
                     pan,
                     ifsc_code,
                     micr_code,
                     pin_serial,
                     pin_number,
                     pin_type,
                     pin_value,
                     balance_amount,
                     password,
                     security_question,
                     answer) VALUES (".$member_id.",
                     ".$sponsor.", 
                     '".$current_date."', 
                     '".$current_time."', 
                     '".$place."', 
                     '".$title."', 
                     '".$fname."', 
                     '".$mname."', 
                     '".$lname."',
                     '".$date_birth."',
                     '".$ffname."',
                     '".$status."',
                     '".$gender."',
                     '".$address."',
                     '".$country."',
                     '".$state."',
                     '".$city."',
                     ".$pincode.",
                     ".$phoner.",
                     ".$phoneo.",
                     ".$mobile.",
                     '".$email."',
                     '".$ntitle."',
                     '".$nname."',
                     '".$relation."',
                     '".$bname."',
                     '".$branchname."',
                     '".$accnum."',
                     '".$cheque_name."',
                     '".$pnum."',
                     '".$ifsc."',
                     '".$micr."',
                     '".$epin_serial."',
                     '".$epin."',
                     '".$pin_type."',
                     '".$pin_value."',
                     '".$bal_amnt."',
                     '".$pass."',
                     '".$squestion."',
                     '".$sanswer."')";
$execinsert = mysql_query($insert); 
if (!$result) {
    echo "Could not successfully run query ($insert) from DB: " . mysql_error();
    exit;
}

 

Why won't my insert work ?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.