Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class AVLTree > extends BST { protected int height; public AVLTree ( ) { super ( ) ; height = - 1 ; }
public class AVLTree extends BST
protected int height;
public AVLTree
super;
height ;
public AVLTreeBSTNode root
superroot;
height ;
public int getHeight
return getHeightroot;
private int getHeightBSTNode node
ifnode null
return ;
else
return Math.maxgetHeightnodeleft getHeightnoderight;
private AVLTree getLeftAVL
AVLTree leftsubtree new AVLTreerootleft;
return leftsubtree;
private AVLTree getRightAVL
AVLTree rightsubtree new AVLTreerootright;
return rightsubtree;
protected int getBalanceFactor
ifisEmpty
return ;
else
return getRightAVLgetHeight getLeftAVLgetHeight;
public void insertAVLT el
super.insertel;
this.balance;
public void deleteAVLT el
Q
protected void balance
ifisEmpty
getLeftAVLbalance;
getRightAVLbalance;
adjustHeight;
int balanceFactor getBalanceFactor;
ifbalanceFactor
System.out.printlnBalancing node with el: root.el;
ifgetLeftAVLgetBalanceFactor
rotateRight;
else
rotateLeftRight;
else ifbalanceFactor
System.out.printlnBalancing node with el: root.el;
ifgetRightAVLgetBalanceFactor
rotateLeft;
else
rotateRightLeft;
protected void adjustHeight
ifisEmpty
height ;
else
height Math.maxgetLeftAVLgetHeight getRightAVLgetHeight;
protected void rotateRight
System.out.printlnRIGHT ROTATION";
Q
protected void rotateLeft
System.out.printlnLEFT ROTATION";
BSTNode tempNode root.left;
root.left root.right;
root.right root.left.right;
root.left.right root.left.left;
root.left.left tempNode;
T val T root.el;
root.el root.left.el;
root.left.el val;
getLeftAVLadjustHeight;
adjustHeight;
protected void rotateLeftRight
System.out.printlnDouble Rotation...";
Q
protected void rotateRightLeft
System.out.printlnDouble Rotation...";
getRightAVLrotateRight;
getRightAVLadjustHeight;
this.rotateLeft;
this.adjustHeight;
Objectives
The objective of this lab is to design, implement and use AVL trees.
Outcomes
After completing this Lab, students are expected to:
Design classes for AVL trees.
Delete from AVL trees.
Notes
For the purpose of this lab, you may download the attached programs.
Lab Exercises
Complete the class AVLTree that extends BST It should have four methods RotateLeft,
RotateRight, RotateLeftRight, and RotateRightLeft. You have to provide the methods
rotateRight and rotateRightLeft. Provide the method deleteAVLT el to delete
elements in the AVL tree. Note that the BST class for AVL trees has been modified to have
an extra constructor.
Create an AVL tree in which the following keys are inserted in the given order:
Print the resulting AVL tree in BFS Your BFS result should correspond to the following
AVL Tree:
Now ask the user to provide any elements to delete. After deleting the elements, print the
BFS of the AVL tree again.
Create an AVL tree of strings. Test your program on the given text file sampletextfiletxt
After creating the AVLTree of strings, print the AVLTree using inorder traversal. Note:
Do not insert duplicate words in your AVL tree Stack Queue and BST BSTnode are provided
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