How do you understand this code, BinarySearch Delete

Is my guess correct?
6 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Rag...JN 🌌 🦡 👽 💰! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Rag...JN 🌌 🦡 👽 💰 🐊
// Given a binary search tree and a key, this function deletes the key and returns the new root
Node deleteNode(Node root, int key) {
// Base case
if (root == null) {
return root;
}

// If the key to be deleted is smaller than the root's key, then it lies in the left subtree
if (key < root.key) {
root.left = deleteNode(root.left, key);
}
// If the key to be deleted is greater than the root's key, then it lies in the right subtree
else if (key > root.key) {
root.right = deleteNode(root.right, key);
}
// If key is same as root's key, then this is the node to be deleted
else {
// Node with only one child or no child
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}

// Node with two children: Get the inorder successor (smallest in the right subtree)
root.key = minValue(root.right);

// Delete the inorder successor
root.right = deleteNode(root.right, root.key);
}

return root;
}

int minValue(Node root) {
int minv = root.key;
while (root.left != null) {
minv = root.left.key;
root = root.left;
}
return minv;
}
// Given a binary search tree and a key, this function deletes the key and returns the new root
Node deleteNode(Node root, int key) {
// Base case
if (root == null) {
return root;
}

// If the key to be deleted is smaller than the root's key, then it lies in the left subtree
if (key < root.key) {
root.left = deleteNode(root.left, key);
}
// If the key to be deleted is greater than the root's key, then it lies in the right subtree
else if (key > root.key) {
root.right = deleteNode(root.right, key);
}
// If key is same as root's key, then this is the node to be deleted
else {
// Node with only one child or no child
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}

// Node with two children: Get the inorder successor (smallest in the right subtree)
root.key = minValue(root.right);

// Delete the inorder successor
root.right = deleteNode(root.right, root.key);
}

return root;
}

int minValue(Node root) {
int minv = root.key;
while (root.left != null) {
minv = root.left.key;
root = root.left;
}
return minv;
}
Rag...JN 🌌 🦡 👽 💰 🐊
So if delete 50, the new root will be 60?
No description
Rag...JN 🌌 🦡 👽 💰 🐊
Aight I figured it out
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server