6.851 Advanced Data Structures (Spring’10) Prof. Erik Demaine Dr. Andr´ e Schulz TA: Aleksandar Zlateski Problem 5 Sample Solutions Cartesian trees in linear time. We will add the elements according to their order in A . If the current element x is greater than the previously added one, we just add it as its right child. Otherwise, we walk up the three until we ﬁnd an element that is smaller then the current one. We then attach x as its right child, and attach it’s previous right child as x ’s left child. It is clear that with every step we make towards the root we reduce the path from the last element to the root by one. Having total of
This is the end of the preview. Sign up
access the rest of the document.