Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need Code for left_balance(); It should work similarly to right_balance(); C++ [NO SCREENSHOT, TYPED ANSWERS] WRITE ORIGINAL CODE FOR THUMBS UP Left Balance: template

I need Code for left_balance(); It should work similarly to right_balance();

C++ [NO SCREENSHOT, TYPED ANSWERS] WRITE ORIGINAL CODE FOR THUMBS UP

Left Balance:

template

void AVL_tree::left_balance(Binary_node* &sub_root)

/*

Pre: sub_root points to a subtree of an AVL_tree that

is doubly unbalanced on the left.

Post: The AVL properties have been restored to the subtree.

*/

{

//HERE

}

Right Balance:

template

void AVL_tree::right_balance(Binary_node *&sub_root)

/*

Pre: sub_root points to a subtree of an AVL_tree that

is unbalanced on the right.

Post: The AVL properties have been restored to the subtree.

*/

{

Binary_node* &right_tree = sub_root->right;

switch (right_tree->get_balance()) {

// case right_higher: sigle left rotation

// O ub --> subroot

// \

// O rh --> right_tree

// \

// O

case right_higher: // single left rotation

sub_root->set_balance(equal_height);

right_tree->set_balance(equal_height);

rotate_left(sub_root); //pointer adjustment

break;

case equal_height: // impossible case

cout << "WARNING: If you see this in an insertion, program error is detected in right_balance" << endl;

break;

// case left_higher: double rotation left

// O ub --> sub_root

// \

// O lh --> right_tree

// /

// O three cases --> sub_tree

case left_higher:

Binary_node *sub_tree = right_tree->left;

//set balance of sub_root and right_tree assuming rotation is done

switch (sub_tree->get_balance()) {

case equal_height:

sub_root->set_balance(equal_height);

right_tree->set_balance(equal_height);

break;

case left_higher:

sub_root->set_balance(equal_height);

right_tree->set_balance(right_higher);

break;

case right_higher:

sub_root->set_balance(left_higher);

right_tree->set_balance(equal_height);

break;

}

//set balance of sub_tree after rotation

sub_tree->set_balance(equal_height);

//perform actual rotation

rotate_right(right_tree);

rotate_left(sub_root);

break;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

BMW Brand Audit

Authors: Marion Maguire

1st Edition

3638653137, 978-3638653138

More Books

Students also viewed these Accounting questions