Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Created February 18, 2022 19:14
Show Gist options
  • Save maxgoren/2e1ed60afed9354c2889306d3fe27118 to your computer and use it in GitHub Desktop.
Save maxgoren/2e1ed60afed9354c2889306d3fe27118 to your computer and use it in GitHub Desktop.
delete arbitrary nodes
private Node<K,V> delete(Node<K,V> h, K key)
{
if (h == null)
return null;
if (key.compareTo(h.key()) < 0)
h.setLeft(delete(h.leftChild(), key));
else if (key.compareTo(h.key()) > 0)
h.setRight(delete(h.rightChild(), key));
else {
if (h.rightChild() == null)
return h.leftChild();
Node<K,V> temp = h;
h = min(temp.rightChild());
h.setRight(deleteMin(temp.rightChild()));
h.setLeft(temp.leftChild());
}
return h;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment