Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::FixedShapeBinaryTree Class Reference

#include <fixed_shape_binary_tree.h>

Public Member Functions

 FixedShapeBinaryTree (LeafIndex num_leaves)
 
int StorageSize () const
 
TreeNodeIndex HighestIntermediateNodeIndex () const
 If you want to use a different storage for intermediate nodes and leaves.
 
TreeNodeIndex HighestNodeIndex () const
 
bool IsLeaf (TreeNodeIndex node) const
 
TreeNodeIndex Root () const
 
TreeNodeIndex FirstLeafNode () const
 
TreeNodeIndex LastLeafNode () const
 
TreeNodeIndex LeftChild (TreeNodeIndex node) const
 
TreeNodeIndex RightChild (TreeNodeIndex node) const
 
TreeNodeIndex Parent (TreeNodeIndex node) const
 
TreeNodeIndex Sibling (TreeNodeIndex node) const
 
LeafIndex LeafValue (TreeNodeIndex node) const
 
int Depth (TreeNodeIndex node) const
 Zero for the root.
 
std::pair< LeafIndex, LeafIndex > GetInterval (TreeNodeIndex node) const
 
TreeNodeIndex GetNodeStartOfRange (LeafIndex first_leaf, LeafIndex last_leaf) const
 
TreeNodeIndex GetNodeEndOfRange (LeafIndex first_leaf, LeafIndex last_leaf) const
 
template<typename TypeWithPushBack>
void PartitionIntervalIntoNodes (LeafIndex first_leaf, LeafIndex last_leaf, TypeWithPushBack *result) const
 
TreeNodeIndex GetLeaf (LeafIndex value) const
 

Detailed Description

An abstract representation of a binary tree that can hold integers in the range [0, num_leaves - 1] and has a depth of exactly 1+ceil(log2(num_leaves)). For example, a FixedShapeBinaryTree(5) can be represented by: [0, 4] / \ / \ / \ [0, 3] [4, 4] / \ / \ / \ / \ [0, 1] [2, 3] [4, 4] [-1, -1] / \ / \ / \ / \ The most common use of this class is to have a concrete binary tree by defining its storage like: StrongVector<TreeNodeIndex, Val> tree(abstract_tree.StorageSize());

Besides the classical binary tree structure of left and right children, this class provides an API to inspect and search the intermediate nodes by their interval values.

Definition at line 51 of file fixed_shape_binary_tree.h.

Constructor & Destructor Documentation

◆ FixedShapeBinaryTree()

operations_research::FixedShapeBinaryTree::FixedShapeBinaryTree ( LeafIndex num_leaves)
inlineexplicit

Definition at line 53 of file fixed_shape_binary_tree.h.

Member Function Documentation

◆ Depth()

int operations_research::FixedShapeBinaryTree::Depth ( TreeNodeIndex node) const
inline

Zero for the root.

Definition at line 110 of file fixed_shape_binary_tree.h.

◆ FirstLeafNode()

TreeNodeIndex operations_research::FixedShapeBinaryTree::FirstLeafNode ( ) const
inline

Definition at line 73 of file fixed_shape_binary_tree.h.

◆ GetInterval()

std::pair< LeafIndex, LeafIndex > operations_research::FixedShapeBinaryTree::GetInterval ( TreeNodeIndex node) const
inline

Will return [0, num_leaves - 1] for the root, [x, x] for a leaf with x and the range of all the descendants of a node for intermediate nodes.

Definition at line 116 of file fixed_shape_binary_tree.h.

◆ GetLeaf()

TreeNodeIndex operations_research::FixedShapeBinaryTree::GetLeaf ( LeafIndex value) const
inline

Definition at line 266 of file fixed_shape_binary_tree.h.

◆ GetNodeEndOfRange()

TreeNodeIndex operations_research::FixedShapeBinaryTree::GetNodeEndOfRange ( LeafIndex first_leaf,
LeafIndex last_leaf ) const
inline

Given a range of values, return the largest node in the tree associated to an interval [int_begin, int_end] that satisfies:

  • int_end == first_leaf
  • int_begin >= last_leaf. For example, GetNodeEndOfRange(0, largest_leaf_index) = Root().

This corresponds to a last node (including all its descendants) to do a DFS traversal to cover all intervals fully contained in the range [begin, end].

To see how high we can go on the tree we need to check the two rules:

  • we need to end at last_leaf, so we need to know which power of two divides last_leaf+1.
  • the interval needs to be not greater than last_leaf - first_leaf. If last_leaf - first_leaf is zero it must be a leaf, if it is one it can be one step above, etc).

Definition at line 186 of file fixed_shape_binary_tree.h.

◆ GetNodeStartOfRange()

TreeNodeIndex operations_research::FixedShapeBinaryTree::GetNodeStartOfRange ( LeafIndex first_leaf,
LeafIndex last_leaf ) const
inline

Given a range of leaf indexes [first_leaf, last_leaf], return the largest node in the tree associated to an interval [int_begin, int_end] that satisfies:

  • int_begin == first_leaf
  • int_end <= last_leaf. For example, GetNodeStartOfRange(0, num_leaves - 1) = Root().

This corresponds to a starting node to do a DFS traversal (including all its children) to cover all intervals fully contained in the range [begin, end].

Since we truncate the intervals to the largest_leaf_index_, this is equivalent on the full binary tree to look for the largest possible value.

To see how high we can go on the tree we need to check the two rules:

  • we need to start at first_leaf, so we need to know which power of two divides first_leaf (odd are leaves, divisible by 2 but not by 4 are right above the leaves, etc).
  • the interval needs to be not greater than last_leaf - first_leaf. If last_leaf - first_leaf is zero it must be a leaf, if it is one it can be one step above, etc).

Definition at line 142 of file fixed_shape_binary_tree.h.

◆ HighestIntermediateNodeIndex()

TreeNodeIndex operations_research::FixedShapeBinaryTree::HighestIntermediateNodeIndex ( ) const
inline

If you want to use a different storage for intermediate nodes and leaves.

Definition at line 63 of file fixed_shape_binary_tree.h.

◆ HighestNodeIndex()

TreeNodeIndex operations_research::FixedShapeBinaryTree::HighestNodeIndex ( ) const
inline

Definition at line 67 of file fixed_shape_binary_tree.h.

◆ IsLeaf()

bool operations_research::FixedShapeBinaryTree::IsLeaf ( TreeNodeIndex node) const
inline

Definition at line 69 of file fixed_shape_binary_tree.h.

◆ LastLeafNode()

TreeNodeIndex operations_research::FixedShapeBinaryTree::LastLeafNode ( ) const
inline

Definition at line 77 of file fixed_shape_binary_tree.h.

◆ LeafValue()

LeafIndex operations_research::FixedShapeBinaryTree::LeafValue ( TreeNodeIndex node) const
inline

Definition at line 101 of file fixed_shape_binary_tree.h.

◆ LeftChild()

TreeNodeIndex operations_research::FixedShapeBinaryTree::LeftChild ( TreeNodeIndex node) const
inline

Definition at line 81 of file fixed_shape_binary_tree.h.

◆ Parent()

TreeNodeIndex operations_research::FixedShapeBinaryTree::Parent ( TreeNodeIndex node) const
inline

Definition at line 91 of file fixed_shape_binary_tree.h.

◆ PartitionIntervalIntoNodes()

template<typename TypeWithPushBack>
void operations_research::FixedShapeBinaryTree::PartitionIntervalIntoNodes ( LeafIndex first_leaf,
LeafIndex last_leaf,
TypeWithPushBack * result ) const
inline

Given an interval [first_leaf, last_leaf], return O(log n) ordered disjoint nodes of the tree that cover the interval. The time complexity is O(log n).

We just moved up.

We just moved down.

Definition at line 219 of file fixed_shape_binary_tree.h.

◆ RightChild()

TreeNodeIndex operations_research::FixedShapeBinaryTree::RightChild ( TreeNodeIndex node) const
inline

Definition at line 86 of file fixed_shape_binary_tree.h.

◆ Root()

TreeNodeIndex operations_research::FixedShapeBinaryTree::Root ( ) const
inline

Definition at line 71 of file fixed_shape_binary_tree.h.

◆ Sibling()

TreeNodeIndex operations_research::FixedShapeBinaryTree::Sibling ( TreeNodeIndex node) const
inline

Definition at line 96 of file fixed_shape_binary_tree.h.

◆ StorageSize()

int operations_research::FixedShapeBinaryTree::StorageSize ( ) const
inline

Definition at line 60 of file fixed_shape_binary_tree.h.


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