Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/ds/DeleteNodeInAVLTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package ds;

@SuppressWarnings("Duplicates")
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) {
Copy link
Owner

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

Copy link
Author

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 ))

Copy link
Owner

@sherxon sherxon Feb 2, 2018

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

Copy link
Author

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

return a > b ? a : b;
}

private int getBalance(AVLNode<E> node) {
return node == null ? 0 : height(node.left) - height(node.right);
}

public void delete(E elem) {
if (elem == null || root == null) throw new IllegalArgumentException();
root = deleteHelper(root, elem);
}

private AVLNode<E> deleteHelper(AVLNode<E> node, E elem) {
// usual delete node from tree
if (node.elem.compareTo(elem) > 0) node.left = deleteHelper(node.left, elem);
if (node.elem.compareTo(elem) < 0) node.right = deleteHelper(node.right, elem);
else {
if (node.left == null && node.right == null) { // case 1: the node has no child
node = null;
} else if (node.left == null || node.right == null) { // case 2: the node has a child
if (node.left != null) node = node.left;
else node = node.right;
} else { // case 3: the node has two child
AVLNode<E> p = node.left;
if (p.right != null) {
while (p.right.right != null) p = p.right;
node.elem = p.right.elem;
p.right = null;
} else {
p.right = node.right;
node = p;
}
node.height = max(height(node.left), height(node.right)) + 1;
int balance = getBalance(node);

// if the node is not balanced
// left-left case
if (balance > -1 && getBalance(node.left) >= 0) {
return rightRotate(node);
}
// left-right case
if (balance > -1 && getBalance(node.left) < 0) {
node.left = leftRotate(node);
return rightRotate(node);
}
// right-right case
if (balance < -1 && getBalance(node.right) <= 0) {
return leftRotate(node);
}
// right-left case
if (balance < -1 && getBalance(node.right) > -1) {
node.right = rightRotate(node);
return leftRotate(node);
}
}
}
return node;
}

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;
}

private class AVLNode<E> {
private int height;
private E elem;
private AVLNode<E> left, right;

public AVLNode(E elem) {
this.elem = elem;
}
}
}