SlideShare a Scribd company logo
1 of 4
Download to read offline
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.pdfabithmobiles
 
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 .pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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 .pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
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.pdfabithmobiles
 
begintabularrrl hline multicolumn1r P23 a.pdf
begintabularrrl hline multicolumn1r P23  a.pdfbegintabularrrl hline multicolumn1r P23  a.pdf
begintabularrrl hline multicolumn1r P23 a.pdfabithmobiles
 
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.pdfabithmobiles
 

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

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscapingDr. M. Kumaresan Hort.
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 

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); } } }