-
Notifications
You must be signed in to change notification settings - Fork 0
/
splay_tree.h
94 lines (75 loc) · 2.24 KB
/
splay_tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdint.h>
#include <math.h>
#include <stdbool.h>
#include "augmentations.h"
typedef int64_t k_t; // key type
typedef int64_t v_t; // value type
typedef uint64_t s_t; // size type
// macros for convenience
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define INF 4e18
// storing augmentations
typedef struct Augmentations {
#ifdef SUBTREE_MIN
v_t min;
#endif
#ifdef SUBTREE_MAX
v_t max;
#endif
#ifdef SUBTREE_SUM
v_t sum;
#endif
#ifdef SUBTREE_SIZE
v_t size;
#endif
#ifdef SUBTREE_INCREMENT
v_t lazy;
#endif
} Augmentations;
// splay tree node
typedef struct Node {
struct Node *parent, *left, *right;
k_t key;
v_t value;
int is_start;
struct Augmentations augments;
} Node;
// create a splay tree node
Node* make_node(k_t key, v_t value, int is_start);
// recursive helper function for freeing ETT nodes
void delete_recursive(Node* node);
// do a range query
Augmentations query(Node* start, Node* end);
#ifdef POINT_UPDATE
// update node value
void update_node(Node* node, v_t new_value);
#endif
// update whether node is the start node
void update_node_start(Node* node, int is_start);
#ifdef SUBTREE_INCREMENT
// increment the values of the nodes in a given range
void update_range(Node* start, Node* end, v_t delta);
#endif
// bring node to root using rotations
void splay(Node* node);
// returns root of the tree
Node* root(Node* node);
// return min of tree rooted at node
// precondition: node is at root
Node* find_min(Node* node);
// return max of tree rooted at node
// precondition: node is at root
Node* find_max(Node* node);
// split tree at node, where node ends up in the right half
// returns root of left tree
Node* split_left(Node* node);
// split tree at node, where node ends up in the left half
// returns root of right tree
Node* split_right(Node* node);
// merge two trees
// precondition: node1 and node2 are roots of their respective trees
// postcondition: node1's tree is concatenated left of node2's tree
Node* merge(Node* node1, Node* node2);
// print in-order traversal of tree rooted at node
void print(Node* node);