1 Replies - 237 Views - Last Post: 09 January 2013 - 11:32 AM Rate Topic: -----

#1 aya_eltokhy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 07-November 12

deleting node from binary search tree

Posted 09 January 2013 - 10:18 AM

hi everyOne :clap:

i have this code for deleting node from binary search tree but becouse :stupid: I did not understand the last part Can anyOne explain it to me.
# define Ok 0
# define Error -1 
# define Lchild 1
 # define Rchild 2 
int bt_delete ( struct bt_node * pred , struct bt_node *node , int stat ) 
{ struct bt_node *child; 
if ( node == Null ) 
return ( Error ); 
if ( pred == Null ) 
{ root = node ─> rchild ;
 child = node ─> lchild;
 return ( bt_insert2 ( child ) );
 } 
if ( node ─> lchild == Null && node ─> rchild == Null )
child = Null; 
elseif ( node ─> lchild == Null ) 
child = node ─> rchild ; 
elseif ( node ─> rchild == Null ) 
child = node ─> lchild ; 
elseif ( node ─> lchild ─> rchild == Null )
 { child = node ─> lchild ;
 node ─> lchild ─> rchild = node ─> rchild; } 
elseif ( node ─> rchild ─> lchild == Null )
 { child = node ─> rchild ; 
node ─> rchild ─> lchild = node ─> lchild; }


//this part which i didnt understand
else
 {child = node ─> rchild;
 if ( stat = Lchild ) 
pred ─> lchild = node ─> rchild ;
 else 
pred ─> rchild = node ─> rchild ;
 bt_freenode ( node );
 return ( bt_insert2 ( node ─> lchild ) ) ; } 
if ( stat == Lchild )
 pred ─> lchild = child ; 
else 
pred ─> rchild = child ;
 return ( Ok ); }



Is This A Good Question/Topic? 0
  • +

Replies To: deleting node from binary search tree

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,634
  • Joined: 05-May 12

Re: deleting node from binary search tree

Posted 09 January 2013 - 11:32 AM

Have you tried drawing out the what is happening with the pointers on paper? I think it will make things much clearer.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1