if a.left is not None:
    a.left.parent = a
if b.right is not None:
    b.right.parent = b
if b.left is not None:
    b.left.parent = b
Algorithm to exchange the roles of two randomly chosen nodes from a tree moving pointers