Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help find mistake in my avl code for remove. Predecessor is used for remove. public T remove(T data) { if (data == null) {

Please help find mistake in my avl code for remove. Predecessor is used for remove.

public T remove(T data) {

if (data == null) {

throw new IllegalArgumentException("Cannot remove null data");

}

AVLNode node = new AVLNode<>(null);

remove2(root,node,data);

return node.getData();

}

private AVLNode remove2(AVLNode initial,AVLNode store, T data) {

if (initial == null) {

throw new java.util.NoSuchElementException("Data not found");

}

if (data.compareTo(initial.getData()) > 0) {

initial.setRight(remove2(initial.getRight(),store,data));

} else if (data.compareTo(initial.getData()) < 0) {

initial.setLeft(remove2(initial.getLeft(),store,data));

}

else {

T thing = initial.getData();

store.setData(thing);

size--;

if (initial.getLeft() != null && initial.getRight() != null) {

initial.setLeft(pred(initial.getLeft(), store));

initial.setData(store.getData());

} else if (initial.getLeft() == null) {

return initial.getRight();

} else if (initial.getRight() == null) {

return initial.getLeft();

} else {

return null;

}

}

balance(initial);

return rotate(initial);

}

private AVLNode pred(AVLNode initial, AVLNode store) {

if (initial.getLeft() == null) {

store.setData(initial.getData());

return initial.getRight();

}

initial.setLeft(pred(initial.getLeft(), store));

balance(initial);

return rotate(initial);

}

private void balance(AVLNode node) { int left; int right; if (node != null) { if (node.getLeft() == null) { left = -1; } else { left = node.getLeft().getHeight(); } if (node.getRight() == null) { right = -1; } else { right = node.getRight().getHeight(); } node.setHeight(Math.max(left, right) + 1); node.setBalanceFactor(left - right); } } private AVLNode rotate(AVLNode node) { if (node != null) { if (node.getBalanceFactor() < -1) { if (node.getRight().getBalanceFactor() <= 0) { return left(node); } if (node.getRight().getBalanceFactor() > 0) { return rightLeft(node); } } if (node.getBalanceFactor() > 1) { if (node.getLeft().getBalanceFactor() >= 0) { return right(node); } if (node.getLeft().getBalanceFactor() < 0) { return leftRight(node); } } } return node; } private AVLNode left(AVLNode node) { AVLNode probe = node.getRight(); node.setRight(probe.getLeft()); probe.setLeft(node); balance(node); balance(probe); return probe; } private AVLNode leftRight(AVLNode node) { node.setLeft((left(node.getLeft()))); return right(node); } private AVLNode right(AVLNode node) { AVLNode probe = node.getLeft(); node.setLeft((probe.getRight())); probe.setRight(node); balance(node); balance(probe); return probe; } private AVLNode rightLeft(AVLNode node) { node.setRight(right(node.getRight())); return left(node); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions

Question

l Describe what a union is and explain why employees join unions.

Answered: 1 week ago