balances before returning implements cases one and two as described earlier in the case three is detected when balance of - or results from rebalancing in that case the pivot is found and rebalancing according to case can occur should pivot be foundno balancing need occur above the pivot this is the use of the self pivotfound variable initialized on line of the code in sect this flag can be set to true to avoid any balancing above the pivot nodeshould it be found balances are adjusted just as described in the case by case analysis earlier in the in the worst case the balances of the pivot and bad child will need to be adjusted implementing both the iterative and the recursive versions of insert into avl trees helps illustrate the special cases that must be handled in the iterative versionwhile the recursive version will not need special cases the recursive version does not need special case handling because of the way the __insert works the function always is given the root node of tree in which to insert the new item and returns the root node of the tree after inserting that item since it works in such regular wayspecial case handling is not necessary maintaining balance versus height the two implementations presented in this the recursive and iterative insert algorithms for avl treesmaintained the balance of each node as an alternativethe height of each node could be maintained in this casethe height of leaf node is the height of any other node is plus the maximum height of its two subtrees the height of an empty tree or none is avlnode with stored height class avlnodedef __init__(self,item,height= ,left=none,right=none)self item item self left left self right right self height height def balance(self)return avltree height(self rightavltree height(self leftif the height of nodes is maintained instead of balancesall heights on the path to the new item' inserted location must be adjusted on the way back up the tree unlike balancesit is not possible to stop adjusting heights at the pivot node after rotation the height of the pivot and bad child must also be recomputed as the rotation may change their height since heights are computed bottom-upall heights on the pathincluding the heights of the pivot and bad child should be recomputed in bottomup fashion the code in sect provides partial declaration of an avlnode storing the height of the tree tree rooted at the node in this implementation the balance of any node can be computed from the heights of the two subtrees this copy belongs to 'acha