removing a leaf node

Page 1 of 1

1 Replies - 447 Views - Last Post: 19 October 2011 - 09:05 AM Rate Topic: -----

#1 imu_1   User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • Posts: 256
  • Joined: 03-June 11

removing a leaf node

Posted 19 October 2011 - 09:04 AM

Hello,
I need to write a method which passes a node as a parameter and removes that node only if that node is leaf node. If its not,then it should not do anything.

So here's what I did:


            private Node<AnyType> remove( AnyType x, Node<AnyType> t) 
    { 
        if( t == null) 
          return t;       // do nothing , return the same element
 
          
		  else 
		  {
		   if(t.element == x && t.left == null && t.right == null) 
		      t.element = null; 
		    
		  
		    if(t.left != null) 
		     {        
			         System.out.println("Inside remove left");
					 return remove(x,t.left); 
			 } 
			else if(t.right !=null) 
			{ 
			  System.out.println("Inside remove right");
			return remove(x,t.right); 
		     }
		   }
	     return t;
	}

 



This method works only if the node that am tyring to delete is on the left subtree.If it on the right,then it doesnt remove it.It compiles and runs fine,it just doesnt remove the specific node if that nod eis on the right subtree and if its a leaf node. I hope my explanation is clear. I will appreciate your responses.

Is This A Good Question/Topic? 0
  • +

Replies To: removing a leaf node

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,834
  • Joined: 12-June 08

Re: removing a leaf node

Posted 19 October 2011 - 09:05 AM

Please do not open duplicate threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1