SlideShare a Scribd company logo
1 of 9
Download to read offline
b. (10 pts) Implement the rotate left method for AVL trees.
c. (10 pts) Implement the rotate right method for AVL trees.
d. (10 pts) Implement the rotate left-right method for AVL trees.
e. (10 pts) Implement the rotate right-left method for AVL trees.
f. (15 pts) Implement the rebalance method for AVL trees.
g. (3 pts) Implement pre-order, in-order, and post-order traversals for AVL trees.
All of this should be implemented in the below code, in C++, without changing any of the
parameters or functions as they are laid out.
#include
#include
#include "AVLTree.h"
#include "AVLNode.h"
using namespace std;
AVLTree::AVLTree() {
root = nullptr;
size = 0;
}
std::shared_ptr AVLTree::getRoot() {
return root;
}
int AVLTree::getSize() {
return size;
}
std::shared_ptr AVLTree::search(int val) {
return search(root, val);
}
std::shared_ptr AVLTree::search(std::shared_ptr n, int val) {
if (n == NULL) //if root node is null just return null value
{
return n;
}
//if target is less than root's value search in left half
if (val < n->value)
{
return search(n->left, val);
}
//if target is greater than root's value search in right half
else if (val > n->value)
{
return search(n->right, val);
}
//else if target is equal to root node just return the root node i.e n.
return n;
}
std::shared_ptr AVLTree::minimum() {
std::shared_ptr curr = root;
while (curr && curr->left) {
curr = curr->left;
}
return curr;
}
std::shared_ptr AVLTree::minimum(std::shared_ptr n) {
std::shared_ptr curr = n;
while (curr && curr->left) {
curr = curr->left;
}
return curr;
}
std::shared_ptr AVLTree::maximum() {
std::shared_ptr curr = root;
while (curr && curr->right) {
curr = curr->right;
}
return curr;
}
std::shared_ptr AVLTree::maximum(std::shared_ptr n) {
std::shared_ptr curr = n;
while (curr && curr->right) {
curr = curr->right;
}
return curr;
}
int getHeight(std::shared_ptr n) {
if (n == nullptr) {
return -1;
}
return n->height;
}
int getBalanceFactor(std::shared_ptr n) {
if (n == nullptr) {
return 0;
}
return getHeight(n->left) - getHeight(n->right);
}
void AVLTree::insertValue(int val) {
root = insertValue(root, val);
}
std::shared_ptr AVLTree::insertValue(std::shared_ptr n, int val) {
if (n == nullptr) {
shared_ptr newNode(new AVLNode(val));
size++;
return newNode;
}
if (val < n->value) {
n->left = insertValue(n->left, val);
}
else if (val > n->value) {
n->right = insertValue(n->right, val);
}
else {
// Duplicate value, no change
return n;
}
// Update the height of the current node
n->height = 1 + max(getHeight(n->left), getHeight(n->right));
// Rebalance the node if necessary
return rebalance(n);
}
void AVLTree::deleteValue(int val) {
root = deleteValue(root, val);
}
std::shared_ptr AVLTree::deleteValue(std::shared_ptr n, int val) {
if (n == nullptr) {
return nullptr;
}
if (val < n->value) {
n->left = deleteValue(n->left, val);
}
else if (val > n->value) {
n->right = deleteValue(n->right, val);
}
else {
// Found the value to delete
if (n->left == nullptr && n->right == nullptr) {
// Leaf node, just delete it
n = nullptr;
size--;
}
else if (val > n->value)
{
n->right = deleteValue(n->right, val);
}
else // if val == n->value, then we have found the node to delete
{
if (n->left == nullptr || n->right == nullptr) // if node has only one child or no child
{
std::shared_ptr temp;
if (n->left != nullptr)
temp = n->left;
else
temp = n->right;
if (temp == nullptr) // no child case
{
temp = n;
n = nullptr;
}
else // one child case
{
*n = *temp;
}
}
else // node has two children
{
// get the inorder successor
std::shared_ptr temp = minimum(n->right);
// copy the inorder successor's value to this node
n->value = temp->value;
// delete the inorder successor
n->right = deleteValue(n->right, temp->value);
}
}
if (n == nullptr)
return n;
// update height of the current node
n->height = std::max(getHeight(n->left), getHeight(n->right)) + 1;
// check balance factor of the current node
int balance = getBalanceFactor(n);
// if the node becomes unbalanced, there are four possible cases:
// left left case
if (balance > 1 && getBalanceFactor(n->left) >= 0)
return rotateRight(n);
// left right case
if (balance > 1 && getBalanceFactor(n->left) < 0)
{
n->left = rotateLeft(n->left);
return rotateRight(n);
}
// right right case
if (balance < -1 && getBalanceFactor(n->right) <= 0)
return rotateLeft(n);
// right left case
if (balance < -1 && getBalanceFactor(n->right) > 0)
{
n->right = rotateRight(n->right);
return rotateLeft(n);
}
return n;
}
}
std::shared_ptr AVLTree::rebalance(std::shared_ptr n) {
return nullptr;
}
std::shared_ptr AVLTree::rotateLeft(std::shared_ptr n)
{
return null_ptr;
}
std::shared_ptr AVLTree::rotateRight(std::shared_ptr n) {
return nullptr;
}
std::shared_ptr AVLTree::rotateLeftRight(std::shared_ptr n)
{
return null_ptr;
}
std::shared_ptr AVLTree::rotateRightLeft(std::shared_ptr n)
{
return null_ptr;
}
void AVLTree::preOrder(std::shared_ptr n, vector>& order) {
return nullptr;
}
void AVLTree::inOrder(std::shared_ptr n, vector>& order) {
return nullptr;
}
void AVLTree::postOrder(std::shared_ptr n, vector>& order) {
return nullptr;
}
Below are files that are just reference code. Do not change the below code.
AVLNode.cpp:
#include "AVLNode.h"
AVLNode::AVLNode(){
value = 0;
height = 0;
balanceFactor = 0;
left = nullptr;
right = nullptr;
parent = nullptr;
}
AVLNode::AVLNode(int v){
value = v;
height = 0;
balanceFactor = 0;
left = nullptr;
right = nullptr;
parent = nullptr;
}
AVLNode.h:
#ifndef AVLNODE_H
#define AVLNODE_H
#include
class AVLNode{
public:
int value;
int height;
int balanceFactor;
std::shared_ptr left;
std::shared_ptr right;
std::shared_ptr parent; // you are not required to update parent nodes but you may use them if
you want
AVLNode();
AVLNode(int);
};
#endif
AVLTree.h:
#ifndef AVLTREE_H
#define AVLTREE_H
#include
#include
#include "AVLNode.h"
class AVLTree{
public:
AVLTree();
std::shared_ptr getRoot();
int getSize();
std::shared_ptr search(int);
std::shared_ptr minimum();
std::shared_ptr maximum();
void insertValue(int);
void deleteValue(int);
void preOrder(std::shared_ptr, std::vector>&);
void inOrder(std::shared_ptr, std::vector>&);
void postOrder(std::shared_ptr, std::vector>&);
private:
std::shared_ptr root;
int size;
std::shared_ptr search(std::shared_ptr, int);
std::shared_ptr minimum(std::shared_ptr);
std::shared_ptr maximum(std::shared_ptr);
std::shared_ptr insertValue(std::shared_ptr, int);
std::shared_ptr deleteValue(std::shared_ptr, int);
std::shared_ptr rebalance(std::shared_ptr);
std::shared_ptr rotateLeft(std::shared_ptr);
std::shared_ptr rotateRight(std::shared_ptr);
std::shared_ptr rotateLeftRight(std::shared_ptr);
std::shared_ptr rotateRightLeft(std::shared_ptr);
};
#endif

More Related Content

Similar to b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf

Write a program that displays an AVL tree along with its balance fact.docx
 Write a program that displays an AVL tree along with its balance fact.docx Write a program that displays an AVL tree along with its balance fact.docx
Write a program that displays an AVL tree along with its balance fact.docxajoy21
 
Dip into Coroutines - KTUG Munich 202303
Dip into Coroutines - KTUG Munich 202303Dip into Coroutines - KTUG Munich 202303
Dip into Coroutines - KTUG Munich 202303Alex Semin
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docxajoy21
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfbermanbeancolungak45
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Conctructing Polytopes via a Vertex Oracle
Conctructing Polytopes via a Vertex OracleConctructing Polytopes via a Vertex Oracle
Conctructing Polytopes via a Vertex OracleVissarion Fisikopoulos
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }John De Goes
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmKALAIRANJANI21
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfeyelineoptics
 
An algorithm for computing resultant polytopes
An algorithm for computing resultant polytopesAn algorithm for computing resultant polytopes
An algorithm for computing resultant polytopesVissarion Fisikopoulos
 
"An output-sensitive algorithm for computing projections of resultant polytop...
"An output-sensitive algorithm for computing projections of resultant polytop..."An output-sensitive algorithm for computing projections of resultant polytop...
"An output-sensitive algorithm for computing projections of resultant polytop...Vissarion Fisikopoulos
 
It is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfIt is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfannaindustries
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 

Similar to b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf (20)

Write a program that displays an AVL tree along with its balance fact.docx
 Write a program that displays an AVL tree along with its balance fact.docx Write a program that displays an AVL tree along with its balance fact.docx
Write a program that displays an AVL tree along with its balance fact.docx
 
Dip into Coroutines - KTUG Munich 202303
Dip into Coroutines - KTUG Munich 202303Dip into Coroutines - KTUG Munich 202303
Dip into Coroutines - KTUG Munich 202303
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdf
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Conctructing Polytopes via a Vertex Oracle
Conctructing Polytopes via a Vertex OracleConctructing Polytopes via a Vertex Oracle
Conctructing Polytopes via a Vertex Oracle
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
Struct examples
Struct examplesStruct examples
Struct examples
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithmRasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
 
An algorithm for computing resultant polytopes
An algorithm for computing resultant polytopesAn algorithm for computing resultant polytopes
An algorithm for computing resultant polytopes
 
"An output-sensitive algorithm for computing projections of resultant polytop...
"An output-sensitive algorithm for computing projections of resultant polytop..."An output-sensitive algorithm for computing projections of resultant polytop...
"An output-sensitive algorithm for computing projections of resultant polytop...
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
Chap11alg
Chap11algChap11alg
Chap11alg
 
It is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdfIt is the main program that implement the min(), max(), floor(), cei.pdf
It is the main program that implement the min(), max(), floor(), cei.pdf
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 

More from akanshanawal

Based on the synopsis of the LeadClick case. Who has or should have .pdf
Based on the synopsis of the LeadClick case. Who has or should have .pdfBased on the synopsis of the LeadClick case. Who has or should have .pdf
Based on the synopsis of the LeadClick case. Who has or should have .pdfakanshanawal
 
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdf
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdfBasado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdf
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdfakanshanawal
 
Banks are illiquid because theirGroup of answer choicesliabilit.pdf
Banks are illiquid because theirGroup of answer choicesliabilit.pdfBanks are illiquid because theirGroup of answer choicesliabilit.pdf
Banks are illiquid because theirGroup of answer choicesliabilit.pdfakanshanawal
 
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdf
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdfBarbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdf
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdfakanshanawal
 
Bacterial genomes evolve through all of the following processes, EXC.pdf
Bacterial genomes evolve through all of the following processes, EXC.pdfBacterial genomes evolve through all of the following processes, EXC.pdf
Bacterial genomes evolve through all of the following processes, EXC.pdfakanshanawal
 
Background about the Industry of Spotify Company a. Industry sales f.pdf
Background about the Industry of Spotify Company a. Industry sales f.pdfBackground about the Industry of Spotify Company a. Industry sales f.pdf
Background about the Industry of Spotify Company a. Industry sales f.pdfakanshanawal
 
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdf
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdfBaarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdf
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdfakanshanawal
 
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdf
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdfB. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdf
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdfakanshanawal
 
Australia has the following EPIC structures. Below indicate the adva.pdf
Australia has the following EPIC structures. Below indicate the adva.pdfAustralia has the following EPIC structures. Below indicate the adva.pdf
Australia has the following EPIC structures. Below indicate the adva.pdfakanshanawal
 
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdf
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdfAvatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdf
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdfakanshanawal
 
Aumentos en el capital social Desplace la curva de oferta agregad.pdf
Aumentos en el capital social Desplace la curva de oferta agregad.pdfAumentos en el capital social Desplace la curva de oferta agregad.pdf
Aumentos en el capital social Desplace la curva de oferta agregad.pdfakanshanawal
 
Author DanteAlighieris landmark book The DivineComedy. - What.pdf
Author DanteAlighieris landmark book The DivineComedy. - What.pdfAuthor DanteAlighieris landmark book The DivineComedy. - What.pdf
Author DanteAlighieris landmark book The DivineComedy. - What.pdfakanshanawal
 
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdf
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdfAtlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdf
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdfakanshanawal
 
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdf
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdfAt Burnt Mesa Pueblo, archaeological studies have used the method of.pdf
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdfakanshanawal
 
Asset TurnoverThe ABC Depot reported the following data (in millio.pdf
Asset TurnoverThe ABC Depot reported the following data (in millio.pdfAsset TurnoverThe ABC Depot reported the following data (in millio.pdf
Asset TurnoverThe ABC Depot reported the following data (in millio.pdfakanshanawal
 
Answer the following questions. You can find the answers in class no.pdf
Answer the following questions. You can find the answers in class no.pdfAnswer the following questions. You can find the answers in class no.pdf
Answer the following questions. You can find the answers in class no.pdfakanshanawal
 
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdf
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdfApplen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdf
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdfakanshanawal
 
Answer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfAnswer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfakanshanawal
 
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdf
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdfArthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdf
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdfakanshanawal
 
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdf
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdfAntes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdf
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdfakanshanawal
 

More from akanshanawal (20)

Based on the synopsis of the LeadClick case. Who has or should have .pdf
Based on the synopsis of the LeadClick case. Who has or should have .pdfBased on the synopsis of the LeadClick case. Who has or should have .pdf
Based on the synopsis of the LeadClick case. Who has or should have .pdf
 
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdf
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdfBasado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdf
Basado en Kuehne & Nagel en el caso de Asia Pac�fico 1. Analizar .pdf
 
Banks are illiquid because theirGroup of answer choicesliabilit.pdf
Banks are illiquid because theirGroup of answer choicesliabilit.pdfBanks are illiquid because theirGroup of answer choicesliabilit.pdf
Banks are illiquid because theirGroup of answer choicesliabilit.pdf
 
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdf
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdfBarbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdf
Barbara Dosseynin ntegral Hemirelik Teorisi, hemirelerin b�t�nsel.pdf
 
Bacterial genomes evolve through all of the following processes, EXC.pdf
Bacterial genomes evolve through all of the following processes, EXC.pdfBacterial genomes evolve through all of the following processes, EXC.pdf
Bacterial genomes evolve through all of the following processes, EXC.pdf
 
Background about the Industry of Spotify Company a. Industry sales f.pdf
Background about the Industry of Spotify Company a. Industry sales f.pdfBackground about the Industry of Spotify Company a. Industry sales f.pdf
Background about the Industry of Spotify Company a. Industry sales f.pdf
 
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdf
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdfBaarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdf
Baarl Proje Planlama, zleme ve Kontrol i�in binann proje yaam d�ng�s� .pdf
 
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdf
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdfB. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdf
B. Firma, Pattersonun varlklarnn ne kadarn ksa vadeli bor� kullanar.pdf
 
Australia has the following EPIC structures. Below indicate the adva.pdf
Australia has the following EPIC structures. Below indicate the adva.pdfAustralia has the following EPIC structures. Below indicate the adva.pdf
Australia has the following EPIC structures. Below indicate the adva.pdf
 
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdf
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdfAvatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdf
Avatar Auto Parts Company, nakit ak tablosunu hazrlamak i�in dolayl .pdf
 
Aumentos en el capital social Desplace la curva de oferta agregad.pdf
Aumentos en el capital social Desplace la curva de oferta agregad.pdfAumentos en el capital social Desplace la curva de oferta agregad.pdf
Aumentos en el capital social Desplace la curva de oferta agregad.pdf
 
Author DanteAlighieris landmark book The DivineComedy. - What.pdf
Author DanteAlighieris landmark book The DivineComedy. - What.pdfAuthor DanteAlighieris landmark book The DivineComedy. - What.pdf
Author DanteAlighieris landmark book The DivineComedy. - What.pdf
 
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdf
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdfAtlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdf
Atlgan yldz gemisinin birinci subay Bay Spockn babas Vulcan gezegen.pdf
 
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdf
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdfAt Burnt Mesa Pueblo, archaeological studies have used the method of.pdf
At Burnt Mesa Pueblo, archaeological studies have used the method of.pdf
 
Asset TurnoverThe ABC Depot reported the following data (in millio.pdf
Asset TurnoverThe ABC Depot reported the following data (in millio.pdfAsset TurnoverThe ABC Depot reported the following data (in millio.pdf
Asset TurnoverThe ABC Depot reported the following data (in millio.pdf
 
Answer the following questions. You can find the answers in class no.pdf
Answer the following questions. You can find the answers in class no.pdfAnswer the following questions. You can find the answers in class no.pdf
Answer the following questions. You can find the answers in class no.pdf
 
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdf
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdfApplen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdf
Applen hisse senedi fiyat 104 dolar. Yatrm yapmak i�in 10.000 dolar.pdf
 
Answer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfAnswer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdf
 
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdf
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdfArthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdf
Arthur Andersenn dahil olduu Enron muhasebe dolandrcl. irketin da.pdf
 
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdf
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdfAntes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdf
Antes de la invenci�n del tel�grafo, las observaciones meteorol�gica.pdf
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf

  • 1. b. (10 pts) Implement the rotate left method for AVL trees. c. (10 pts) Implement the rotate right method for AVL trees. d. (10 pts) Implement the rotate left-right method for AVL trees. e. (10 pts) Implement the rotate right-left method for AVL trees. f. (15 pts) Implement the rebalance method for AVL trees. g. (3 pts) Implement pre-order, in-order, and post-order traversals for AVL trees. All of this should be implemented in the below code, in C++, without changing any of the parameters or functions as they are laid out. #include #include #include "AVLTree.h" #include "AVLNode.h" using namespace std; AVLTree::AVLTree() { root = nullptr; size = 0; } std::shared_ptr AVLTree::getRoot() { return root; } int AVLTree::getSize() { return size; } std::shared_ptr AVLTree::search(int val) { return search(root, val); } std::shared_ptr AVLTree::search(std::shared_ptr n, int val) { if (n == NULL) //if root node is null just return null value { return n; } //if target is less than root's value search in left half if (val < n->value) {
  • 2. return search(n->left, val); } //if target is greater than root's value search in right half else if (val > n->value) { return search(n->right, val); } //else if target is equal to root node just return the root node i.e n. return n; } std::shared_ptr AVLTree::minimum() { std::shared_ptr curr = root; while (curr && curr->left) { curr = curr->left; } return curr; } std::shared_ptr AVLTree::minimum(std::shared_ptr n) { std::shared_ptr curr = n; while (curr && curr->left) { curr = curr->left; } return curr; } std::shared_ptr AVLTree::maximum() { std::shared_ptr curr = root; while (curr && curr->right) { curr = curr->right; } return curr; } std::shared_ptr AVLTree::maximum(std::shared_ptr n) { std::shared_ptr curr = n; while (curr && curr->right) { curr = curr->right; }
  • 3. return curr; } int getHeight(std::shared_ptr n) { if (n == nullptr) { return -1; } return n->height; } int getBalanceFactor(std::shared_ptr n) { if (n == nullptr) { return 0; } return getHeight(n->left) - getHeight(n->right); } void AVLTree::insertValue(int val) { root = insertValue(root, val); } std::shared_ptr AVLTree::insertValue(std::shared_ptr n, int val) { if (n == nullptr) { shared_ptr newNode(new AVLNode(val)); size++; return newNode; } if (val < n->value) { n->left = insertValue(n->left, val); } else if (val > n->value) { n->right = insertValue(n->right, val); } else { // Duplicate value, no change return n; } // Update the height of the current node n->height = 1 + max(getHeight(n->left), getHeight(n->right)); // Rebalance the node if necessary
  • 4. return rebalance(n); } void AVLTree::deleteValue(int val) { root = deleteValue(root, val); } std::shared_ptr AVLTree::deleteValue(std::shared_ptr n, int val) { if (n == nullptr) { return nullptr; } if (val < n->value) { n->left = deleteValue(n->left, val); } else if (val > n->value) { n->right = deleteValue(n->right, val); } else { // Found the value to delete if (n->left == nullptr && n->right == nullptr) { // Leaf node, just delete it n = nullptr; size--; } else if (val > n->value) { n->right = deleteValue(n->right, val); } else // if val == n->value, then we have found the node to delete { if (n->left == nullptr || n->right == nullptr) // if node has only one child or no child { std::shared_ptr temp; if (n->left != nullptr) temp = n->left; else temp = n->right;
  • 5. if (temp == nullptr) // no child case { temp = n; n = nullptr; } else // one child case { *n = *temp; } } else // node has two children { // get the inorder successor std::shared_ptr temp = minimum(n->right); // copy the inorder successor's value to this node n->value = temp->value; // delete the inorder successor n->right = deleteValue(n->right, temp->value); } } if (n == nullptr) return n; // update height of the current node n->height = std::max(getHeight(n->left), getHeight(n->right)) + 1; // check balance factor of the current node int balance = getBalanceFactor(n); // if the node becomes unbalanced, there are four possible cases: // left left case if (balance > 1 && getBalanceFactor(n->left) >= 0) return rotateRight(n); // left right case if (balance > 1 && getBalanceFactor(n->left) < 0) { n->left = rotateLeft(n->left); return rotateRight(n); }
  • 6. // right right case if (balance < -1 && getBalanceFactor(n->right) <= 0) return rotateLeft(n); // right left case if (balance < -1 && getBalanceFactor(n->right) > 0) { n->right = rotateRight(n->right); return rotateLeft(n); } return n; } } std::shared_ptr AVLTree::rebalance(std::shared_ptr n) { return nullptr; } std::shared_ptr AVLTree::rotateLeft(std::shared_ptr n) { return null_ptr; } std::shared_ptr AVLTree::rotateRight(std::shared_ptr n) { return nullptr; } std::shared_ptr AVLTree::rotateLeftRight(std::shared_ptr n) { return null_ptr; } std::shared_ptr AVLTree::rotateRightLeft(std::shared_ptr n) { return null_ptr; } void AVLTree::preOrder(std::shared_ptr n, vector>& order) { return nullptr; } void AVLTree::inOrder(std::shared_ptr n, vector>& order) { return nullptr;
  • 7. } void AVLTree::postOrder(std::shared_ptr n, vector>& order) { return nullptr; } Below are files that are just reference code. Do not change the below code. AVLNode.cpp: #include "AVLNode.h" AVLNode::AVLNode(){ value = 0; height = 0; balanceFactor = 0; left = nullptr; right = nullptr; parent = nullptr; } AVLNode::AVLNode(int v){ value = v; height = 0; balanceFactor = 0; left = nullptr; right = nullptr; parent = nullptr; } AVLNode.h: #ifndef AVLNODE_H #define AVLNODE_H #include class AVLNode{ public: int value; int height; int balanceFactor; std::shared_ptr left; std::shared_ptr right; std::shared_ptr parent; // you are not required to update parent nodes but you may use them if
  • 8. you want AVLNode(); AVLNode(int); }; #endif AVLTree.h: #ifndef AVLTREE_H #define AVLTREE_H #include #include #include "AVLNode.h" class AVLTree{ public: AVLTree(); std::shared_ptr getRoot(); int getSize(); std::shared_ptr search(int); std::shared_ptr minimum(); std::shared_ptr maximum(); void insertValue(int); void deleteValue(int); void preOrder(std::shared_ptr, std::vector>&); void inOrder(std::shared_ptr, std::vector>&); void postOrder(std::shared_ptr, std::vector>&); private: std::shared_ptr root; int size; std::shared_ptr search(std::shared_ptr, int); std::shared_ptr minimum(std::shared_ptr); std::shared_ptr maximum(std::shared_ptr); std::shared_ptr insertValue(std::shared_ptr, int); std::shared_ptr deleteValue(std::shared_ptr, int); std::shared_ptr rebalance(std::shared_ptr); std::shared_ptr rotateLeft(std::shared_ptr);