Question
2. AVL Tree a. Add the following implementations to AVLTree.java i. balance (BinaryNode t): add the code for balancing the tree for the 4 cases
2. AVL Tree a. Add the following implementations to AVLTree.java i. balance (BinaryNode
package assignment2;
/** * Implements a self-balancing AVL Tree. * * @author CSC330 * @param
public AVLTree() { super(); this.BALANCE_FACTOR = 1; }
private final int BALANCE_FACTOR;
/** * * @param root The root of the BST * @return The balanced tree. */ private BinaryNode
return root; }
/** * Rotate binary tree node with left child. For AVL trees, this is a single * rotation for case 1. */ private BinaryNode
return k1; }
/** * Rotate binary tree node with right child. For AVL trees, this is a single * rotation for case 4. */ private BinaryNode
BinaryNode
return k2; }
/** * Double rotate binary tree node: first left child with its right child; * then node k3 with new left child. For AVL trees, this is a double * rotation for case 2. */ private BinaryNode
k3.left = rotateLeftWithRightChild(k3.left);
return rotateRightWithLeftChild(k3); }
/** * Double rotate binary tree node: first right child with its left child; * then node k1 with new right child. For AVL trees, this is a double * rotation for case 3. */ private BinaryNode
k1.right = rotateRightWithLeftChild(k1.right);
return rotateLeftWithRightChild(k1); }
/** * Insert into the tree; duplicates are ignored. * * @param x the item to insert. */ @Override public void insert(AnyType x) { root = insert(x, root); }
/** * Internal method to insert into a subtree. * * @param x the item to insert. * @param root the node that roots the subtree. * @return the new root of the subtree. */ @Override protected BinaryNode
/** * Remove from the tree. Nothing is done if x is not found. * * @param x the item to remove. */ @Override public void remove(AnyType x) { root = remove(x, root); }
/** * Internal method to remove from a subtree. * * @param x the item to remove. * @param root the node that roots the subtree. * @return the new root of the subtree. */ @Override protected BinaryNode
return balance(super.remove(x, root)); }
public void checkBalance() { checkBalance(root); }
private int checkBalance(BinaryNode
return height(root); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started