# binary tree exam question - W2016
from binary_tree import BinaryTree


def swap_even(t, depth=0):
    """
    Swap left and right children of nodes at even depth.

    Recall that the root has depth 0, its children have depth 1,
    grandchildren have depth 2, and so on.

    @param BinaryTree t: tree to carry out swapping on.
    @param int depth: distance from the root
    @rtype: None

    >>> b1 = BinaryTree(1, BinaryTree(2, BinaryTree(3)))
    >>> b4 = BinaryTree(4, BinaryTree(5), b1)
    >>> print(b4)
        1
            2
                3
    4
        5
    <BLANKLINE>
    >>> swap_even(b4)
    >>> print(b4)
        5
    4
        1
                3
            2
    <BLANKLINE>
    """
    if t is None:
        return None
    elif depth % 2 == 0:
        t.left, t.right = t.right, t.left
    else:
        pass
    swap_even(t.left, depth+1)
    swap_even(t.right, depth+1)

