-
-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the class that provides AVL tree delete method has been added #40
Open
bakhodir10
wants to merge
2
commits into
sherxon:master
Choose a base branch
from
bakhodir10:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package ds; | ||
|
||
public class DeleteNodeInAVLTree<E extends Comparable<E>> { | ||
private AVLNode<E> root; | ||
|
||
private int height(AVLNode<E> node) { | ||
return node == null ? -1 : node.height; | ||
} | ||
|
||
private int max(int a, int b) { | ||
return a > b ? a : b; | ||
} | ||
|
||
private int getBalance(AVLNode<E> node) { | ||
return node == null ? 0 : height(node.left) - height(node.right); | ||
} | ||
|
||
private AVLNode<E> leftRotate(AVLNode<E> x) { | ||
AVLNode<E> y = x.right; | ||
AVLNode<E> z = y.left; | ||
|
||
// rotation | ||
y.left = x; | ||
x.right = z; | ||
|
||
// update heights | ||
x.height = max(height(x.left), height(x.right)) + 1; | ||
y.height = max(height(y.left), height(y.right)) + 1; | ||
return x; | ||
} | ||
|
||
private AVLNode<E> rightRotate(AVLNode<E> y) { | ||
AVLNode<E> x = y.left; | ||
AVLNode<E> z = x.right; | ||
|
||
//rotation | ||
x.right = y; | ||
y.left = z; | ||
|
||
// update heights | ||
x.height = max(height(x.left), height(x.right)) + 1; | ||
y.height = max(height(y.left), height(y.right)) + 1; | ||
return x; | ||
} | ||
|
||
public void delete(E elem) { | ||
if (elem == null) throw new IllegalArgumentException(); | ||
root = deleteHelper(root, elem); | ||
} | ||
|
||
private AVLNode<E> deleteHelper(AVLNode<E> node, E elem) { | ||
if (node.elem.compareTo(elem) > 0) node.left = deleteHelper(node.left, elem); | ||
else if (node.elem.compareTo(elem) < 0) node.right = deleteHelper(node.right, elem); | ||
else { // found, now let's remove it | ||
if (node.left == null && node.right == null) { | ||
node = null; // case 1: if the node has no child | ||
} else if (node.left == null || node.right == null) { | ||
if (node.left != null) node = node.left; // case 2: if the node has only one child | ||
else node = node.right; | ||
} else { // case 3: if the node has two child | ||
AVLNode<E> p = findMin(node.left); | ||
node.elem = p.elem; | ||
node.left = deleteHelper(node.left, p.elem); | ||
} | ||
} | ||
|
||
if (node == null) return null; | ||
node.height = max(height(node.left), height(node.right)) + 1; | ||
int balance = getBalance(node); | ||
|
||
// Left Left Case | ||
if (balance > 1 && getBalance(root.left) >= 0) | ||
return rightRotate(root); | ||
|
||
// Left Right Case | ||
if (balance > 1 && getBalance(root.left) < 0) { | ||
root.left = leftRotate(root.left); | ||
return rightRotate(root); | ||
} | ||
|
||
// Right Right Case | ||
if (balance < -1 && getBalance(root.right) <= 0) | ||
return leftRotate(root); | ||
|
||
// Right Left Case | ||
if (balance < -1 && getBalance(root.right) > 0) { | ||
root.right = rightRotate(root.right); | ||
return leftRotate(root); | ||
} | ||
return node; | ||
} | ||
|
||
private AVLNode<E> findMin(AVLNode<E> node) { | ||
while (node.right != null) { | ||
node = node.right; | ||
} | ||
return node; | ||
} | ||
|
||
private class AVLNode<E> { | ||
private int height; | ||
private E elem; | ||
private AVLNode<E> left, right; | ||
|
||
public AVLNode(E elem) { | ||
this.elem = elem; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why didn't you use Math.max() instead of creating this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I should have used Math.max() method. it would be less code ))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bakhodirsbox Did you test all cases? if it is passing i will merge and close this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sherxon It looked correct after then I have found some errors, let me fix it. I think I can pull request after making sure it works correctly for every test