subject

Write a c++ function restructure to perform a rotation operation on an unbalanced binary tree

#ifndef AVLTREE_H
#define AVLTREE_H
#include "AVLEntry. h"
#include "BoundaryViolation. h"
#include "NonexistentElement. h"
#include "SearchTree. h"
#include "PrintExpressionTour. h"

template // an AVL tree
class AVLTree : public SearchTree< AVLEntry > {
public: // public types
typedef AVLEntry AVLEntry; // an entry
typedef typename SearchTree::Iterator Iterator; // an iterator
protected: // local types
typedef typename AVLEntry::Key K; // a key
typedef typename AVLEntry::Value V; // a value
typedef SearchTree ST; // a search tree
typedef typename ST::TPos TPos; // a tree position
public: // public functions
AVLTree(); // constructor
Iterator insert(const K& k, const V& x); // insert (k, x)
void erase(const K& k) throw(NonexistentElement); // remove key k entry
void erase(const Iterator& p); // remove entry at p
protected: // utility functions
int height(const TPos& v) const; // node height utility
void setHeight(TPos v); // set height utility
bool isBalanced(const TPos& v) const; // is v balanced?
TPos tallGrandchild(const TPos& v) const; // get tallest grandchild
void rebalance(const TPos& v); // rebalance utility
void restructure(const TPos& v);
};

template // constructor
AVLTree::AVLTree() : ST() { }

template // node height utility
int AVLTree::height(const TPos& v) const
{
return (v. isExternal() ? 0 : v->height());
}

template // set height utility
void AVLTree::setHeight(TPos v) {
int hl = height(v. left());
int hr = height(v. right());
v->setHeight(1 + std::max(hl, hr)); // max of left & right
}

template // is v balanced?
bool AVLTree::isBalanced(const TPos& v) const {
int bal = height(v. left()) - height(v. right());
return ((-1 <= bal) && (bal <= 1));
}

template // get tallest grandchild
typename AVLTree::TPos AVLTree::tallGrandchild(const TPos& z) const {
TPos zl = z. left();
TPos zr = z. right();
if (height(zl) >= height(zr)) // left child taller
if (height(zl. left()) >= height(zl. right()))
return zl. left();
else
return zl. right();
else // right child taller
if (height(zr. right()) >= height(zr. left()))
return zr. right();
else
return zr. left();
}

template // insert (k, x)
typename AVLTree::Iterator AVLTree::insert(const K& k, const V& x) {
TPos v = inserter(k, x); // insert in base tree
setHeight(v); // compute its height
rebalance(v); // rebalance if needed
return Iterator(v);
}

template // remove key k entry
void AVLTree::erase(const K& k) throw(NonexistentElement) {
TPos v = finder(k, ST::root()); // find in base tree
if (Iterator(v) == ST::end()) // not found?
throw NonexistentElement("Erase of nonexistent");
TPos w = eraser(v); // remove it
rebalance(w); // rebalance if needed
}

template
void AVLTree::restructure(const TPos& x)
{

}

template // rebalancing utility
void AVLTree::rebalance(const TPos& v) {
TPos z = v;
while (!(z == ST::root())) { // rebalance up to root
z = z. parent();
setHeight(z); // compute new height
if (!isBalanced(z)) { // restructuring needed
TPos x = tallGrandchild(z);
z = restructure(x); // trinode restructure
setHeight(z. left()); // update heights
setHeight(z. right());
setHeight(z);
}
}
}
#endif

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 22:40, Bgreene2377
In this lab, you complete a python program that calculates an employee's annual bonus. input is an employee's first name, last name, salary, and numeric performance rating. if the rating is 1, 2, or 3, the bonus rate used is .25, .15, or .1 respectively. if the rating is 4 or higher, the rate is 0. the employee bonus is calculated by multiplying the bonus rate by the annual salary.
Answers: 1
image
Computers and Technology, 23.06.2019 00:30, Thisisdifinite
Which of the following would you find on a network
Answers: 3
image
Computers and Technology, 23.06.2019 01:30, marmar72
Negative methods of behavior correction include all but this: sarcasm verbal abuse setting an example for proper behavior humiliation
Answers: 1
image
Computers and Technology, 23.06.2019 05:00, bellad0124outlookcom
In cell b18, enter a formula to calculate the amount budgeted for meals. this amount is based on the daily meal allowance and the total travel days (# of nights+1).
Answers: 1
You know the right answer?
Write a c++ function restructure to perform a rotation operation on an unbalanced binary tree
...

Questions in other subjects: