SlideShare a Scribd company logo
public class AVLTree> extends BST {
protected int height;
public AVLTree() {
super();
height = -1;
}
public AVLTree(BTNode root) {
super(root);
height = -1;
}
public void purge(){
super.purge();
}
public int getHeight() {
return getHeight(root);
}
private int getHeight(BTNode node) {
if(node == null)
return -1;
else
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
private AVLTree getLeftAVL() {
AVLTree leftsubtree = new AVLTree(root.left);
return leftsubtree;
}
private AVLTree getRightAVL() {
AVLTree rightsubtree = new AVLTree(root.right);
return rightsubtree;
}
protected int getBalanceFactor() {
if(isEmpty())
return 0;
else
return getRightAVL().getHeight() - getLeftAVL().getHeight();
}
public void insertAVL(T el) {
super.insert(el);
this.balance();
}
public void deleteAVL(T el) {
// to be completed by students
}
protected void balance()
{
if(!isEmpty())
{
getLeftAVL().balance();
getRightAVL().balance();
adjustHeight();
int balanceFactor = getBalanceFactor();
if(balanceFactor == -2) {
System.out.println("Balance factor = " + balanceFactor);
System.out.println("Balancing node with el: "+root.data);
if(getRightAVL().getBalanceFactor() == 0 && getLeftAVL().getBalanceFactor() == -1)
/// special case
rotateRight();
else if(getLeftAVL().getBalanceFactor() <= 0)
rotateRight();
else
rotateLeftRight();
}
else if(balanceFactor == 2) {
System.out.println("Balance factor = " + balanceFactor);
System.out.println("Balancing node with el: "+root.data);
if(getRightAVL().getBalanceFactor() == 0) /// special case that cannot be done
rotateLeft(); /// by double rotations
else if(getRightAVL().getBalanceFactor() > 0)
rotateLeft();
else
rotateRightLeft();
}
}
}
protected void adjustHeight()
{
if(isEmpty())
height = -1;
else
height = 1 + Math.max(getLeftAVL().getHeight(), getRightAVL().getHeight());
}
protected void rotateRight() {
System.out.println("RIGHT ROTATION");
// to be completed by students
}
protected void rotateLeft() {
System.out.println("LEFT ROTATION");
BTNode 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.data;
root.data = root.left.data;
root.left.data = val;
getLeftAVL().adjustHeight();
adjustHeight();
}
protected void rotateLeftRight()
{
System.out.println("Double Rotation...");
getLeftAVL().rotateLeft();
getLeftAVL().adjustHeight();
this.rotateRight();
this.adjustHeight();
}
protected void rotateRightLeft()
{
System.out.println("Double Rotation...");
// to be completed by students
}
public void levelOrderTraversal(){
levelOrderTraversal(root);
}
}
public class AVLTreeDriver {
public static void main(String[] args) {
System.out.println("nSINGLE LEFT ROTATION EXAMPLE: ");
System.out.println("Insert 45 in the following AVL tree:");
AVLTree avlTree2 = new AVLTree();
Integer[] array1 = new Integer []{30, 5, 35, 32, 40};
for(int i = 0; i < array1.length ; i++)
avlTree2.insertAVL(array1[i]);
avlTree2.printTree();
System.out.println("nInsertion result: ");
avlTree2.insertAVL(45);
System.out.println();
avlTree2.printTree();
}
} 2. Verify the following deletion by the test program:
to get the output given below: CASE 3A DELETION EXAMPLE: Delete 1 in the following
AVL tree: Root- 7 Deletion result: Balance factor =2 Balancing node with el: 2 LEFT
ROTATION Balance factor =2 Balancing node with el: 7 Double Rotation... RIGHT
ROTATION LEFT ROTATION Root- -10 Page 3 of 5

More Related Content

Similar to public class AVLTreeT extends ComparableT extends BSTT { p.pdf

Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
領一 和泉田
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
anukoolelectronics
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
DEVTYPE
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
shaktisinhgandhinaga
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdf
manjan6
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
Tomáš Kypta
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
Abdulrahman890100
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
ShiraPrater50
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
Stfalcon Meetups
 
About java
About javaAbout java
About java
Jay Xu
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
ADITIEYEWEAR
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
rambagra74
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
Here is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdfHere is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdf
arbaazrabs
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
petercoiffeur18
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
Kuldeep Jain
 

Similar to public class AVLTreeT extends ComparableT extends BSTT { p.pdf (20)

Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
 
Here is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdfHere is the code given in the instructionsclass AVL {.pdf
Here is the code given in the instructionsclass AVL {.pdf
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
 
Complete the C++ program and implement the routines that are not .docx
  Complete the C++ program and implement the routines that are not .docx  Complete the C++ program and implement the routines that are not .docx
Complete the C++ program and implement the routines that are not .docx
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
 
About java
About javaAbout java
About java
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
Here is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdfHere is the following code mentioned in the instructions.pdf
Here is the following code mentioned in the instructions.pdf
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 

More from agmobiles

Question 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdfQuestion 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdf
agmobiles
 
Question Please provide the links or the references where the info.pdf
Question  Please provide the links or the references where the info.pdfQuestion  Please provide the links or the references where the info.pdf
Question Please provide the links or the references where the info.pdf
agmobiles
 
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdfQN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
agmobiles
 
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdfQS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
agmobiles
 
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdfq60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
agmobiles
 
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdfQ4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
agmobiles
 
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdfQ1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
agmobiles
 
Python, fill in code are marked with #FillInStart and #FillInEndim.pdf
Python, fill in code are marked with #FillInStart and #FillInEndim.pdfPython, fill in code are marked with #FillInStart and #FillInEndim.pdf
Python, fill in code are marked with #FillInStart and #FillInEndim.pdf
agmobiles
 
Put this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdfPut this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdf
agmobiles
 
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdfProyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
agmobiles
 
Provide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdfProvide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdf
agmobiles
 
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdfProvide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
agmobiles
 
Prove (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdfProve (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdf
agmobiles
 
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdfProject Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
agmobiles
 
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdfProductos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
agmobiles
 
Process Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdfProcess Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdf
agmobiles
 
programming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdfprogramming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdf
agmobiles
 
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdfProblema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
agmobiles
 
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdfPreguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
agmobiles
 
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdfProblem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
agmobiles
 

More from agmobiles (20)

Question 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdfQuestion 1 (25 marks) The most recently audited Statement of Financi.pdf
Question 1 (25 marks) The most recently audited Statement of Financi.pdf
 
Question Please provide the links or the references where the info.pdf
Question  Please provide the links or the references where the info.pdfQuestion  Please provide the links or the references where the info.pdf
Question Please provide the links or the references where the info.pdf
 
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdfQN. 3A sample of 111 mortgages approved during the current year sh.pdf
QN. 3A sample of 111 mortgages approved during the current year sh.pdf
 
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdfQS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
QS 13-7 Contabilizaci�n de peque�os dividendos en acciones LO P2 A c.pdf
 
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdfq60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
q60. u anda isiz olan iki kiiyi d��n�n. Tim �almak istiyor ama i a.pdf
 
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdfQ4 (Excel spreadsheet is available). From the data given in the fo.pdf
Q4 (Excel spreadsheet is available). From the data given in the fo.pdf
 
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdfQ1 Feasibility of applying audit data analytics 1) Name a situation.pdf
Q1 Feasibility of applying audit data analytics 1) Name a situation.pdf
 
Python, fill in code are marked with #FillInStart and #FillInEndim.pdf
Python, fill in code are marked with #FillInStart and #FillInEndim.pdfPython, fill in code are marked with #FillInStart and #FillInEndim.pdf
Python, fill in code are marked with #FillInStart and #FillInEndim.pdf
 
Put this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdfPut this in APA format Add information if needed.CONSTRUCTION.pdf
Put this in APA format Add information if needed.CONSTRUCTION.pdf
 
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdfProyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
Proyecto EH! requiere una inversi�n inicial de $50 000 y tiene un va.pdf
 
Provide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdfProvide logical case studies where the application of the following .pdf
Provide logical case studies where the application of the following .pdf
 
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdfProvide excel work, spider-plot, and tornado diagram. Opportunit.pdf
Provide excel work, spider-plot, and tornado diagram. Opportunit.pdf
 
Prove (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdfProve (i.e. give a derivation for) each of the following. You can us.pdf
Prove (i.e. give a derivation for) each of the following. You can us.pdf
 
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdfProject Bird speciesThe OrdwayBirds data frame is a historical re.pdf
Project Bird speciesThe OrdwayBirds data frame is a historical re.pdf
 
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdfProductos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
Productos de ingenier�a matrimonial Cynthia Gao, gerente de adqu.pdf
 
Process Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdfProcess Cost SystemsWhat type of product or business would use a p.pdf
Process Cost SystemsWhat type of product or business would use a p.pdf
 
programming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdfprogramming assignment 2Preprocessing Before building the language.pdf
programming assignment 2Preprocessing Before building the language.pdf
 
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdfProblema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
Problema 4 Un monopolio se enfrenta a la demanda del mercado QD =.pdf
 
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdfPreguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
Preguntas para el ejercicio 34Instrucciones Para cada uno de los .pdf
 
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdfProblem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
Problem 9.06 (Preferred Stock Valuation)Farley Inc. has perpetual .pdf
 

Recently uploaded

Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 

Recently uploaded (20)

Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 

public class AVLTreeT extends ComparableT extends BSTT { p.pdf

  • 1. public class AVLTree> extends BST { protected int height; public AVLTree() { super(); height = -1; } public AVLTree(BTNode root) { super(root); height = -1; } public void purge(){ super.purge(); } public int getHeight() { return getHeight(root); } private int getHeight(BTNode node) { if(node == null) return -1; else return 1 + Math.max(getHeight(node.left), getHeight(node.right)); } private AVLTree getLeftAVL() { AVLTree leftsubtree = new AVLTree(root.left); return leftsubtree; } private AVLTree getRightAVL() { AVLTree rightsubtree = new AVLTree(root.right); return rightsubtree; } protected int getBalanceFactor() { if(isEmpty()) return 0; else return getRightAVL().getHeight() - getLeftAVL().getHeight();
  • 2. } public void insertAVL(T el) { super.insert(el); this.balance(); } public void deleteAVL(T el) { // to be completed by students } protected void balance() { if(!isEmpty()) { getLeftAVL().balance(); getRightAVL().balance(); adjustHeight(); int balanceFactor = getBalanceFactor(); if(balanceFactor == -2) { System.out.println("Balance factor = " + balanceFactor); System.out.println("Balancing node with el: "+root.data); if(getRightAVL().getBalanceFactor() == 0 && getLeftAVL().getBalanceFactor() == -1) /// special case rotateRight(); else if(getLeftAVL().getBalanceFactor() <= 0) rotateRight(); else rotateLeftRight(); } else if(balanceFactor == 2) { System.out.println("Balance factor = " + balanceFactor); System.out.println("Balancing node with el: "+root.data); if(getRightAVL().getBalanceFactor() == 0) /// special case that cannot be done rotateLeft(); /// by double rotations else if(getRightAVL().getBalanceFactor() > 0) rotateLeft(); else
  • 3. rotateRightLeft(); } } } protected void adjustHeight() { if(isEmpty()) height = -1; else height = 1 + Math.max(getLeftAVL().getHeight(), getRightAVL().getHeight()); } protected void rotateRight() { System.out.println("RIGHT ROTATION"); // to be completed by students } protected void rotateLeft() { System.out.println("LEFT ROTATION"); BTNode 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.data; root.data = root.left.data; root.left.data = val; getLeftAVL().adjustHeight(); adjustHeight(); } protected void rotateLeftRight() { System.out.println("Double Rotation..."); getLeftAVL().rotateLeft(); getLeftAVL().adjustHeight(); this.rotateRight(); this.adjustHeight(); }
  • 4. protected void rotateRightLeft() { System.out.println("Double Rotation..."); // to be completed by students } public void levelOrderTraversal(){ levelOrderTraversal(root); } } public class AVLTreeDriver { public static void main(String[] args) { System.out.println("nSINGLE LEFT ROTATION EXAMPLE: "); System.out.println("Insert 45 in the following AVL tree:"); AVLTree avlTree2 = new AVLTree(); Integer[] array1 = new Integer []{30, 5, 35, 32, 40}; for(int i = 0; i < array1.length ; i++) avlTree2.insertAVL(array1[i]); avlTree2.printTree(); System.out.println("nInsertion result: "); avlTree2.insertAVL(45); System.out.println(); avlTree2.printTree(); } } 2. Verify the following deletion by the test program: to get the output given below: CASE 3A DELETION EXAMPLE: Delete 1 in the following AVL tree: Root- 7 Deletion result: Balance factor =2 Balancing node with el: 2 LEFT ROTATION Balance factor =2 Balancing node with el: 7 Double Rotation... RIGHT ROTATION LEFT ROTATION Root- -10 Page 3 of 5