lib/binary_search_tree/tree.rb
# rubocop:disable Metrics/AbcSize
# Deletes the node from the tree and update references accordingly.
# @param value [Numeric] the value of the node to be deleted
# @param node [BinarySearchTree::Node] the root note to start searching
# @return [BinarySearchTree<Node>] the deleted node
disable Metrics/AbcSize
Deletes the node from the tree and update references accordingly.
disable Metrics / AbcSize Deletes the node from the tree and update references accordingly .
def delete(value, node = root)
      delete_node = search(value, node)
      return unless node
      if delete_node.children.count == 2
        successor = inorder_successor(delete_node)
        successor.parent.replace_child(successor, successor.right)
        successor.copy_children(delete_node)
        delete_node.parent.replace_child(delete_node, successor)
      else
        child = delete_node.children.first
        child.parent = delete_node.parent if child
        delete_node.parent.replace_child(delete_node, child)
      end
      delete_node
    end
def delete ( value , node = root ) delete_node = search ( value , node ) return unless node if delete_node . children . count == 2 successor = inorder_successor ( delete_node ) successor . parent . replace_child ( successor , successor . right ) successor . copy_children ( delete_node ) delete_node . parent . replace_child ( delete_node , successor ) else child = delete_node . children . first child . parent = delete_node . parent if child delete_node . parent . replace_child ( delete_node , child ) end delete_node end
rubocop:disable Metrics/AbcSize
Deletes the node from the tree and update references accordingly.
rubocop : disable Metrics / AbcSize Deletes the node from the tree and update references accordingly .
# Find the node to be deleted # Detach the successor and assign its right side to parent # Swap the successor with the current node
the deleted node
the deleted node
BinarySearchTree<Node>
value
the value of the node to be deleted
the value of the node to be deleted
node
the root note to start searching
the root note to start searching
root