def relink(selfparentchildmake left child)"""relink parent node with child node (we allow child to be none""make it left child if make left childparent left child elsemake it right child parent right child if child is not nonemake child point to parent child parent parent def rotate(selfp)"""rotate position above its parent "" node we assume this exists parent grandparent (possibly nonez parent if is nonex becomes root self root parent none elsex becomes direct child of self relink(zxy = leftnow rotate and yincluding transfer of middle subtree if = leftx right becomes left child of self relink(yx righttruey becomes right child of self relink(xyfalseelsex left becomes right child of self relink(yx leftfalsey becomes left child of self relink(xytruedef restructure(selfx)"""perform trinode restructure of position with parent/grandparent "" self parent(xz self parent(yif ( =self right( )=( =self right( ))matching alignments single rotation (of yself rotate(yreturn is new subtree root elseopposite alignments double rotation (of xself rotate(xself rotate(xreturn is new subtree root code fragment additional code for the treemap class (continued from code fragment )to provide nonpublic utilities for balanced search tree subclasses