Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!