TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
range_queries::heavy_light_decomposition::Tree< X > Class Template Reference

A Basic Tree, which supports binary lifting. More...

Inheritance diagram for range_queries::heavy_light_decomposition::Tree< X >:
[legend]
Collaboration diagram for range_queries::heavy_light_decomposition::Tree< X >:
[legend]

Public Member Functions

 Tree (int nodes)
 Class parameterized constructor, resizes the and initializes the data members.
void add_edge (const int u, const int v)
 Adds an undirected edge from node u to node v in the tree.
void change_root (int new_root)
 Set the root for the tree.
void set_node_val (const std::vector< X > &node_val)
 Set the values for all the nodes.
void init ()
 This function must be called after the tree adjacency list and node values are populated The function initializes the required parameters, and populates the segment tree.
void lift (int *const p, int dist)
 The function lifts a node, k units up the tree. The lifting is done in place, and the result is stored in the address pointed by p.
int kth_ancestor (int p, const int &dist)
 The function returns the kth ancestor of a node.
int lca (int a, int b)
 The function returns the least common ancestor of two nodes.

Private Member Functions

void dfs_size (int u, int p=-1)
 Utility function to compute sub-tree sizes.
void dfs_lca (int u, int p=-1)
 Utility function to populate the t_par vector.

Private Attributes

std::vector< std::list< int > > t_adj
 an adjacency list to stores the tree edges
const int t_nodes
 number of nodes
const int t_maxlift
 maximum possible height of the tree
std::vector< std::vector< int > > t_par
 a matrix to store every node's 2^kth parent
std::vector< int > t_depth
 a vector to store the depth of a node,
std::vector< int > t_size
 a vector to store the subtree size rooted at node
int t_root
 the root of the tree
std::vector< X > t_val
 values of nodes

Friends

template<typename T>
class HLD

Detailed Description

template<typename X>
class range_queries::heavy_light_decomposition::Tree< X >

A Basic Tree, which supports binary lifting.

Template Parameters
thedata type of the values stored in the tree nodes

Deleting the default constructor An instance can only be created with the number of nodes Defaults: t_node indexing are zero based t_root is 0 depth of root_node is 0 Supports: lift :- lift a node k units up the tree kth_ancestor :- returns the kth ancestor lca :- returns the least common ancestor

Definition at line 79 of file heavy_light_decomposition.cpp.

Constructor & Destructor Documentation

◆ Tree()

template<typename X>
range_queries::heavy_light_decomposition::Tree< X >::Tree ( int nodes)
inlineexplicit

Class parameterized constructor, resizes the and initializes the data members.

Parameters
nodesthe total number of nodes in the tree

Definition at line 142 of file heavy_light_decomposition.cpp.

143 : t_nodes(nodes), t_maxlift(static_cast<int>(floor(log2(nodes))) + 1) {
144 /* Initialize and resize all the vectors */
145 t_root = 0; /* Default */
146 t_adj.resize(t_nodes);
148 t_depth.assign(t_nodes, 0);
149 t_size.assign(t_nodes, 1);
150 t_val.resize(t_nodes);
151 }
A Basic Tree, which supports binary lifting.
std::vector< int > t_depth
a vector to store the depth of a node,
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
const int t_maxlift
maximum possible height of the tree
std::vector< int > t_size
a vector to store the subtree size rooted at node

Member Function Documentation

◆ add_edge()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::add_edge ( const int u,
const int v )
inline

Adds an undirected edge from node u to node v in the tree.

Parameters
uthe node where the edge is from
vthe node where the edge is to
Returns
void

Definition at line 159 of file heavy_light_decomposition.cpp.

159 {
160 t_adj[u].push_back(v);
161 t_adj[v].push_back(u);
162 }

◆ change_root()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::change_root ( int new_root)
inline

Set the root for the tree.

Parameters
new_rootthe new root
Returns
void

Definition at line 169 of file heavy_light_decomposition.cpp.

169{ t_root = new_root; }

◆ dfs_lca()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::dfs_lca ( int u,
int p = -1 )
inlineprivate

Utility function to populate the t_par vector.

Parameters
ucurrent dfs node
pthe parent of node u
Returns
void

Definition at line 118 of file heavy_light_decomposition.cpp.

118 {
119 t_par[u][0] = p;
120 if (p != -1) {
121 t_depth[u] = 1 + t_depth[p];
122 }
123 for (int k = 1; k < t_maxlift; k++) {
124 if (t_par[u][k - 1] != -1) {
125 t_par[u][k] = t_par[t_par[u][k - 1]][k - 1];
126 }
127 }
128
129 for (const int &v : t_adj[u]) {
130 if (v ^ p) {
131 dfs_lca(v, u);
132 }
133 }
134 }
void dfs_lca(int u, int p=-1)
Utility function to populate the t_par vector.

◆ dfs_size()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::dfs_size ( int u,
int p = -1 )
inlineprivate

Utility function to compute sub-tree sizes.

Parameters
ucurrent dfs node
pthe parent of node
u
Returns
void

Definition at line 103 of file heavy_light_decomposition.cpp.

103 {
104 for (const int &v : t_adj[u]) {
105 if (v ^ p) {
106 dfs_size(v, u);
107 t_size[u] += t_size[v];
108 }
109 }
110 }
void dfs_size(int u, int p=-1)
Utility function to compute sub-tree sizes.

◆ init()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::init ( )
inline

This function must be called after the tree adjacency list and node values are populated The function initializes the required parameters, and populates the segment tree.

Returns
void

Definition at line 188 of file heavy_light_decomposition.cpp.

188 {
189 assert(t_nodes > 0);
192 }

◆ kth_ancestor()

template<typename X>
int range_queries::heavy_light_decomposition::Tree< X >::kth_ancestor ( int p,
const int & dist )
inline

The function returns the kth ancestor of a node.

Parameters
pthe node id whose kth ancestor is to be found
distthe distance to move up the tree
Returns
the kth ancestor of node

Definition at line 220 of file heavy_light_decomposition.cpp.

220 {
221 lift(&p, dist);
222 return p;
223 }
void lift(int *const p, int dist)
The function lifts a node, k units up the tree. The lifting is done in place, and the result is store...

◆ lca()

template<typename X>
int range_queries::heavy_light_decomposition::Tree< X >::lca ( int a,
int b )
inline

The function returns the least common ancestor of two nodes.

Parameters
anode id_1
bnode id_2
Returns
the least common ancestor of node a, and node b

Definition at line 231 of file heavy_light_decomposition.cpp.

231 {
232 assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes);
233 if (t_depth[a] > t_depth[b]) {
234 lift(&a, t_depth[a] - t_depth[b]);
235 }
236 if (t_depth[b] > t_depth[a]) {
237 lift(&b, t_depth[b] - t_depth[a]);
238 }
239 if (a == b) {
240 return a;
241 }
242 for (int k = t_maxlift - 1; k >= 0; k--) {
243 if (t_par[a][k] != t_par[b][k]) {
244 a = t_par[a][k];
245 b = t_par[b][k];
246 }
247 }
248 return t_par[a][0];
249 }

◆ lift()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::lift ( int *const p,
int dist )
inline

The function lifts a node, k units up the tree. The lifting is done in place, and the result is stored in the address pointed by p.

Parameters
pa pointer to the variable that stores the node id
distthe distance to move up the tree
Returns
void

Definition at line 202 of file heavy_light_decomposition.cpp.

202 {
203 for (int k = 0; k < t_maxlift; k++) {
204 if (*p == -1) {
205 return;
206 }
207 if (dist & 1) {
208 *p = t_par[*p][k];
209 }
210 dist >>= 1;
211 }
212 }

◆ set_node_val()

template<typename X>
void range_queries::heavy_light_decomposition::Tree< X >::set_node_val ( const std::vector< X > & node_val)
inline

Set the values for all the nodes.

Parameters
node_vala vector of size n, with all the node values where, n is the number of nodes
Returns
void

Definition at line 177 of file heavy_light_decomposition.cpp.

177 {
178 assert(static_cast<int>(node_val.size()) == t_nodes);
179 t_val = node_val;
180 }

◆ HLD

template<typename X>
template<typename T>
friend class HLD
friend

Definition at line 95 of file heavy_light_decomposition.cpp.

Member Data Documentation

◆ t_adj

template<typename X>
std::vector<std::list<int> > range_queries::heavy_light_decomposition::Tree< X >::t_adj
private

an adjacency list to stores the tree edges

Definition at line 84 of file heavy_light_decomposition.cpp.

◆ t_depth

template<typename X>
std::vector<int> range_queries::heavy_light_decomposition::Tree< X >::t_depth
private

a vector to store the depth of a node,

Definition at line 89 of file heavy_light_decomposition.cpp.

◆ t_maxlift

template<typename X>
const int range_queries::heavy_light_decomposition::Tree< X >::t_maxlift
private

maximum possible height of the tree

Definition at line 86 of file heavy_light_decomposition.cpp.

◆ t_nodes

template<typename X>
const int range_queries::heavy_light_decomposition::Tree< X >::t_nodes
private

number of nodes

Definition at line 85 of file heavy_light_decomposition.cpp.

◆ t_par

template<typename X>
std::vector<std::vector<int> > range_queries::heavy_light_decomposition::Tree< X >::t_par
private

a matrix to store every node's 2^kth parent

Definition at line 88 of file heavy_light_decomposition.cpp.

◆ t_root

template<typename X>
int range_queries::heavy_light_decomposition::Tree< X >::t_root
private

the root of the tree

Definition at line 92 of file heavy_light_decomposition.cpp.

◆ t_size

template<typename X>
std::vector<int> range_queries::heavy_light_decomposition::Tree< X >::t_size
private

a vector to store the subtree size rooted at node

Definition at line 90 of file heavy_light_decomposition.cpp.

◆ t_val

template<typename X>
std::vector<X> range_queries::heavy_light_decomposition::Tree< X >::t_val
private

values of nodes

Definition at line 93 of file heavy_light_decomposition.cpp.


The documentation for this class was generated from the following file: