With this code I'm having trouble writing the "remove" function for the AVL Tree utilizing the below Binary Node Class I created (BinNode). How would  I get the "remove" method to work? Or how would I replace it? @Override public void remove(String data) {this.setRoot(remove(this.getRoot(), data));} I keep getting an error and not sure why or how to fix. Would it be possible to help with recreating the AVL Remove Function or fix this issue?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

With this code I'm having trouble writing the "remove" function for the AVL Tree utilizing the below Binary Node Class I created (BinNode). How would  I get the "remove" method to work? Or how would I replace it?

@Override
public void remove(String data) {this.setRoot(remove(this.getRoot(), data));}

I keep getting an error and not sure why or how to fix. Would it be possible to help with recreating the AVL Remove Function or fix this issue?

Binary Node Class

public class BinNode
{
private String data;
private BinNode left;
private BinNode right;

private int height; //height field as with AVL tree or Red Black Tree

private boolean color; //Needed for Red Black Tree


public BinNode(){
data = "";
left = null;
right = null;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
public BinNode(String d){
data = d;
left = null;
right = null;
}

public void setData(String d){
this.data = d;
}
public String getData(){
return this.data;
}
public void setLeft(BinNode l){
this.left = l;
}
public BinNode getLeft(){
return this.left;
}
public void setRight(BinNode r){
this.right = r;
}
public BinNode getRight(){
return this.right;
}
}

 

AVL Tree I built

public class AVLTree extends BinaryTree{
@Override
public void insert(String data) {
this.setRoot(insert(this.getRoot(), data));
}

private BinNode insert(BinNode node, String data) {
if (node == null) {
return new BinNode(data);
} else {
int i = node.getData().compareTo(data);

if (i < 0) {
node.setRight(insert(node.getRight(), data));
} else if (i >= 0) {
node.setLeft(insert(node.getLeft(), data));
}

// Update the height of the current node
int leftHeight = height(node.getLeft());
int rightHeight = height(node.getRight());
node.setHeight(1 + Math.max(leftHeight, rightHeight));

int balance = getBalance(node);

if (balance > 1) {
if (data.compareTo(node.getLeft().getData()) < 0) {

return rightRotate(node);
} else {
// Left-Right case
node.setLeft(leftRotate(node.getLeft()));
return rightRotate(node);
}
}
if (balance < -1) {
if (data.compareTo(node.getRight().getData()) > 0) {
// Right-Right case
return leftRotate(node);
} else {
// Right-Left case
node.setRight(rightRotate(node.getRight()));
return leftRotate(node);
}
}

return node;
}
}

private int height(BinNode node) {
if (node == null) {
return 0;
}
return node.getHeight();
}

private int getBalance(BinNode node) {
if (node == null) {
return 0;
}
return height(node.getLeft()) - height(node.getRight());
}

private BinNode rightRotate(BinNode y) {
BinNode x = y.getLeft();
BinNode T2 = x.getRight();

// Perform rotation
x.setRight(y);
y.setLeft(T2);

y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);
x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);

return x;
}

private BinNode leftRotate(BinNode x) {
BinNode y = x.getRight();
BinNode T2 = y.getLeft();

// Perform rotation
y.setLeft(x);
x.setRight(T2);


x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);
y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);

return y;
}


/* Don't know why this doesn't work
@Override
public void remove(String data) {this.setRoot(remove(this.getRoot(), data));}
*/

public BinNode remove(BinNode root, String data) {
// Step 1 - Perform standard BST delete
if (root == null) {
return root;
}

int cmp = data.compareTo(root.getData());

if (cmp < 0) {
root.setLeft(remove(root.getLeft(), data));
} else if (cmp > 0) {
root.setRight(remove(root.getRight(), data));
} else {
// node with only one child or no child
if ((root.getLeft() == null) || (root.getRight() == null)) {
BinNode temp = null;
if (temp == root.getLeft()) {
temp = root.getRight();
} else {
temp = root.getLeft();
}

// No child case
if (temp == null) {
temp = root;
root = null;
} else { // One child case
root = temp; // Copy the contents of the non-empty child
}
} else {
// node with two children: get the inorder successor (smallest in the right subtree)
BinNode temp = minValueNode(root.getRight());

// copy the inorder successor's data to this node
root.setData(temp.getData());


root.setRight(remove(root.getRight(), temp.getData()));
}
}

// if the tree had only one node then return
if (root == null) {
return root;
}

// Step 2 - Update the height of the current node
root.setHeight(Math.max(height(root.getLeft()), height(root.getRight())) + 1);

// Step 3 - Get the balance factor
int balance = getBalance(root);

// Step 4 - If the node is unbalanced, then try out the 4 cases
// Left Left Case
if (balance > 1 && getBalance(root.getLeft()) >= 0) {
return rightRotate(root);
}

// Right Right Case
if (balance < -1 && getBalance(root.getRight()) <= 0) {
return leftRotate(root);
}

// Left Right Case
if (balance > 1 && getBalance(root.getLeft()) < 0) {
root.setLeft(leftRotate(root.getLeft()));
return rightRotate(root);
}

// Right Left Case
if (balance < -1 && getBalance(root.getRight()) > 0) {
root.setRight(rightRotate(root.getRight()));
return leftRotate(root);
}

return root;
}

private BinNode minValueNode(BinNode node) {
BinNode current = node;

/* loop down to find the leftmost leaf */
while (current.getLeft() != null) {
current = current.getLeft();
}

return current;
}


}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education