Kalkwst / MicroLib

MIT License
3 stars 0 forks source link

:sparkles: Add Cartesian Tree Implementation #37

Open Kalkwst opened 1 year ago

Kalkwst commented 1 year ago

A Cartesian tree is a type of binary tree that can be used to efficiently store and retrieve data. It is built from a given array of data and the tree has the property that the value of a node is greater than or equal to the value of its left child and less than or equal to the value of its right child. This allows for efficient operations like range queries, as it can be used to find the range of elements in a given interval efficiently.

From a more technical perspective, a Cartesian tree is a binary tree that is built from a given array of data. It is constructed by selecting the element that is at the middle of the array, which becomes the root of the tree, and recursively building the left and right subtrees by selecting the middle element of the left and right parts of the array. This way, the value of a node is greater than or equal to the value of its left child and less than or equal to the value of its right child. This property allows for efficient operations like range queries, as it can be used to find the range of elements in a given interval efficiently in a time complexity of O(log n) where n is the number of nodes in the tree. It can also be used to construct a balanced binary search tree from a given array of data, which can be useful in situations where the data is not guaranteed to be sorted.