Question: add public E find ( int K ) to the AVLTree code / / ik the code isnt completed, it doesnt let me add all
add public E findint K to the AVLTree codeik the code isnt completed, it doesnt let me add all of it public class AVLTree extends BST
Create an empty AVL tree
public AVLTree
public AVLTreeEobjects
superobjects;
@Override Override createNewNode to create an AVLTreeNode
protected AVLTreeNode createNewNodeE e
return new AVLTreeNodee;
@Override Insert an element and rebalance if necessary
public boolean insertE e
boolean successful super.inserte;
if successful
return false; e is already in the tree
else
balancePathe;
return true;
private void updateHeightAVLTreeNode node
ifnodeleft null && node.right null
node.height ;
else if nodeleft null
node.height AVLTreeNodenoderightheight;
else if noderight null
node.height AVLTreeNodenodeleftheight;
else
node.height
Math.maxAVLTreeNodenoderightheight,
AVLTreeNodenodeleftheight;
private void balancePathE e
java.util.ArrayList path pathe;
for int i path.size; i ; i
AVLTreeNode A AVLTreeNodepathgeti;
updateHeightA;
AVLTreeNode parentOfA A root null :
AVLTreeNodepathgeti ;
switch balanceFactorA
case :
if balanceFactorAVLTreeNodeAleft
balanceLLA parentOfA;
else
balanceLRA parentOfA;
break;
case :
if balanceFactorAVLTreeNodeAright
balanceRRA parentOfA; Perform RR rotation
else
balanceRLA parentOfA;
private int balanceFactorAVLTreeNode node
if noderight null node has no right subtree
return node.height;
else if nodeleft null node has no left subtree
return node.height;
else
return AVLTreeNodenoderightheight
AVLTreeNodenodeleftheight;
Balance LL see Figure
private void balanceLLTreeNode A TreeNode parentOfA
TreeNode B Aleft;
ifA root
root B;
else
if parentOfAleft A
parentOfA.left B;
else
parentOfA.right B;
Aleft Bright; Make T the left subtree of A
Bright A; Make A the left child of B
updateHeightAVLTreeNodeA;
updateHeightAVLTreeNodeB;
private void balanceLRTreeNode A TreeNode parentOfA
TreeNode B Aleft; A is leftheavy
TreeNode C Bright; B is rightheavy
if A root
root C;
else
if parentOfAleft A
parentOfA.left C;
else
parentOfA.right C;
Aleft Cright; Make T the left subtree of A
Bright Cleft; Make T the right subtree of B
Cleft B;
Cright A;
Adjust heights
updateHeightAVLTreeNodeA;
updateHeightAVLTreeNodeB;
updateHeightAVLTreeNodeC;
Balance RR see Figure
private void balanceRRTreeNode A TreeNode parentOfA
TreeNode B Aright; A is rightheavy and B is rightheavy
if A root
root B;
else
if parentOfAleft A
parentOfA.left B;
else
parentOfA.right B;
Aright Bleft; Make T the right subtree of A
Bleft A;
updateHeightAVLTreeNodeA;
updateHeightAVLTreeNodeB;
private void balanceRLTreeNode A TreeNode parentOfA
TreeNode B Aright; A is rightheavy
TreeNode C Bleft; B is leftheavy
if A root
root C;
else
if parentOfAleft A
parentOfA.left C;
else
parentOfA.right C;
Aright Cleft; Make T the right subtree of A
Bleft Cright; Make T the left subtree of B
Cleft A;
Cright B;
updateHeightAVLTreeNodeA;
updateHeightAVLTreeNodeB;
updateHeightAVLTreeNodeC;
@Override Delete an element from the AVL tree.
Return true if the element is deleted successfully
Return false if the element is not in the tree
public boolean deleteE element
if root null
return false; Element is not in the tree
Locate the node to be deleted and also locate its parent node
TreeNode parent null;
TreeNode current root;
while current null
if elementcompareTocurrentelement
parent current;
current current.left;
else if elementcompareTocurrentelement
parent current;
current current.ri
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
