Question: Anyone helps for comments? so explain what it is doing like this // ... public class AVLTree> extends BinarySearchTree { private static final int ALLOWED_IMBALANCE
Anyone helps for comments? so explain what it is doing like this // ...
public class AVLTree> extends BinarySearchTree { private static final int ALLOWED_IMBALANCE = 1; private boolean shouldPrintRotations; public AVLTree() { this(false); } public AVLTree(boolean shouldPrintRotations) { this.shouldPrintRotations = shouldPrintRotations; } public boolean isBalanced() { return isBalanced(root); } private boolean isBalanced(BinaryNode root) { if(root == null) { return true; } else { int leftHeight = height(root.getLeft()); int rightHeight = height(root.getRight()); return Math.abs(leftHeight - rightHeight) <= 1; } } @Override protected BinaryNode insert(T data, BinaryNode root) { return balance(super.insert(data, root)); } @Override protected BinaryNode remove(T data, BinaryNode root) { return balance(super.remove(data, root)); } private BinaryNode balance(BinaryNode root) { if(root == null) { return null; } if(height(root.getLeft()) - height(root.getRight()) > 1) { if(height(root.getLeft().getLeft()) >= height(root.getLeft().getRight())) { root = singleRightRotation(root); } else { root = doubleLeftRightRotation(root); } } else if(height(root.getRight()) - height(root.getLeft()) > 1) { if(height(root.getRight().getRight()) >= height(root.getRight().getLeft())) { root = singleLeftRotation(root); } else { root = doubleRightLeftRotation(root); } } return root; } private BinaryNode singleRightRotation(BinaryNode k2) { if(shouldPrintRotations) { System.out.println("Single Right Rotation: " + k2.getData()); } BinaryNode k1 = k2.getLeft(); k2.setLeft(k1.getRight()); k1.setRight(k2); k2.setHeight(Math.max(height(k2.getLeft()), height(k2.getRight())) + 1); k1.setHeight(Math.max(height(k1.getLeft()), k2.getHeight()) + 1); return k1; } private BinaryNode singleLeftRotation(BinaryNode k1) { if(shouldPrintRotations) { System.out.println("Single Left Rotation: " + k1.getData()); } BinaryNode k2 = k1.getRight(); k1.setRight(k2.getLeft()); k2.setLeft(k1); k1.setHeight(Math.max(height(k1.getLeft()), height(k1.getRight())) + 1); k2.setHeight(Math.max(height(k2.getRight()), k1.getHeight()) + 1); return k2; } private BinaryNode doubleLeftRightRotation(BinaryNode k3) { if(shouldPrintRotations) { System.out.println("Double Left-Right Rotation: " + k3.getData()); } k3.setLeft(singleLeftRotation(k3.getLeft())); return singleRightRotation(k3); } private BinaryNode doubleRightLeftRotation(BinaryNode k1) { if(shouldPrintRotations) { System.out.println("Double Right-Left Rotation: " + k1.getData()); } k1.setRight(singleRightRotation(k1.getRight())); return singleLeftRotation(k1); } }
----------------------------------------------------------------------------
import java.util.Comparator; import java.util.Random; public class BinarySearchTree> { protected BinaryNode root; public BinarySearchTree() { root = null; } public void empty() { root = null; } public boolean isEmpty(){ return root == null; } public BinaryNode getRoot(){ return root; } public int height() { return height(root); } protected int height(BinaryNode root) { if(root == null) { return -1; } else { return Math.max(height(root.getLeft()), height(root.getRight())) + 1; } } public boolean contains(T data){ return contains(data, root); } protected boolean contains(T data, BinaryNode root){ if(root == null){ return false; } int compareResult = root.getData().compareTo(data); if(compareResult > 0){ return contains(data, root.getLeft()); } else if(compareResult < 0){ return contains(data, root.getRight()); } else{ return true; } } public T findMin() { BinaryNode node = findMin(root); return node == null ? null : node.getData(); } protected BinaryNode findMin(BinaryNode root) { if(root == null) { return null; } else if(root.getLeft() == null) { return root; } else { return findMin(root.getLeft()); } } public T findMax() { BinaryNode node = findMax(root); return node == null ? null : node.getData(); } protected BinaryNode findMax(BinaryNode root) { if(root == null) { return null; } else if(root.getRight() == null) { return root; } else { return findMin(root.getRight()); } } public void insert(T data) { root = insert(data, root); } protected BinaryNode insert(T data, BinaryNode root) { if(root == null) { return new BinaryNode<>(data); } int compareResult = root.getData().compareTo(data); if(compareResult > 0) { root.setLeft(insert(data, root.getLeft())); } else if(compareResult < 0) { root.setRight(insert(data, root.getRight())); } return root; } public void remove(T data) { root = remove(data, root); } protected BinaryNode remove(T data, BinaryNode root) { if(root == null) { return null; } int compareResult = root.getData().compareTo(data); if(compareResult > 0) { root.setLeft(remove(data, root.getLeft())); } else if(compareResult < 0) { root.setRight(remove(data, root.getRight())); } else if(root.getLeft() != null && root.getRight() != null) { root.setData(findMin(root.getRight()).getData()); root.setRight(remove(root.getData(), root.getRight())); } else { root = root.getLeft() != null ? root.getLeft() : root.getRight(); } return root; } public void printTree() { printTree(root); } protected void printTree(BinaryNode root) { if(root != null) { printTree(root.getLeft()); System.out.println(root.getData()); printTree(root.getRight()); } } }
---------------------------------------------------------------
public class BinaryNode> { private T data; private int height; private BinaryNode left; private BinaryNode right; public BinaryNode(T data) { this(data, null, null); } public BinaryNode(T data, BinaryNode left, BinaryNode right) { this.data = data; this.left = left; this.right = right; this.height = 0; } public T getData() { return data; } public void setData(T data) { this.data = data; } public BinaryNode getLeft() { return left; } public void setLeft(BinaryNode left) { this.left = left; } public BinaryNode getRight() { return right; } public void setRight(BinaryNode right) { this.right = right; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
