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>
|
struct | segment_tree |
| This structures holds all the data that is required by a segment tree. More...
|
|
|
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.
|
|
|
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
- Author
- Lakhan Nad
◆ combine_function
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.
- Note
- The function is same to binary function in Discrete Mathematics
- Parameters
-
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 |
◆ main()
Main Function.
- Returns
- 0 on exit
◆ minimum()
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.
- Parameters
-
a | pointer to integer a |
b | pointer to integer b |
c | pointer where minimum of a and b is tored as result |
196 *(
int *)c = *(
int *)a < *(
int *)b ? *(
int *)a : *(
int *)b;
◆ segment_tree_build()
Builds a Segment tree It is assumed that leaves of tree already contains data.
- Parameters
-
tree | pointer to segment tree to be build |
58 int index = (tree->
length - 2);
60 char *ptr = (
char *)tree->
root;
61 for (; index >= 0; index--)
63 b = index * elem_size;
64 l = (2 * index + 1) * elem_size;
65 r = (2 * index + 2) * elem_size;
66 tree->
combine(ptr + l, ptr + r, ptr + b);
◆ segment_tree_dispose()
Dispose Segment Tree Frees all heap memory accquired by segment tree.
- Parameters
-
tree | pointer to segment tree |
◆ segment_tree_init()
Initializes Segment Tree Accquires memory for segment tree and fill the leaves of segment tree with data from array.
- Parameters
-
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 |
- Returns
- pointer to sgement tree build
147 tree->
root = malloc(
sizeof(
char) * elem_size * (2 * len - 1));
148 tree->
identity = malloc(
sizeof(
char) * elem_size);
149 char *ptr = (
char *)tree->
root;
150 memset(ptr, 0, (len - 1) * elem_size);
151 ptr = ptr + (len - 1) * elem_size;
152 memcpy(ptr, arr, elem_size * len);
153 memcpy(tree->
identity, identity, elem_size);
◆ segment_tree_print_int()
Prints the data in segment tree The data should be of int type A utility to print segment tree with data type of int.
- Parameters
-
tree | pointer to segment tree |
177 char *base = (
char *)tree->
root;
179 for (; i < 2 * tree->
length - 1; i++)
181 printf(
"%d ", *(
int *)(base + i * tree->
elem_size));
◆ segment_tree_query()
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].
- Parameters
-
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 |
108 memcpy(res, tree->
identity, elem_size);
110 char *root = (
char *)tree->
root;
117 tree->
combine(res, root + l * elem_size, res);
121 tree->
combine(res, root + r * elem_size, res);
◆ segment_tree_update()
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.
- Parameters
-
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 |
82 index = index + tree->
length - 1;
83 char *base = (
char *)tree->
root;
84 char *t = base + index * elem_size;
85 memcpy(t, val, elem_size);
88 index = ((index - 1) >> 1);
89 tree->
combine(base + (2 * index + 1) * elem_size,
90 base + (2 * index + 2) * elem_size,
91 base + index * elem_size);
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Test RMQ Testing Segment tree using Range Minimum Queries.
- Returns
- void
207 int32_t arr[10] = {1, 0, 3, 5, 7, 2, 11, 6, -2, 8};
208 int32_t identity = __INT32_MAX__;
216 assert(result == -2);
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...
Definition: segment_tree.c:194
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 acco...
Definition: segment_tree.c:79
void * root
the root of formed segment tree
Definition: segment_tree.c:40
void * identity
identity element for combine function
Definition: segment_tree.c:41
combine_function combine
the function to be used to combine two node's data to form parent's data
Definition: segment_tree.c:47
static void test()
Test RMQ Testing Segment tree using Range Minimum Queries.
Definition: segment_tree.c:205
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 d...
Definition: segment_tree.c:140
void segment_tree_dispose(segment_tree *tree)
Dispose Segment Tree Frees all heap memory accquired by segment tree.
Definition: segment_tree.c:162
void func(int sockfd)
Continuous loop to send and receive over the socket.
Definition: client.c:37
void segment_tree_build(segment_tree *tree)
Builds a Segment tree It is assumed that leaves of tree already contains data.
Definition: segment_tree.c:55
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 ...
Definition: segment_tree.c:105
size_t elem_size
size in bytes of each data element
Definition: segment_tree.c:42
size_t length
total size of array which segment tree represents
Definition: segment_tree.c:43
This structures holds all the data that is required by a segment tree.
Definition: segment_tree.c:39