Answered step by step
Verified Expert Solution
Question
1 Approved Answer
An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of
An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as: balanceFactor = height(left subtree) - height(right subtree) The balance factor of any node of an AVL tree is in the integer range [1,+1]. If after any modification in the tree, the balance factor becomes less than -1 or greater than +1 , the subtree rooted at this node is unbalanced, and a rotation is needed. 'node' is defined as: struct node \{ int val; //value struct node* left; //left child struct node* right; //right child int ht; //height of the node \} node; You only need to complete the function. Note: All the values in the tree will be distinct. Height of a Null node is -1 and the height of the leaf node is 0 . Sample Output Explanation After inserting 6 in the tree. the tree becomes: 3 (Balance Factor =2 ) 11 24 (Balance Factor =2 ) 1 5 (Balance Factor = -1) 1 6 (Balance Factor =0 ) Balance Factor of nodes 3 and 4 is no longer in the range [1,1]. We need to perform a rotation to balance the tree. This is the right right case. We perform a single rotation to balance the tree. After performing the rotation, the tree becomes : 3 (Balance Factor =1 ) 11(BalanceFactor=0)25(BalanceFactor=0)11(BalanceFactor=0)46(BalanceFactor=0) 28// Node is defined as: typedef struct node \{ int val; struct node* left; struct node* right; int ht; \} node; */ node * insert(node * root, int val) \{ \}
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