Introduction to Li Chao Tree
Last Updated :
08 Oct, 2023
A Li Chao tree (also known as a Dynamic Convex Hull or Segment Tree with lazy propagations) is a data structure that allows for efficient dynamic maintenance of the convex hull of a set of points in a 2D plane. The Li Chao tree allows for dynamic insertion, deletion, and query operations on the set of points, and can be used in a variety of geometric problems such as line segment intersection, visibility graphs, and polygon triangulation.
- The structure of a Li Chao tree is a segment tree, a balanced binary tree data structure where each leaf node represents a single point, and each internal node represents a convex hull of the points represented by its child nodes.
- The tree is balanced to ensure that each leaf node is at roughly the same depth, which allows for logarithmic time complexity for operations such as insertion and deletion.
To go further one should begin with some geometric utility functions as given below:
C++
struct line {
long double m, b;
long double operator()( long double x)
{
return m * x + b;
}
};
vector<line> a(N * 4);
|
Java
class Line {
double m, b;
public Line( double m, double b) {
this .m = m;
this .b = b;
}
public double evaluate( double x) {
return m * x + b;
}
}
List<Line> a = new ArrayList<>(N * 4 );
|
Python3
class Line:
def __init__( self , m, b):
self .m = m
self .b = b
def __call__( self , x):
return self .m * x + self .b
a = [Line( 0 , 0 ) for i in range (N * 4 )]
|
C#
public class Line {
private double m;
private double b;
public Line( double m, double b)
{
this .m = m;
this .b = b;
}
public double Evaluate( double x) {
return m * x + b;
}
}
List<Line> a = new List<Line>(N * 4);
|
Javascript
class Line {
constructor(m, b) {
this .m = m;
this .b = b;
}
call(x) {
return this .m * x + this .b;
}
}
const a = new Array(N * 4).fill( null ).map(() => new Line(0, 0));
|
On every node of the segment tree, we are storing the line that maximizes (or minimizes) the value of the mid. If the interval of the node is [L, R), then the line stored in it will maximize(or minimize).
Insert: Let’s say we are inserting a new line to the node which is corresponding to Interval [L, R).To make it easy let’s assume that the line on the node has a smaller slope. So, mid =\ frac{L + R}{2}. Now we have two cases to think about.
Note: Red is the original line in the node
Case 1: red(mid) < blue(mid)
In this Case, We have to replace red line with blue.Now, should we remove red? The answer is No, because there is need of red in segment [L, mid). Because of this reason we should pass red to the node with interval [L, mid), which is its left son.
representation of case 1
Case 2: red(mid) > blue(mid)
In similar way, we should pass blue to its right son and keep red in this node, whose interval is [mid, R).
representation of case 2
Below is the code for the above approach:
Code block
C++
void insert( int l, int r, line segment, int idx = 0)
{
if (l + 1 == r) {
if (segment(l) > a[idx](l))
a[idx] = segment;
return ;
}
int mid = (l + r) / 2;
int leftson = idx * 2 + 1, rightson = idx * 2 + 2;
if (a[idx].m > segment.m)
swap(a[idx], segment);
if (a[idx](mid) < segment(mid)) {
swap(a[idx], segment);
insert(l, mid, segment, leftson);
}
else
insert(mid, r, segment, rightson);
}
|
Java
void insert( int l, int r, Line segment, int idx)
{
if (l + 1 == r) {
if (segment.get(l) > a[idx].get(l)) {
a[idx] = segment;
}
return ;
}
int mid = (l + r) / 2 ;
int leftson = idx * 2 + 1 , rightson = idx * 2 + 2 ;
if (a[idx].m > segment.m)
swap(a[idx], segment);
if (a[idx].get(mid) < segment.get(mid)) {
swap(a[idx], segment);
insert(l, mid, segment, leftson);
}
else {
insert(mid, r, segment, rightson);
}
}
|
Python3
def insert(l, r, segment, idx = 0 ):
if l + 1 = = r:
if segment(l) > a[idx](l):
a[idx] = segment
return
mid = (l + r) / 2
leftson = idx * 2 + 1
rightson = idx * 2 + 2
if a[idx].m > segment.m:
swap(a[idx], segment)
if a[idx](mid) < segment(mid):
swap(a[idx], segment)
insert(l, mid, segment, leftson)
else :
insert(mid, r, segment, rightson)
|
C#
void Insert( int l, int r, Line segment, int idx)
{
if (l + 1 == r)
{
if (segment.Get(l) > a[idx].Get(l))
{
a[idx] = segment;
}
return ;
}
int mid = (l + r) / 2;
int leftson = idx * 2 + 1, rightson = idx * 2 + 2;
if (a[idx].m > segment.m)
Swap(a[idx], segment);
if (a[idx].Get(mid) < segment.Get(mid))
{
Swap(a[idx], segment);
Insert(l, mid, segment, leftson);
}
else
{
Insert(mid, r, segment, rightson);
}
}
|
Javascript
function insert(l, r, segment, idx=0) {
if (l + 1 === r) {
if (segment(l) > a[idx](l)) {
a[idx] = segment;
}
return ;
}
let mid = (l + r) / 2;
let leftson = idx * 2 + 1;
let rightson = idx * 2 + 2;
if (a[idx].m > segment.m) {
swap(a[idx], segment);
}
if (a[idx](mid) < segment(mid)) {
swap(a[idx], segment);
insert(l, mid, segment, leftson);
}
else {
insert(mid, r, segment, rightson);
}
}
|
Code for queries, as we know we only need to consider the intervals that contain the point we need to ask from the way we inserted lines.
C++
long double query( int l, int r, int x, int idx = 0)
{
if (l + 1 == r)
return a[idx](x);
int mid = (l + r) / 2;
int leftson = idx * 2 + 1;
int rightson = idx * 2 + 2;
if (x < mid)
return max(a[idx](x), query(l, mid, x, leftson));
else
return max(a[idx](x), query(mid, r, x, rightson));
}
|
Java
public double query( int l, int r, int x, int idx) {
if (l + 1 == r) {
return a[idx](x);
}
int mid = (l + r) / 2 ;
int leftson = idx * 2 + 1 ;
int rightson = idx * 2 + 2 ;
if (x < mid) {
return Math.max(a[idx](x), query(l, mid, x, leftson));
}
else {
return Math.max(a[idx](x), query(mid, r, x, rightson));
}
}
|
Python3
def query(l, r, x, idx = 0 ):
if l + 1 = = r:
return a[idx](x)
mid = (l + r) / / 2
leftson = idx * 2 + 1
rightson = idx * 2 + 2
if x < mid:
return max (a[idx](x), query(l, mid, x, leftson))
else :
return max (a[idx](x), query(mid, r, x, rightson))
|
C#
using System;
public class Program
{
public delegate long double Function( int x);
public static Function[] a = new Function[10];
public static long double Query( int l, int r, int x, int idx = 0)
{
if (l + 1 == r)
{
return a[idx](x);
}
int mid = (l + r) / 2;
int leftSon = idx * 2 + 1;
int rightSon = idx * 2 + 2;
if (x < mid)
{
return Math.Max(a[idx](x), Query(l, mid, x, leftSon));
}
else
{
return Math.Max(a[idx](x), Query(mid, r, x, rightSon));
}
}
public static void Main( string [] args)
{
a[0] = ( int x) => ( long double )x;
a[1] = ( int x) => ( long double )(x * x);
a[2] = ( int x) => ( long double )(x + 1);
long double result = Query(0, 10, 5);
Console.WriteLine( "Result: " + result);
}
}
|
Javascript
function query(l, r, x, idx = 0) {
if (l + 1 === r) {
return a[idx](x);
}
let mid = Math.floor((l + r) / 2);
let leftson = idx * 2 + 1;
let rightson = idx * 2 + 2;
if (x < mid) {
return Math.max(a[idx](x), query(l, mid, x, leftson));
}
else {
return Math.max(a[idx](x), query(mid, r, x, rightson));
}
}
|
Time Complexity: O(NLogN), for the construction of the tree and O(LogN) for each Query
Auxiliary Space: O(N)
Advantages of Li Chao tree:
- Dynamic Convex Hull: The Li Chao tree is a dynamic data structure that can handle line segments and can be used to maintain the convex hull of a set of lines in real-time.
- Fast Queries: Li Chao tree has a logarithmic time complexity for querying the minimum value on a given line segment, making it efficient for large datasets.
- Space-efficient: Li Chao tree uses a balanced binary tree structure, which is space-efficient.
Disadvantages of the Li Chao tree:
- Complexity: The construction of the Li Chao tree takes O(n log n) time and O(n) space, which can be computationally expensive for large datasets.
- Limited to lines: Li Chao tree can only handle line segments and not other types of geometric shapes.
- Limited to 2D: Li Chao tree is limited to 2-dimensional space and cannot be used for 3D or higher-dimensional problems.
- Limited to linear functions: Li Chao tree is limited to linear functions and cannot be used for non-linear functions.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...