Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
segment trees with only point updates More...
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Data Structures | |
struct | segment_tree |
This structures holds all the data that is required by a segment tree. More... | |
Typedefs | |
typedef void(* | combine_function) (const void *a, const void *b, void *result) |
Function that combines two data to generate a new one The name of function might be misleading actually combine here signifies the fact that in segment trees we take partial result from two ranges and using partial results we derive the result for joint range of those two ranges For Example: array(1,2,3,4,5,6) sum of range [0,2] = 6 and sum of range [3,5] = 15 the combined sum of two range is 6+15=21. More... | |
typedef struct segment_tree | segment_tree |
This structures holds all the data that is required by a segment tree. | |
Functions | |
void | segment_tree_build (segment_tree *tree) |
Builds a Segment tree It is assumed that leaves of tree already contains data. More... | |
void | segment_tree_update (segment_tree *tree, size_t index, void *val) |
For point updates This function updates the element at given index and also updates segment tree accordingly. More... | |
void | segment_tree_query (segment_tree *tree, long long l, long long r, void *res) |
Query the segment tree This function helps in range query of segment tree This function assumes that the given range is valid Performs the query in range [l,r]. More... | |
segment_tree * | segment_tree_init (void *arr, size_t elem_size, size_t len, void *identity, combine_function func) |
Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with data from array. More... | |
void | segment_tree_dispose (segment_tree *tree) |
Dispose Segment Tree Frees all heap memory accquired by segment tree. More... | |
void | segment_tree_print_int (segment_tree *tree) |
Prints the data in segment tree The data should be of int type A utility to print segment tree with data type of int. More... | |
void | minimum (const void *a, const void *b, void *c) |
Utility for test A function compare for minimum between two integers This function is used as combine_function for RMQ. More... | |
static void | test () |
Test RMQ Testing Segment tree using Range Minimum Queries. More... | |
int | main () |
Main Function. More... | |
segment trees with only point updates
This code implements segment trees. Segment trees are general structures which allow range based queries in a given array in logN time. Segment tree with point updates allow update of single element in the array in logN time. Learn more about segment trees here
typedef void(* combine_function) (const void *a, const void *b, void *result) |
Function that combines two data to generate a new one The name of function might be misleading actually combine here signifies the fact that in segment trees we take partial result from two ranges and using partial results we derive the result for joint range of those two ranges For Example: array(1,2,3,4,5,6) sum of range [0,2] = 6 and sum of range [3,5] = 15 the combined sum of two range is 6+15=21.
a | pointer to first data |
b | pointer to second data |
result | pointer to memory location where result of combining a and b is to be stored |
int main | ( | void | ) |
void minimum | ( | const void * | a, |
const void * | b, | ||
void * | c | ||
) |
Utility for test A function compare for minimum between two integers This function is used as combine_function for RMQ.
a | pointer to integer a |
b | pointer to integer b |
c | pointer where minimum of a and b is tored as result |
void segment_tree_build | ( | segment_tree * | tree | ) |
Builds a Segment tree It is assumed that leaves of tree already contains data.
tree | pointer to segment tree to be build |
void segment_tree_dispose | ( | segment_tree * | tree | ) |
Dispose Segment Tree Frees all heap memory accquired by segment tree.
tree | pointer to segment tree |
segment_tree * segment_tree_init | ( | void * | arr, |
size_t | elem_size, | ||
size_t | len, | ||
void * | identity, | ||
combine_function | func | ||
) |
Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with data from array.
arr | the array data upon which segment tree is build |
elem_size | size of each element in segment tree |
len | total no of elements in array |
identity | the identity element for combine_function |
func | the combine_function used to build segment tree |
void segment_tree_print_int | ( | segment_tree * | tree | ) |
Prints the data in segment tree The data should be of int type A utility to print segment tree with data type of int.
tree | pointer to segment tree |
void segment_tree_query | ( | segment_tree * | tree, |
long long | l, | ||
long long | r, | ||
void * | res | ||
) |
Query the segment tree This function helps in range query of segment tree This function assumes that the given range is valid Performs the query in range [l,r].
tree | pointer to segment tree |
l | the start of range |
r | the end of range |
res | the pointer to memory where result of query is stored |
void segment_tree_update | ( | segment_tree * | tree, |
size_t | index, | ||
void * | val | ||
) |
For point updates This function updates the element at given index and also updates segment tree accordingly.
tree | pointer to segment tree |
index | the index whose element is to be updated (0 based indexing used) |
val | pointer to value that is to be replaced at given index |
|
static |
Test RMQ Testing Segment tree using Range Minimum Queries.