Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Deletion of node from binary search tree in java

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 111
Comment on it

Like a linked list we can also delete a node from binary search tree. This operation perform O(log(n)) time complexity.

Here is an example of deletion :

```private TreeNode delete(TreeNode rootNode, int k)

{
// create a node to store temp data
TreeNode node, node2, n;

if (root.getValue() == k)

{
// create both left and right node

TreeNode leftNode, rightNode;

leftNode = root.getLeftNode();

rightNode = root.getRightNode();

// return null if its last node or leaf
if (leftNode == null && rightNode == null)

return null;

else if (leftNode == null)

{

node = rightNode;

return node;

}

else if (rightNode == null)

{

node = leftNode;

return node;

}

else

{

node2 = rightNode;

node = rightNode;

while (node.getLeftNode() != null)

node = node.getLeftNode();

node.setLeftNode(leftNode);

return node2;

}

}

// traverse to correct node by checking node value, if vallue is greater than //current node then go right otherwise go left.

if (k < root.getValue())

{

n = delete(root.getLeftNode(), k);

root.setLeftNode(n);

}

else

{

n = delete(root.getRightNode(), k);

root.setRightNode(n);

}

return root;

}```

## 0 Comment(s)

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General