Results 1 to 7 of 7
  1. #1

    Default how to delete a node from a binary tree


    sa tanan,

    pls. help me find what's wrong with the code i'm writing. this method is use
    to delete a node in a binary tree. Dili man mogana.
    Here's the code:
    Code:
                public void delete(Node node, int value)  // akong ipasa diri kay ang root node og ang value sa node nga idelete.
                {
                        Node n = new Node(value); //naa na koy gicreate nga Node class nga nag.implement og Comparable where ang value akong gicompare.
                        if(node != null)
                       {
                           if(node.compareTo(n) == 0)
                                   node = null; 
                           else if(node.compareTo(n) < 0)
                                delete(node.left, value);
                           else if(node.compareTo(n) > 0)
                                delete(node.right, value);
                       }else System.out.println("Node not found!");
                 }

  2. #2
    1. is this recursive? if it is kay wala nimu gi usab ang pointers after deleting your node..

    2. why not just use the search function first before deleting the node? or maybe naa nani sa imu compare na method?

    but if ang imung compare function is only comparing the values, and wala nag iterate sa imung tree,
    i think ni delete raka daun ug child if you find that your value is lesser or greater than your root.
    in other words, imu ra daun gi delete ang child na wa ka kahibaw if that is what you were searching for,

    maybe it is still way down your tree.

    ----------------------------10
    ---------------------5---------------12
    -----------------3-------6--------11-----15

    if this is the case, u can only delete that 5 and 12. and even if u input 4 or 3 or 2. what will be deleted is the 5. enlighten me if wala kau ko ka gets sa imu situation.. hehe

  3. #3
    Code:
     if(node.compareTo(n) == 0)
                                   node = null; 
                           else if(node.compareTo(n) < 0)
                                delete(node.left, value);
                           else if(node.compareTo(n) > 0)
                                delete(node.right, value);
    wala ko kasabot sa logic, murag imo man dayon gi delete ang tibuok subtree instead of traversing it

  4. #4
    @-_-:thanks for the reply..
    yes, ako ra unta idelete ang tibuok subtree, pareha sa folder scenario nga if idelete nimo ang folder, madelete pod tanan niya nga subfolders.

  5. #5
    Quote Originally Posted by g_em View Post
    1. is this recursive? if it is kay wala nimu gi usab ang pointers after deleting your node..

    2. why not just use the search function first before deleting the node? or maybe naa nani sa imu compare na method?

    but if ang imung compare function is only comparing the values, and wala nag iterate sa imung tree,
    i think ni delete raka daun ug child if you find that your value is lesser or greater than your root.
    in other words, imu ra daun gi delete ang child na wa ka kahibaw if that is what you were searching for,

    maybe it is still way down your tree.

    ----------------------------10
    ---------------------5---------------12
    -----------------3-------6--------11-----15

    if this is the case, u can only delete that 5 and 12. and even if u input 4 or 3 or 2. what will be deleted is the 5. enlighten me if wala kau ko ka gets sa imu situation.. hehe
    Thanks for the reply!!!
    reply lng pod ko:
    1. yes, recursive ni nga method.... do i need to change the pointers? ang akong gusto unta idelete ra man ang tibuok subtree just like the folder scenario bitaw nga if idelete nimo ang folder, madelete pod iya subfolders.

    2. wala nuon ko magsearch didto sa akong compareTo() method;... i think dili dayon madelete if value is lesser or greater sa root node value kay mo recurse pa man siya then moagi pa siya sa first nga if statement which ngvalidate if equal ba ang mga values, then diha pa niya idelete. sakto ba ko? hehe..\
    thanks..

  6. #6
    Quote Originally Posted by cencio View Post
    @-_-:thanks for the reply..
    yes, ako ra unta idelete ang tibuok subtree, pareha sa folder scenario nga if idelete nimo ang folder, madelete pod tanan niya nga subfolders.
    @cencio, ambot lang, pero sa ako ra ni, dapat man guro jud nimo itraverse ang tree before nimo ma delete ang subtree, para safe lang basin dili ma free ang resources allocated sa mga nodes within the subtree

  7. #7

    Default Re: how to delete a node from a binary tree

    Java man... hehehe libog ko gamay atay C rajud mi dri sa USC... very unrealistic but C is the foundation of that currently taking java now... hehehe intro pami...

  8.    Advertisement

Similar Threads

 
  1. Replies: 1
    Last Post: 02-11-2011, 04:10 PM
  2. How to recover desktop files from deleted account,
    By dodoijose in forum Networking & Internet
    Replies: 6
    Last Post: 02-07-2011, 09:09 PM
  3. how to remove/erase text from picture w/o damaging the picture
    By micmic in forum Software & Games (Old)
    Replies: 5
    Last Post: 04-14-2006, 12:19 PM
  4. How to save video streaming from youtube.com
    By koto in forum Software & Games (Old)
    Replies: 29
    Last Post: 03-26-2006, 10:54 PM
  5. How to make a connection from my 6600 to computer?
    By rey04 in forum Gizmos & Gadgets (Old)
    Replies: 7
    Last Post: 12-03-2005, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top