Try this in Python code. I used strings for leaves, but this will work with any objects.
def lift_middle_child(in_tree): (A, (B,), (C,), (D,)) = in_tree return (C, (B,), (D,)) print lift_middle_child(('A', ('B',), ('C',), ('D',)))
Such a tree transformation is usually better performed in a functional style - if you create a bunch of these functions, you can explicitly compose them or create a composition function to work with them in a dot-free style.
Since you used s-expressions, I assume that it is convenient for you to represent trees as nested lists (or the equivalent - if I'm not mistaken, lxml nodes are iterable in this way). Obviously this example is based on a known input structure, but your question implies this. You can write more flexible functions and still compose them if they have this uniform interface.
Here's the code in action: http://ideone.com/02Uv0i
Now, here is the function for looking back at children and using this function and above, one for lifting and reversing:
def compose2(a,b): # might want to get this from the functional library return lambda *x: a(b(*x)) def compose(*funcs): #compose(a,b,c) = a(b(c(x))) - you might want to reverse that return reduce(compose2,funcs) def reverse_children(in_tree): return in_tree[0:1] + in_tree[1:][::-1] # slightly cryptic, but works for anything subscriptable lift_and_reverse = compose(reverse_children,lift_middle_child) # right most function applied first - if you find this confusing, reverse order in compose function. print lift_and_reverse(('A', ('B',), ('C',), ('D',)))