6.851 Advanced Data Structures (Spring’10)Prof. Erik DemaineDr. Andr´e SchulzTA: Aleksandar ZlateskiProblem 5Sample SolutionsCartesian trees in linear time.We will add the elements according to their order inA. If the current elementxis greater thanthe previously added one, we just add it as its right child. Otherwise, we walk up the three untilwe find an element that is smaller then the current one. We then attachxas its right child, andattach it’s previous right child asx’s left child.It is clear that with every step we make towards the root we reduce the path from the lastelement to the root by one. Having total of
This is the end of the preview.
access the rest of the document.