Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please conplete the recursive function \" private AVLNode Remove(AVLNode nd, Com

ID: 3862173 • Letter: P

Question

please conplete the recursive function " private AVLNode Remove(AVLNode nd, Comparable item)" in the AVL tree class, some information are given below.

8 class AVLNode 10 public comparable data; 11 public de left 12 public de righ 13 public int height; 14 15 default constructor 16e public AVLNode (Comparable value) 17 data value 18 left null 19 20 righ null 21 height 0; 22 23 24 parameterized constructor 25 public AVLNode (Comparable value de left1. AVLNode right 1) 26 data value 27 left left1. 28 29 right right1 30 heigh 0; 31 32 The ResetHeight method recomputes height if the 33 34 left or right subtrees have changed 35 void Reset Height) 36 37 int lefth eight -1; int rightheight -1; 38 39 if (left null) leftheight left. heigh 41 if (right null) rightheight righ height 42 43 heigh 1 ath. max (left height rightheight) 45 46 47 public class Tree 48 member attributes 49 private AVLNode root 50 private int size

Explanation / Answer

private AVLNode Remove(AVLNode nd, Comparable item) {

if (item==null) {
System.out.println("Node to be deleted, doesn't exist in this tree");
return null;
}
  
if (item.compareTo(nd.element) < 0 ) {
nd.left = Remove(item, nd.left);
int l = nd.left != null ? nd.left.height : 0;
  
if((nd.right != null) && (nd.right.height - l >= 2)) {
int rightHeight = nd.right.right != null ? nd.right.right.height : 0;
int leftHeight = nd.right.left != null ? nd.right.left.height : 0;
  
if(rightHeight >= leftHeight)
nd = LLBalance(nd);
else
nd = LRBalance(nd);
}
}
else if (item.compareTo(nd.element) > 0) {
nd.right = Remove(item,nd.right);
int r = nd.right != null ? nd.right.height : 0;
if((nd.left != null) && (nd.left.height - r >= 2)) {
int leftHeight = nd.left.left != null ? nd.left.left.height : 0;
int rightHeight = nd.left.right != null ? nd.left.right.height : 0;
if(leftHeight >= rightHeight)
nd = RRBalance(nd);   
else
nd = RLBalance(nd);
}
}
return nd;
}