Thread: deleting a node in a recursive way algorithm..

  1. #1
    Banned
    Join Date
    Jul 2009
    Posts
    46

    deleting a node in a recursive way algorithm..

    after solving the previus one with the root.
    i looked in
    Eternally Confuzzled - Binary Search Trees I
    but i coudnt see the general algorithm for
    deleting a node in a BST tree in a recursive way
    so it will remain BST afterward

    ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you delete a node in a binary search tree the data structure is still a binary search tree. The tree would be rebalanced when you insert something again. At least, that's my understanding.

  3. #3
    Banned
    Join Date
    Jul 2009
    Posts
    46
    i have some corrections to the original question:
    the node to be deleted is not te root
    it has only one son or no sons.

    regarding whiteflags post:
    if i free the wanted node i need to rearrange the tree in order to make it BST
    i have been given in a previous article to help
    but i cant see there a way of solving this one.

  4. #4

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    p
      \
      d
    /  \
    *  *
    
    p
     \
     d
    / \
    c  *
    If d has a child:
    d and c swap
    separate d from tree, replace with empty leaf
    free d

    If d is childless:
    separate d from tree
    replace d with empty leaf
    free d

    It's not much more than that and in fact it's one of the first things it covers regarding deletion. Like I said it's still a tree.

  6. #6
    Banned
    Join Date
    Jul 2009
    Posts
    46
    Quote Originally Posted by Spidey View Post
    can you specify what algorithm is for me?

    i looked it threw
    and it talks only about avl trees
    not bst
    although avl is a self balancing bst

    and there is no recursive node removal

    ??
    Last edited by cfan; 08-02-2009 at 02:25 AM.

  7. #7
    Banned
    Join Date
    Jul 2009
    Posts
    46
    are you sure that avl is my answer
    because i am ask to act on pure BST
    not self balancing bst trees

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    AVL trees are a form of BST trees which keep themselves balanced.
    The article has a section on bounded removal so that the tree will remain balanced again.
    If you do not intend to keep the tree balanced then do what whiteflags suggested.
    Spidey out!

  9. #9
    Banned
    Join Date
    Jul 2009
    Posts
    46
    correct me if i am wrong
    balanced means that height of the left subtree minus height of the right tree
    is less then two.

    so thats not what i am looking for

    so i will use whiteflags method:

    "If d has a child:
    d and c swap
    separate d from tree, replace with empty leaf
    free d"
    you didnt tell what node are you deleting.
    i didnt see what node dissapears in the tree

    ??

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Heres a way to simply remove a node from the tree -
    Code:
    template <typename ElemType>
    bool BST<ElemType>::remove(ElemType data) {
         return recRemoveNode(root, data);
    }
    template <typename ElemType>
    bool BST<ElemType>::recRemoveNode(nodeT *& t, ElemType data) {
      if (t == NULL) return false;
      int sign = cmpFn(data, t->data);
      if (sign == 0) {
        removeTargetNode(t);
        return true;
        } else if (sign < 0) {
        return recRemoveNode(t->left, data);
        } else {
        return recRemoveNode(t->right, data);
      }
    }
    This is the only recursive part required, the removetarget node keeps the bst a bst.
    Here are the 3 cases -

    Code:
    if (t->left == NULL)
    
    else if (t->right == NULL
    
    else
    Spidey out!

  11. #11
    Banned
    Join Date
    Jul 2009
    Posts
    46
    Code:
    template <typename ElemType>
    bool BST<ElemType>::remove(ElemType data) {
         return recRemoveNode(root, data);
    }
    template <typename ElemType>
    bool BST<ElemType>::recRemoveNode(nodeT *& t, ElemType data) {
      if (t == NULL) return false;
      int sign = cmpFn(data, t->data);
      if (sign == 0) {
        removeTargetNode(t);
        return true;
        } else if (sign < 0) {
        return recRemoveNode(t->left, data);
        } else {
        return recRemoveNode(t->right, data);
      }
    }
    i think its a c++ code i am not familiar with it.(i am taking a basic C course)
    what is

    i would like to understand the algorithm and to write the code my self.

    suppose i will find the node i want to delete

    there is a surrounding connections
    (the node to be deleted is not the root it has only one son or no sons.)
    so i will have to keep the father of the to be deleted node.
    and then reconnect the father with one of it sons

    correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM