balanced binary search trees avl tree recursive insert when implementing recursive function it is much easier to write as stand-alone function as opposed to method of class this is because stand-alone method may be called on nothing ( none in the case of pythonwhile method must always have non-null self reference writing recursive functions as methods leads to special cases for self for instancethe insert methodif written recursivelyis easier to implement if it calls __insert as its recursive function the __insert function of sect won' suffice for height balanced avl trees the insert algorithm must take into account the current balance of the tree and operate to maintain the balance as we discussed in the three cases presented in the previous section the recursive insert avl tree class declaration class avltreeclass avlnodedef __init__(self,item,balance= ,left=none,right=none)self item item self left left self right right self balance balance other methods to be written here like __iter__ and __repr__ see chap def __init__(self,root=none)self root root def insert(selfitem) def __insert(root,item)code to be written here return root self pivotfound false self root __insert(self root,item def __repr__(self)return "avltree(repr(self root") def __iter__(self)return iter(self rootthe shell of the recursive implementation is given in sect the algorithm proceeds much like combination of the three cases presented above along with the implementation of insert presented in sect there is no path stack in the recursive implementation insteadthe run-time stack serves that purpose between lines and or lines and of sect there is an opportunity to rebalance the tree as the code returns and works its way back up from the recursive calls as each call returnsthe balances of each node can be adjusted accordingly adjusting this copy belongs to 'acha