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) {
// .
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