R – How to delete a binary tree node with all possibility

binary treec

I created a binary tree with some integer values, I can search the tree by my code. But I don't know how to proceed delete node operation.

So, how can I delete a node?

Best Answer

Wikipedia entry - Binary Search Tree - explains how to implement BST operations.

Deletion: There are several cases to be considered:

  • Deleting a leaf: Deleting a node with no children is easy, as we can simply remove it from the tree.
  • Deleting a node with one child: Delete it and replace it with its child.
  • Deleting a node with two children: Call the node to be deleted "N". Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, "R". Replace the value of N with the value of R, then delete R. (Note: R itself has up to one child.)