38 #ifndef FCL_INTERVAL_TREE_INL_H 39 #define FCL_INTERVAL_TREE_INL_H 50 class IntervalTree<double>;
60 nil->stored_interval =
nullptr;
66 root->stored_interval =
nullptr;
69 recursion_node_stack_size = 128;
71 recursion_node_stack_top = 1;
72 recursion_node_stack[0].start_node =
nullptr;
80 std::deque<IntervalTreeNode<S>*> nodes_to_free;
86 nodes_to_free.push_back(x->
left);
90 nodes_to_free.push_back(x->
right);
94 while( nodes_to_free.size() > 0)
96 x = nodes_to_free.back();
97 nodes_to_free.pop_back();
100 nodes_to_free.push_back(x->
left);
104 nodes_to_free.push_back(x->
right);
111 free(recursion_node_stack);
115 template <
typename S>
140 template <
typename S>
164 template <
typename S>
182 if((y == root) || (y->
key > z->
key))
189 template <
typename S>
200 template <
typename S>
260 root->left->red =
false;
265 template <
typename S>
270 if(nil != (y = x->
right))
272 while(y->
left != nil)
284 if(y == root)
return nil;
290 template <
typename S>
295 if(nil != (y = x->
left))
297 while(y->
right != nil)
306 if(y == root)
return nil;
315 template <
typename S>
320 recursivePrint(x->
left);
322 recursivePrint(x->
right);
327 template <
typename S>
330 recursivePrint(root->left);
334 template <
typename S>
340 while((!x->
red) && (root_left_node != x))
409 template <
typename S>
418 template <
typename S>
427 if(left != nil)
return left;
429 if(right != nil)
return right;
436 template <
typename S>
443 y= ((z->
left == nil) || (z->
right == nil)) ? z : getSuccessor(z);
488 if(!(y->
red)) deleteFixup(x);
492 return node_to_delete;
497 template <
typename S>
511 template <
typename S>
514 std::deque<SimpleInterval<S>*> result_stack;
516 bool run = (x != nil);
525 recursion_node_stack[current_parent].try_right_branch =
true;
529 if(recursion_node_stack_top == recursion_node_stack_size)
531 recursion_node_stack_size *= 2;
533 if(recursion_node_stack ==
nullptr)
536 recursion_node_stack[recursion_node_stack_top].start_node = x;
537 recursion_node_stack[recursion_node_stack_top].try_right_branch =
false;
538 recursion_node_stack[recursion_node_stack_top].parent_index = current_parent;
539 current_parent = recursion_node_stack_top++;
546 while((!run) && (recursion_node_stack_top > 1))
548 if(recursion_node_stack[--recursion_node_stack_top].try_right_branch)
550 x=recursion_node_stack[recursion_node_stack_top].start_node->
right;
551 current_parent=recursion_node_stack[recursion_node_stack_top].parent_index;
552 recursion_node_stack[current_parent].try_right_branch =
true;
void deleteFixup(IntervalTreeNode< S > *node)
void fixupMaxHigh(IntervalTreeNode< S > *node)
Travels up to the root fixing the max_high fields after an insertion or deletion. ...
Class describes the information needed when we take the right branch in searching for intervals but p...
IntervalTreeNode< S > * getSuccessor(IntervalTreeNode< S > *node) const
Get the successor of a given node.
bool red
red or black node: if red = false then the node is black
void leftRotate(IntervalTreeNode< S > *node)
left rotation of tree node
bool overlap(S a1, S a2, S b1, S b2)
returns 1 if the intervals overlap, and 0 otherwise
IntervalTreeNode< S > * insert(SimpleInterval< S > *new_interval)
Insert one node of the interval tree.
void recursivePrint(IntervalTreeNode< S > *node) const
recursively print a subtree
void print(IntervalTreeNode *left, IntervalTreeNode *right) const
Print the interval node information: set left = nil and right = root.
Interval trees implemented using red-black-trees as described in the book Introduction_To_Algorithms_...
SimpleInterval< S > * deleteNode(IntervalTreeNode< S > *node)
Delete one node of the interval tree.
IntervalTreeNode< S > * recursiveSearch(IntervalTreeNode< S > *node, SimpleInterval< S > *ivl) const
recursively find the node corresponding to the interval
void rightRotate(IntervalTreeNode< S > *node)
right rotation of tree node
std::deque< SimpleInterval< S > * > query(S low, S high)
Return result for a given query.
IntervalTreeNode< S > * getPredecessor(IntervalTreeNode< S > *node) const
get the predecessor of a given node
void recursiveInsert(IntervalTreeNode< S > *node)
Inserts node into the tree as if it were a regular binary tree.
The node for interval tree.
SimpleInterval< S > * stored_interval
interval stored in the node
IntervalTreeNode * parent
void print() const
Print the whole interval tree.