Data Structure in C++ AVL tree and its Register machine I tried this homework an
ID: 3844198 • Letter: D
Question
Data Structure in C++
AVL tree and its Register machine
I tried this homework and there are errors.
Please check my code and fix errors.
:
#include<iostream>
#include<sstream>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<algorithm>
using std::string;
using std::vector;
struct node {
int key;
node* left;
node* right;
node* parent;
int height;
node(int k) {key = k; left = right = 0; height = 1;}
};
int height(node* root){
return root == nullptr ? 0 : root->height;
}
int bfactor(node* p){ // the balance factor of the given node. It operates with nonzero pointers only
return height(p->right) - height(p->left);
}
void fix_height(node* p){
int hl = height(p->left);
int hr = height(p->right);
p->height = (hl > hr ? hl : hr) + 1;
}
node* rotateR(node* p){
node* q = p->left;
p->left = q->right;
q->right = p;
fix_height(p);
fix_height(q);
return q;
}
node* rotateL(node* q){
node* p = q->right;
q->right = p->left;
p->left = q;
fix_height(q);
fix_height(p);
return p;
}
node* balance(node* p){
fix_height(p);
if(bfactor(p) == 2){
if(bfactor(p->right) < 0)
p->right = rotateR(p->right);
return rotateL(p);
}
if(bfactor(p) == -2){
if(bfactor(p->left) > 0)
p->left = rotateL(p->left);
return rotateR(p);
}
return p;
}
node*& find(node*& root, int value){ // Search
if(!root || root->key == value)
return root;
else if(value < root->key)
return find(root->left, value);
else
return find(root->right, value);
}
void node::insert(node*& root, int key){ // Insertion
node*& n = find(root, key);
if(node==nullptr)
n = new node{key, nullptr, nullptr};
}
node*& largest(node*& root) {
if(!root)
return root;
else if(!root->right)
return root;
else
return largest(root->right);
}
node*& pred(node*& value) {
if(value->left)
return largest(value->left);
}
void remove(node*& root, int value){ // Deletion
if(!value)
return; // Not found
if(!value->left && !value->right) {
// No children, just remove
delete value;
target = nullptr;
}
else if(value->left) {
// One left child, remove and replace with left
node*& vl = value->left;
delete value;
value = vl;
}
else if(value->right) {
// One child right, remove and replace with right
node*& vr = value->right;
delete value;
value = vr;
}
else {
// Both children, swap with predecessor
node*& p = pred(root, key);
std::swap(value->key, p->key);
remove(p);
}
}
void print(std::ostream& out, node* root); // Print
node* merge(node* t1, node* t2); // Tree-merge
bool check(node* root){ // Check tree for correctness
int height = bfactor(root);
if(height == 0 || height == 1)
return true;
else
return false;
}
int main() {
//node* root = NULL; // Creating an empty tree
return 0;
}
Explanation / Answer
#include<iostream>
#include<sstream>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<algorithm>
using std::string;
using std::vector;
struct node {
int key;
node* left;
node* right;
node* parent;
int height;
node(int k) {key = k; left = right = 0; height = 1;}
};
int height(node* root){
return root == nullptr ? 0 : root->height;
}
int bfactor(node* p){ // the balance factor of the given node. It operates with nonzero pointers only
return height(p->right) - height(p->left);
}
void fix_height(node* p){
int hl = height(p->left);
int hr = height(p->right);
p->height = (hl > hr ? hl : hr) + 1;
}
node* rotateR(node* p){
node* q = p->left;
p->left = q->right;
q->right = p;
fix_height(p);
fix_height(q);
return q;
}
node* rotateL(node* q){
node* p = q->right;
q->right = p->left;
p->left = q;
fix_height(q);
fix_height(p);
return p;
}
node* balance(node* p){
fix_height(p);
if(bfactor(p) == 2){
if(bfactor(p->right) < 0)
p->right = rotateR(p->right);
return rotateL(p);
}
if(bfactor(p) == -2){
if(bfactor(p->left) > 0)
p->left = rotateL(p->left);
return rotateR(p);
}
return p;
}
node*& find(node*& root, int value){ // Search
if(!root || root->key == value)
return root;
else if(value < root->key)
return find(root->left, value);
else
return find(root->right, value);
}
void node::insert(node*& root, int key){ // Insertion
node*& n = find(root, key);
if(node==nullptr)
n = new node{key, nullptr, nullptr};
}
node*& largest(node*& root) {
if(!root)
return root;
else if(!root->right)
return root;
else
return largest(root->right);
}
node*& pred(node*& value) {
if(value->left)
return largest(value->left);
}
void remove(node*& root, int value){ // Deletion
if(!value)
return; // Not found
if(!value->left && !value->right) {
// No children, just remove
delete value;
target = nullptr;
}
else if(value->left) {
// One left child, remove and replace with left
node*& vl = value->left;
delete value;
value = vl;
}
else if(value->right) {
// One child right, remove and replace with right
node*& vr = value->right;
delete value;
value = vr;
}
else {
// Both children, swap with predecessor
node*& p = pred(root, key);
std::swap(value->key, p->key);
remove(p);
}
}
void print(std::ostream& out, node* root); // Print
node* merge(node* t1, node* t2); // Tree-merge
bool check(node* root){ // Check tree for correctness
int height = bfactor(root);
if(height == 0 || height == 1)
return true;
else
return false;
}
int main() {
//node* root = NULL; // Creating an empty tree
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.