SlideShare a Scribd company logo
I am working on making an AVL tree in Java, I need help finishing my roatation methods,
rightRotate() is not working so I have not yet attempted rotateLeft();
public class MyAVLNode {
private MyAVLNode left, right, parent;
private int element, height;
public MyAVLNode getRight() {
return right;
}
public void setRight(MyAVLNode right) {
this.right = right;
}
public MyAVLNode getLeft() {
return left;
}
public void setLeft(MyAVLNode left) {
this.left = left;
}
public MyAVLNode getParent() {
return parent;
}
public void setParent(MyAVLNode parent) {
this.parent = parent;
}
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
public class MyAVLTree {
private MyAVLNode root;
public MyAVLTree() {
root = null;
}
private MyAVLNode createNode(int element) {
MyAVLNode temp = new MyAVLNode();
temp.setElement(element);
temp.setLeft(null);
temp.setRight(null);
temp.setParent(null);
temp.setHeight(0);
return temp;
}
private void inorder(MyAVLNode current) {
if(current!=null) {
inorder(current.getLeft());
System.out.println(current.getElement() + " "+current.getHeight());
inorder(current.getRight());
}
}
public void print() {
inorder(root);
}
private int getBalanceFactor(MyAVLNode current) {
int leftChildHeight = current.getLeft()==null ? -1 : current.getLeft().getHeight();
int rightChildHeight = current.getRight()==null ? -1 : current.getRight().getHeight();
return rightChildHeight - leftChildHeight;
}
private void rightRotate(MyAVLNode current) {
MyAVLNode oldParent = current.getParent();
MyAVLNode grandparent = oldParent.getParent();
if(grandparent==null) {
root = current;
} else {
if(grandparent.getElement() < current.getElement()) {
grandparent.setRight(current);
} else {
grandparent.setLeft(current);
}
current.setParent(grandparent);
oldParent.setLeft(current.getRight());
current.getRight().setParent(oldParent);
current.setRight(oldParent);
oldParent.setParent(current);
}
}
private void leftRotate(MyTreeNode current){
}
private void updateHeight(MyAVLNode current) {
if(current!=null) {
int leftChildHeight = current.getLeft()==null ? -1 : current.getLeft().getHeight();
int rightChildHeight = current.getRight()==null ? -1 : current.getRight().getHeight();
//check if current is balanced
if(Math.abs(rightChildHeight-leftChildHeight)>1) {
//unbalanced, fix
if(getBalanceFactor(current)==-2) {
//left case
if(getBalanceFactor(current.getLeft())==-1) {
//left left case
rightRotate(current.getLeft());
} else {
//left right case
}
} else {
//right case
}
} else {
//if it is balanced, update heights
current.setHeight(Math.max(leftChildHeight, rightChildHeight)+1);
updateHeight(current.getParent());
}
}
}
private void insert(MyAVLNode current, MyAVLNode insertMe) {
if(current.getElement()<insertMe.getElement()) {
//look to the right
if(current.getRight()==null) {
//structure things
current.setRight(insertMe);
insertMe.setParent(current);
//update heights, balance factors, parents?
updateHeight(current);
} else {
insert(current.getRight(), insertMe);
}
} else{
//look to the left
if(current.getLeft()==null) {
//structure things
current.setLeft(insertMe);
insertMe.setParent(current);
//update heights, balance factors, parents?
updateHeight(current);
} else {
insert(current.getLeft(), insertMe);
}
}
}
public void insert(int element) {
MyAVLNode temp = createNode(element);
if(root==null) {
root = temp;
} else {
insert(root, temp);
}
}
}

More Related Content

More from abithmobiles

frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdffrcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
abithmobiles
 
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard .pdf
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard  .pdfAplicar MOT Framework al iPhone en caso IVK Caso Harvard  .pdf
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard .pdf
abithmobiles
 
1 Private consumption C in a small closed economy can be des.pdf
1 Private consumption C in a small closed economy can be des.pdf1 Private consumption C in a small closed economy can be des.pdf
1 Private consumption C in a small closed economy can be des.pdf
abithmobiles
 
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdfwaine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
abithmobiles
 
Use one paragraph to answer the question of which of the two.pdf
Use one paragraph to answer the question of which of the two.pdfUse one paragraph to answer the question of which of the two.pdf
Use one paragraph to answer the question of which of the two.pdf
abithmobiles
 
This is a follow up question to my unity agents farmer hors.pdf
This is a follow up question to my unity agents farmer hors.pdfThis is a follow up question to my unity agents farmer hors.pdf
This is a follow up question to my unity agents farmer hors.pdf
abithmobiles
 
Dunphy Company issued 16000 of 7510year bonds at par v.pdf
Dunphy Company issued 16000 of 7510year bonds at par v.pdfDunphy Company issued 16000 of 7510year bonds at par v.pdf
Dunphy Company issued 16000 of 7510year bonds at par v.pdf
abithmobiles
 
67Divya a cybersecurity analyst has been noticing more in.pdf
67Divya a cybersecurity analyst has been noticing more in.pdf67Divya a cybersecurity analyst has been noticing more in.pdf
67Divya a cybersecurity analyst has been noticing more in.pdf
abithmobiles
 
4 Multiple choice On the Internet application protocols p.pdf
4  Multiple choice On the Internet application protocols p.pdf4  Multiple choice On the Internet application protocols p.pdf
4 Multiple choice On the Internet application protocols p.pdf
abithmobiles
 
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
abithmobiles
 
Airqual Test Corporation provides onsite air quialify testi.pdf
Airqual Test Corporation provides onsite air quialify testi.pdfAirqual Test Corporation provides onsite air quialify testi.pdf
Airqual Test Corporation provides onsite air quialify testi.pdf
abithmobiles
 
The Mars company says that before the introduction of purple.pdf
The Mars company says that before the introduction of purple.pdfThe Mars company says that before the introduction of purple.pdf
The Mars company says that before the introduction of purple.pdf
abithmobiles
 
The partial worksheet for the Jamison Company showed the fol.pdf
The partial worksheet for the Jamison Company showed the fol.pdfThe partial worksheet for the Jamison Company showed the fol.pdf
The partial worksheet for the Jamison Company showed the fol.pdf
abithmobiles
 
Reference Question In an early meeting with your customer t.pdf
Reference Question In an early meeting with your customer t.pdfReference Question In an early meeting with your customer t.pdf
Reference Question In an early meeting with your customer t.pdf
abithmobiles
 
Send answers please Thanks 27 The ANS generally exhibits .pdf
Send answers please Thanks 27 The ANS generally exhibits .pdfSend answers please Thanks 27 The ANS generally exhibits .pdf
Send answers please Thanks 27 The ANS generally exhibits .pdf
abithmobiles
 
Suppose a byteaddressable memory contains 2MB and cache con.pdf
Suppose a byteaddressable memory contains 2MB and cache con.pdfSuppose a byteaddressable memory contains 2MB and cache con.pdf
Suppose a byteaddressable memory contains 2MB and cache con.pdf
abithmobiles
 
Starting with the SIR model with births and deaths where i.pdf
Starting with the SIR model with births and deaths where  i.pdfStarting with the SIR model with births and deaths where  i.pdf
Starting with the SIR model with births and deaths where i.pdf
abithmobiles
 
QUESTION 23 focus on a single product or service and have th.pdf
QUESTION 23 focus on a single product or service and have th.pdfQUESTION 23 focus on a single product or service and have th.pdf
QUESTION 23 focus on a single product or service and have th.pdf
abithmobiles
 
begintabularrrl hline multicolumn1r P23 a.pdf
begintabularrrl hline multicolumn1r P23  a.pdfbegintabularrrl hline multicolumn1r P23  a.pdf
begintabularrrl hline multicolumn1r P23 a.pdf
abithmobiles
 
Marin Supplies Company sells many different widgets and uses.pdf
Marin Supplies Company sells many different widgets and uses.pdfMarin Supplies Company sells many different widgets and uses.pdf
Marin Supplies Company sells many different widgets and uses.pdf
abithmobiles
 

More from abithmobiles (20)

frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdffrcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
frcantrer bi borientes eitasstion 1 th medis de be dodezesi.pdf
 
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard .pdf
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard  .pdfAplicar MOT Framework al iPhone en caso IVK Caso Harvard  .pdf
Aplicar MOT Framework al iPhone en caso IVK Caso Harvard .pdf
 
1 Private consumption C in a small closed economy can be des.pdf
1 Private consumption C in a small closed economy can be des.pdf1 Private consumption C in a small closed economy can be des.pdf
1 Private consumption C in a small closed economy can be des.pdf
 
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdfwaine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
waine the NPV or the preiont Mr 45N of saiea Pius Deprec.pdf
 
Use one paragraph to answer the question of which of the two.pdf
Use one paragraph to answer the question of which of the two.pdfUse one paragraph to answer the question of which of the two.pdf
Use one paragraph to answer the question of which of the two.pdf
 
This is a follow up question to my unity agents farmer hors.pdf
This is a follow up question to my unity agents farmer hors.pdfThis is a follow up question to my unity agents farmer hors.pdf
This is a follow up question to my unity agents farmer hors.pdf
 
Dunphy Company issued 16000 of 7510year bonds at par v.pdf
Dunphy Company issued 16000 of 7510year bonds at par v.pdfDunphy Company issued 16000 of 7510year bonds at par v.pdf
Dunphy Company issued 16000 of 7510year bonds at par v.pdf
 
67Divya a cybersecurity analyst has been noticing more in.pdf
67Divya a cybersecurity analyst has been noticing more in.pdf67Divya a cybersecurity analyst has been noticing more in.pdf
67Divya a cybersecurity analyst has been noticing more in.pdf
 
4 Multiple choice On the Internet application protocols p.pdf
4  Multiple choice On the Internet application protocols p.pdf4  Multiple choice On the Internet application protocols p.pdf
4 Multiple choice On the Internet application protocols p.pdf
 
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
5 Suppose Xi Normal 0i21 where i129 and X1X2X9.pdf
 
Airqual Test Corporation provides onsite air quialify testi.pdf
Airqual Test Corporation provides onsite air quialify testi.pdfAirqual Test Corporation provides onsite air quialify testi.pdf
Airqual Test Corporation provides onsite air quialify testi.pdf
 
The Mars company says that before the introduction of purple.pdf
The Mars company says that before the introduction of purple.pdfThe Mars company says that before the introduction of purple.pdf
The Mars company says that before the introduction of purple.pdf
 
The partial worksheet for the Jamison Company showed the fol.pdf
The partial worksheet for the Jamison Company showed the fol.pdfThe partial worksheet for the Jamison Company showed the fol.pdf
The partial worksheet for the Jamison Company showed the fol.pdf
 
Reference Question In an early meeting with your customer t.pdf
Reference Question In an early meeting with your customer t.pdfReference Question In an early meeting with your customer t.pdf
Reference Question In an early meeting with your customer t.pdf
 
Send answers please Thanks 27 The ANS generally exhibits .pdf
Send answers please Thanks 27 The ANS generally exhibits .pdfSend answers please Thanks 27 The ANS generally exhibits .pdf
Send answers please Thanks 27 The ANS generally exhibits .pdf
 
Suppose a byteaddressable memory contains 2MB and cache con.pdf
Suppose a byteaddressable memory contains 2MB and cache con.pdfSuppose a byteaddressable memory contains 2MB and cache con.pdf
Suppose a byteaddressable memory contains 2MB and cache con.pdf
 
Starting with the SIR model with births and deaths where i.pdf
Starting with the SIR model with births and deaths where  i.pdfStarting with the SIR model with births and deaths where  i.pdf
Starting with the SIR model with births and deaths where i.pdf
 
QUESTION 23 focus on a single product or service and have th.pdf
QUESTION 23 focus on a single product or service and have th.pdfQUESTION 23 focus on a single product or service and have th.pdf
QUESTION 23 focus on a single product or service and have th.pdf
 
begintabularrrl hline multicolumn1r P23 a.pdf
begintabularrrl hline multicolumn1r P23  a.pdfbegintabularrrl hline multicolumn1r P23  a.pdf
begintabularrrl hline multicolumn1r P23 a.pdf
 
Marin Supplies Company sells many different widgets and uses.pdf
Marin Supplies Company sells many different widgets and uses.pdfMarin Supplies Company sells many different widgets and uses.pdf
Marin Supplies Company sells many different widgets and uses.pdf
 

Recently uploaded

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

I am working on making an AVL tree in Java I need help fini.pdf

  • 1. I am working on making an AVL tree in Java, I need help finishing my roatation methods, rightRotate() is not working so I have not yet attempted rotateLeft(); public class MyAVLNode { private MyAVLNode left, right, parent; private int element, height; public MyAVLNode getRight() { return right; } public void setRight(MyAVLNode right) { this.right = right; } public MyAVLNode getLeft() { return left; } public void setLeft(MyAVLNode left) { this.left = left; } public MyAVLNode getParent() { return parent; } public void setParent(MyAVLNode parent) { this.parent = parent; } public int getElement() { return element; } public void setElement(int element) { this.element = element; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } } public class MyAVLTree { private MyAVLNode root;
  • 2. public MyAVLTree() { root = null; } private MyAVLNode createNode(int element) { MyAVLNode temp = new MyAVLNode(); temp.setElement(element); temp.setLeft(null); temp.setRight(null); temp.setParent(null); temp.setHeight(0); return temp; } private void inorder(MyAVLNode current) { if(current!=null) { inorder(current.getLeft()); System.out.println(current.getElement() + " "+current.getHeight()); inorder(current.getRight()); } } public void print() { inorder(root); } private int getBalanceFactor(MyAVLNode current) { int leftChildHeight = current.getLeft()==null ? -1 : current.getLeft().getHeight(); int rightChildHeight = current.getRight()==null ? -1 : current.getRight().getHeight(); return rightChildHeight - leftChildHeight; } private void rightRotate(MyAVLNode current) { MyAVLNode oldParent = current.getParent(); MyAVLNode grandparent = oldParent.getParent(); if(grandparent==null) { root = current; } else { if(grandparent.getElement() < current.getElement()) { grandparent.setRight(current); } else { grandparent.setLeft(current);
  • 3. } current.setParent(grandparent); oldParent.setLeft(current.getRight()); current.getRight().setParent(oldParent); current.setRight(oldParent); oldParent.setParent(current); } } private void leftRotate(MyTreeNode current){ } private void updateHeight(MyAVLNode current) { if(current!=null) { int leftChildHeight = current.getLeft()==null ? -1 : current.getLeft().getHeight(); int rightChildHeight = current.getRight()==null ? -1 : current.getRight().getHeight(); //check if current is balanced if(Math.abs(rightChildHeight-leftChildHeight)>1) { //unbalanced, fix if(getBalanceFactor(current)==-2) { //left case if(getBalanceFactor(current.getLeft())==-1) { //left left case rightRotate(current.getLeft()); } else { //left right case } } else { //right case } } else { //if it is balanced, update heights current.setHeight(Math.max(leftChildHeight, rightChildHeight)+1); updateHeight(current.getParent()); } } } private void insert(MyAVLNode current, MyAVLNode insertMe) { if(current.getElement()<insertMe.getElement()) {
  • 4. //look to the right if(current.getRight()==null) { //structure things current.setRight(insertMe); insertMe.setParent(current); //update heights, balance factors, parents? updateHeight(current); } else { insert(current.getRight(), insertMe); } } else{ //look to the left if(current.getLeft()==null) { //structure things current.setLeft(insertMe); insertMe.setParent(current); //update heights, balance factors, parents? updateHeight(current); } else { insert(current.getLeft(), insertMe); } } } public void insert(int element) { MyAVLNode temp = createNode(element); if(root==null) { root = temp; } else { insert(root, temp); } } }