Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Delete(T, K)
- {
- if (T == null) return; // K not found so nothing to do
- if (T.root == K)
- {
- if (T.left <> null)
- {
- BinaryTree max = findMax(T.left);
- T.root = max.root;
- Delete(T.left, max.root);
- }
- else if (T.right <> null)
- {
- BinaryTree min = findMin(T.right);
- T.root = min.root;
- Delete(T.right, min.root);
- }
- else
- {
- Free(T);
- }
- } // T is a leaf so free its memory allocation (e.g., T.root = null)
- else if (T.root < K)
- {
- Delete(T.right, K);
- }
- else if (T.root > K)
- {
- Delete(T.left, K);
- }
- cleanUp(T); // remove any leaf nodes where T.root = null
- }
Add Comment
Please, Sign In to add comment