VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cartesian-tree/

⇱ Cartesian Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Cartesian Tree

Last Updated : 23 Jul, 2025

A Cartesian tree is a tree data structure created from a set of data that obeys the  following structural invariants:
 

  1. The tree obeys in the min (or max) heap property - each node is less (or greater) than its children.
  2. An inorder traversal of the nodes yields the values in the same order in which they appear in the initial sequence.


Suppose we have an input array- {5,10,40,30,28}. Then the max-heap Cartesian Tree would be.
 

👁 cartesianTree0


A min-heap Cartesian Tree of the above input array will be- 
 

👁 cartesianTree1


Note: 
 

  1. Cartesian Tree is not a height-balanced tree.
  2. Cartesian tree of a sequence of distinct numbers is always unique.


Cartesian tree of a sequence of distinct numbers is always unique. 
We will prove this using induction. As a base case, empty tree is always unique. For the inductive case, assume that for all trees containing n' < n elements, there is a unique Cartesian tree for each sequence of n' nodes. Now take any sequence of n elements. Because a Cartesian tree is a min-heap, the smallest element of the sequence must be the root of the Cartesian tree. Because an inorder traversal of the elements must yield the input sequence, we know that all nodes to the left of the min element must be in its left subtree and similarly for the nodes to the right. Since the left and right subtree are both Cartesian trees with at most n-1 elements in them (since the min element is at the root), by the induction hypothesis there is a unique Cartesian tree that could be the left or right subtree. Since all our decisions were forced, we end up with a unique tree, completing the induction.
How to construct Cartesian Tree? 
A O(n2) solution for construction of Cartesian Tree is discussed here (Note that the above program here constructs the “special binary tree” (which is nothing but a Cartesian tree)
A O(nlogn) Algorithm : 
It's possible to build a Cartesian tree from a sequence of data in O(NlogN) time on average. Beginning with the empty tree,
Scan the given sequence from left to right adding new nodes as follows: 
 

  1. Position the node as the right child of the rightmost node.
  2. Scan upward from the node's parent up to the root of the tree until a node is found whose value is greater than the current value.
  3. If such a node is found, set its right child to be the new node, and set the new node's left child to be the previous right child.
  4. If no such node is found, set the new child to be the root, and set the new node's left child to be the previous tree.


 

Output: 
 

Inorder traversal of the constructed tree :
5 10 40 30 28


Time Complexity : 
At first look, the code seems to be taking O(n2) time as there are two loop in buildCartesianTree(). But actually, it takes O(NlogN) time in average and O(n^2) for sorted preorder traversal.
Auxiliary Space: 
We declare a structure for every node as well as three extra arrays- leftchild[], rightchild[], parent[] to hold the indices of left-child, right-child, parent of each value in the input array. Hence the overall O(4*n) = O(n) extra space.
Application of Cartesian Tree 
 

  • Cartesian Tree Sorting
  • A range minimum query on a sequence is equivalent to a lowest common ancestor query on the sequence's Cartesian tree. Hence, RMQ may be reduced to LCA using the sequence's Cartesian tree.
  • Treap, a balanced binary search tree structure, is a Cartesian tree of (key,priority) pairs; it is heap-ordered according to the priority values, and an inorder traversal gives the keys in sorted order.
  • Suffix tree of a string may be constructed from the suffix array and the longest common prefix array. The first step is to compute the Cartesian tree of the longest common prefix array.


References: 
http://wcipeg.com/wiki/Cartesian_tree
 

Comment
Article Tags: