CLR cover
Previous problem

Problem 145: Cartesian tree pattern-matching

Next problem
\( \def\PD{\mathit{PD}} \)

In the problem we consider words drawn from a linear-sortable alphabet $\Sigma$ of integers. Let $x=x[0\dd m-1]$ be a word of length $m$. The Cartesian Tree $\CT(x)$ of $x$ is the binary tree in which:

The Cartesian tree pattern-matching problem is naturally defined as follows: given a pattern $x$ and a text $y$ of length $m$ and $n$ respectively, find all the factors of $y$ that have the same Cartesian tree as $x$.

Design an online linear time and space algorithm that builds the Cartesian tree of $x\in\Sigma^*$.
Consider only the nodes on the rightmost paths of the tree.

Design a linear-time algorithm for the Cartesian tree pattern-matching related to a pattern $x$ and a text $y$ in $\Sigma^*$.
Find a linear representation of Cartesian trees and design a notion of border table (see Problem 19) adequate to the problem.

References