Question
modify just the last two functions in order to implement double rotations directly instead of calling the two single rotations. thank you very much class
modify just the last two functions in order to implement double rotations directly instead of calling the two single rotations. thank you very much
class AvlNode{
public methods below.... private: struct AvlNode { Comparable element; AvlNode *left; AvlNode *right; int height;
AvlNode *root; int nodeCount(AvlNode *t){ if(t == NULL) return 0; return (nodeCount(t->left) + nodeCount(t->right)) + 1; } /** * Return the height of node t or -1 if nullptr. */ int height( AvlNode *t ) const { return t == nullptr ? -1 : t->height; }
int max( int lhs, int rhs ) const { return lhs > rhs ? lhs : rhs; }
/** * Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then set new root. */ void rotateWithLeftChild( AvlNode * & k2 ) { AvlNode *k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = max( height( k2->left ), height( k2->right ) ) + 1; k1->height = max( height( k1->left ), k2->height ) + 1; k2 = k1; }
/** * Rotate binary tree node with right child. * For AVL trees, this is a single rotation for case 4. * Update heights, then set new root. */ void rotateWithRightChild( AvlNode * & k1 ) { AvlNode *k2 = k1->right; k1->right = k2->left; k2->left = k1; k1->height = max( height( k1->left ), height( k1->right ) ) + 1; k2->height = max( height( k2->right ), k1->height ) + 1; k1 = k2; }
/** * Double rotate binary tree node: first left child. * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. * Update heights, then set new root. */ void doubleWithLeftChild( AvlNode * & k3 ) { rotateWithRightChild( k3->left ); rotateWithLeftChild( k3 ); }
/** * Double rotate binary tree node: first right child. * with its left child; then node k1 with new right child. * For AVL trees, this is a double rotation for case 3. * Update heights, then set new root. */ void doubleWithRightChild( AvlNode * & k1 ) { rotateWithLeftChild( k1->right ); rotateWithRightChild( k1 ); }
};
Instread of doing two single rotations for the double rotations please implement so it directly.
thank you so much.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started