Module rbtree_int_mod
Uses:
module kinds_mod, only: i_k
Types:
public type rb_node_int_links
public type rb_node_int
public type rbtree_int
public type rbtree_int_iterator
Variables:
integer (kind=i_k), private, parameter :: int_variable = 0
integer (kind=i_k), public, parameter :: RBTREE_NOT_FOUND = huge (int_variable)
Subroutines and functions:
private function is_red (node)
private function single_rotate (node, dir)
private function double_rotate (node, dir)
private recursive function validate (node) result (height)
private recursive subroutine print_tree (node, level)
private function create_node (key, value)
private function node_equal (node1, node2)
public subroutine rbtree_int_validate (rbtree, ierr)
public subroutine rbtree_int_print (rbtree)
public function rbtree_int_get (rbtree, key, found)
public subroutine rbtree_int_insert (rbtree, key, value, ierr)
public function rbtree_int_delete (rbtree, key, iter, found)
public subroutine rbtree_int_clear (rbtree)
public function rbtree_int_iterator_first (iter, rbtree)
public function rbtree_int_iterator_last (iter, rbtree)
public function rbtree_int_iterator_next (iter)
public function rbtree_int_iterator_prev (iter)
public function rbtree_int_iterator_set (iter, key)
public function rbtree_int_iterator_key (iter)
public function rbtree_int_iterator_value (iter)
Red-Black binary search tree combined with a doubly-linked list for fast iteration.
The compexity of the insert, delete, and get (i.e. find) routines is logarithmic.
Iterator functions next and previous have constant complexity.
The tree stores integer numbers.
The Red-Black tree is coded following the descripton from Julienne Walker at
http://www.eternallyconfuzzled.com.
Author: Matthias Lieber
Description of Types
rb_node_int_links
public type rb_node_int_links
type (rb_node_int), pointer :: n => NULL ()
end type rb_node_int_links
Helper to build an array of pointers to tree nodes. Only used internally.
rb_node_int
public type rb_node_int
type (rb_node_int_links), dimension (0:1) :: link
type (rb_node_int_links), dimension (0:1) :: list
integer (kind=i_k) :: red = 0
integer (kind=i_k) :: key = 0
integer (kind=i_k) :: value = 0
end type rb_node_int
Components:
link | pointers to left and right children |
list | pointers to previous and next node in sorted list |
red | color of the node, 1 if red, 0 if black |
key | the key of the data |
value | the node value |
A tree node. Only used internally.
rbtree_int
public type rbtree_int
type (rb_node_int), pointer :: root => NULL ()
type (rb_node_int_links), dimension (0:1) :: list
integer (kind=i_k) :: length = 0
end type rbtree_int
Components:
root | pointer to root node |
list | pointers to first and last node in sorted list |
length | number of elements in the tree |
The tree.
rbtree_int_iterator
public type rbtree_int_iterator
type (rbtree_int), pointer :: rbtree
type (rb_node_int), pointer :: cur => NULL ()
end type rbtree_int_iterator
Components:
rbtree | pointer to the tree to iterate |
cur | pointer to the current node |
A tree iterator type.
Description of Subroutines and Functions
is_red
private function is_red (node)
type (rb_node_int), pointer :: node
logical :: is_red
end function is_red
Check if node is red. Internal.
single_rotate
private function single_rotate (node, dir)
type (rb_node_int), pointer :: node
integer (kind=i_k), intent(in) :: dir
type (rb_node_int), pointer :: single_rotate
end function single_rotate
Parameters:
node | pivot node |
dir | direction of rotation, 0 means left, 1 means right |
single_rotate | return value, new root node of the subtree |
Single rotation. Internal.
double_rotate
private function double_rotate (node, dir)
type (rb_node_int), pointer :: node
integer (kind=i_k), intent(in) :: dir
type (rb_node_int), pointer :: double_rotate
end function double_rotate
Parameters:
node | pivot node |
dir | direction of rotation, 0 means left, 1 means right |
double_rotate | return value, new root node of the subtree |
validate
private recursive function validate (node) result (height)
type (rb_node_int), pointer :: node
integer (kind=i_k) :: height
end function validate
Check if tree is a valid red black tree. Internal.
print_tree
private recursive subroutine print_tree (node, level)
type (rb_node_int), pointer :: node
integer (kind=i_k), intent(in) :: level
end subroutine print_tree
Print tree. Internal.
create_node
private function create_node (key, value)
integer (kind=i_k), intent(in) :: key
integer (kind=i_k) :: value
type (rb_node_int), pointer :: create_node
end function create_node
Parameters:
key | the key |
value | pointer to the data the node should hold |
create_node | return value, the new node just created |
Create new node. Internal.
node_equal
private function node_equal (node1, node2)
type (rb_node_int), pointer :: node1
type (rb_node_int), pointer :: node2
integer (kind=i_k) :: node_equal
end function node_equal
Parameters:
node1 | first node to compare |
node2 | second node to compare |
node_equal | return value, 1 if equal, 0 otherwise |
Check if two node pointers are the same node. Internal.
rbtree_int_validate
public subroutine rbtree_int_validate (rbtree, ierr)
type (rbtree_int), intent(in) :: rbtree
integer (kind=i_k), optional, intent(out) :: ierr
end subroutine rbtree_int_validate
Parameters:
rbtree | the red black tree |
ierr | error status |
Check if tree is valid red black tree.
Prints errors to stdout.
rbtree_int_print
public subroutine rbtree_int_print (rbtree)
type (rbtree_int), intent(in) :: rbtree
end subroutine rbtree_int_print
Parameters:
rbtree | the red black tree |
Print the tree to stdout. Useful for debugging.
rbtree_int_get
public function rbtree_int_get (rbtree, key, found)
type (rbtree_int), intent(in) :: rbtree
integer (kind=i_k), intent(in) :: key
logical, optional, intent(out) :: found
integer (kind=i_k) :: rbtree_int_get
end function rbtree_int_get
Parameters:
rbtree | the red black tree |
key | key of the entry to retrieve |
found | .true. if item found |
rbtree_int_get | return value, pointer to requested data or null if not found |
Get a value from the tree. Top-down search without recursion.
rbtree_int_insert
public subroutine rbtree_int_insert (rbtree, key, value, ierr)
type (rbtree_int), intent(inout) :: rbtree
integer (kind=i_k), intent(in) :: key
integer (kind=i_k) :: value
integer (kind=i_k), optional, intent(out) :: ierr
end subroutine rbtree_int_insert
Parameters:
rbtree | the red black tree |
key | key of the entry to insert |
value | pointer to value of the entry to insert |
ierr | optional error status, 0 means ok, 1 means duplicate key |
Insert new entry to the tree. Top-down insertion without recursion.
rbtree_int_delete
public function rbtree_int_delete (rbtree, key, iter, found)
type (rbtree_int), intent(inout) :: rbtree
integer (kind=i_k), intent(in) :: key
type (rbtree_int_iterator), optional, intent(inout) :: iter
logical, optional, intent(out) :: found
integer (kind=i_k) :: rbtree_int_delete
end function rbtree_int_delete
Parameters:
rbtree | the red black tree |
key | key of the entry to delete |
iter | an optional iterator which will be repaired if damaged due to deletion |
found | .true. if item found and deleted |
rbtree_int_delete | return value, the value deleted from list or null if key not found |
Delete entry from the tree. Top-down deletion without recursion.
If an optional iterator is given, it will be repaired when damaged due
deletion. This damage may happen to any iterator of the tree,
so you must do this when deleting entries within a tree iteration!
This damage is due to the delete algorithm, which sometimes swap tree node contents.
In this case, the iterator would point to the wrong node.
CAUTION: Do not delete the current entry of the iterator!
If you still do so, you must re-initialize the iterator.
The following code snipped shows how to safely delete entries within an iteration:
value = rbtree_int_iterator_first(rbiter, rbtree)
do while ( associated(value) )
! get key of current
key = rbtree_int_iterator_key(rbiter)
! this function returns true if we do not longer need this item
useless = is_item_useless(value)
! iterate one item forward
value = rbtree_int_iterator_next(rbiter)
if(useless) then
! delete previous item and fix iterator if necessary
deleted = rbtree_int_delete(rbtree, key, rbiter)
end if
end do
rbtree_int_clear
public subroutine rbtree_int_clear (rbtree)
type (rbtree_int), intent(inout) :: rbtree
end subroutine rbtree_int_clear
Parameters:
rbtree | the red black tree |
Clear the whole tree.
rbtree_int_iterator_first
public function rbtree_int_iterator_first (iter, rbtree)
type (rbtree_int_iterator), intent(inout) :: iter
type (rbtree_int), target, intent(in) :: rbtree
integer (kind=i_k) :: rbtree_int_iterator_first
end function rbtree_int_iterator_first
Parameters:
iter | the iterator |
rbtree | the red black tree |
rbtree_int_iterator_first | return value, the first value in the tree |
Initialize iterator for tree
rbtree,
position on the first element.
rbtree_int_iterator_last
public function rbtree_int_iterator_last (iter, rbtree)
type (rbtree_int_iterator), intent(inout) :: iter
type (rbtree_int), target, intent(in) :: rbtree
integer (kind=i_k) :: rbtree_int_iterator_last
end function rbtree_int_iterator_last
Parameters:
iter | the iterator |
rbtree | the red black tree |
rbtree_int_iterator_last | return value, the last value in the tree |
Initialize iterator for tree
rbtree,
position on the last element.
rbtree_int_iterator_next
public function rbtree_int_iterator_next (iter)
type (rbtree_int_iterator), intent(inout) :: iter
integer (kind=i_k) :: rbtree_int_iterator_next
end function rbtree_int_iterator_next
Parameters:
iter | the iterator |
rbtree_int_iterator_next | return value, the next value |
Iterate to next item in tree.
CAUTION: This routine will intentionally cause a segmentation violation
if the iterator is not valid!
rbtree_int_iterator_prev
public function rbtree_int_iterator_prev (iter)
type (rbtree_int_iterator), intent(inout) :: iter
integer (kind=i_k) :: rbtree_int_iterator_prev
end function rbtree_int_iterator_prev
Parameters:
iter | the iterator |
rbtree_int_iterator_prev | return value, the previous value |
Iterate to previous item in tree.
CAUTION: This routine will intentionally cause a segmentation violation
if the iterator is not valid!
rbtree_int_iterator_set
public function rbtree_int_iterator_set (iter, key)
type (rbtree_int_iterator), intent(inout) :: iter
integer (kind=i_k), intent(in) :: key
integer (kind=i_k) :: rbtree_int_iterator_set
end function rbtree_int_iterator_set
Parameters:
iter | the iterator |
key | key of the entry to search |
rbtree_int_iterator_set | return value, value at the requested position ot null if not found |
Set position of an iterator. Top-down search without recursion.
rbtree_int_iterator_key
public function rbtree_int_iterator_key (iter)
type (rbtree_int_iterator), intent(in) :: iter
integer (kind=i_k) :: rbtree_int_iterator_key
end function rbtree_int_iterator_key
Parameters:
iter | the iterator |
rbtree_int_iterator_key | return value, the key of current node |
Get the key of current node.
CAUTION: This routine will intionally cause a segmentation violation
if the iterator is not valid!
rbtree_int_iterator_value
public function rbtree_int_iterator_value (iter)
type (rbtree_int_iterator), intent(in) :: iter
integer (kind=i_k) :: rbtree_int_iterator_value
end function rbtree_int_iterator_value
Parameters:
iter | the iterator |
rbtree_int_iterator_value | return value, the value of current node or null if current node is null |
Get value of current node.