BSTY::BSTY() { root = NULL; } #include \"BSTY.hpp\" #include <iostream> #include
ID: 3708484 • Letter: B
Question
BSTY::BSTY() {
root = NULL;
}
#include "BSTY.hpp"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
With this information please write this method in C++
// the adjustHeights method updates the heights of every ancestor of the node n // This method will be massively useful with our next lab, so make sure you have // this working now. It should be called whenever you change the height of a // a particular node. So, for instance, when a new node is inserted as a leaf, // its height is 1. The adjustHeights method should then check the parent to see // if the height of the parent changes (the height of each node is the maximum of // the height of the left child and the height of the right child, plus 1). If // the parent's height changes, then the grandparent's height should be checked and, // if necessary, adjusted. If the grandparent's height is updated, then the great- // grandparent's height should be adjusted, etc. The checking stops when either // the loop has worked its way up to the root, or until the currently being checked // ancestor is not changed. void BSTY: :adjustHeights (NodeT *n) {Explanation / Answer
The code inside adjustHieghts method will be some thing like this:-
//traverse through the loop till the root element of tree is reached
while(n != null){
//calculating the address of parent node
parent = n-1/2;
//if parent is null set hieght =0 and return
}
n = parent;
}
//function to return max
int max(int i, int j){
if(i>j){
return i;
}else{
return j;
}
//program to return height of node n
int hieght(Node n){
if(n==null){
return 0;
}
else{
return n.hieght;
}
Then calculate the address of the parent, if it does not exists set hieght to 0 else set hieght to hieght of left or right node which ever is maximum +1 and the finally, move to next parent node by assigning parent to node or n.
Hope this will help you!!!Thanks!!!Do give it a thumbs up!!!:)
The code is self explanatory and comments are added for your better understanding.While loop is used to iterate through the entire tree and find if parent exists or not and if it does find the updated hieght of parent.Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.