This statement bugged me a little, primarily because it sounded far too easy. Would a seasoned programmer really struggle to swap left/right node values;
doubtful, hence I filed it away as something that needed clarification. First thought I'd have to PM Max to ask for clarification, but apparently it was easier than that: it has already been asked and answered.
Let's summarize
Many assumed incorrectly that it implied swapping the left/right nodes (i.e. a simplistic horizontal flip of a binary tree) -- an action with no obvious practical application, yet others assumed it was to vertically flip the tree; again impractical as that is only a representational output difference, again no obvious practical use.
So the question is what was actually expected? Max provided the following clarification On Twitter:
View attachment 360700
...or more specifically, the problem was to demonstrate how to turn a binary tree into a min-max binary tree, otherwise known as a double ended priority queue. Min-max ordered binary trees are used to expedite the process to find both the largest and the smallest elements of a binary tree in a predictable timeframe (constant); as opposed to reordering at a cost of a full sort (n log n) or worst case (n^2). Similarly time penalities are reduced when deleting or inserting nodes as it again doesn't required an expensive sort, but the ordering stays localised to only the relevant even (odd) neighbors.
Detail explanation
A binary tree is consider min-max ordered once every element on even (odd) levels is less (or greater) than all children and grand children. Here's two examples of a min-max ordered binary tree:
View attachment 360708
View attachment 360758
The implementation will typically require the following methods:
- insert / delete node with localised reordering
- Localised sorting using: bubble up, sink down and swap
- Custom min / max that iterates between even / odd levels to find answers
Reference:
http://cglab.ca/~morin/teaching/5408/refs/minmax.pdf