Class: RBTree

Inherits:
MultiRBTree show all
Defined in:
ext/archipelago_rbtree.c,
ext/archipelago_rbtree.c

Overview

RBTree is a sorted associative collection using Red-Black Tree as the internal data structure. The elements of RBTree are ordered and the interface is the almost same as Hash, so simply you can consider RBTree sorted Hash.

Red-Black Tree is a kind of binary tree that automatically balances by itself when a node is inserted or deleted. Thus the complexity for insert, search and delete is O(log N) in expected and worst case. On the other hand the complexity of Hash is O(1). Because Hash is unordered the data structure is more effective than Red-Black Tree as an associative collection.

The interface of RBTree is the almost same as Hash although there are some limitations.

  • While iterating (e.g. in RBTree#each block), RBTree is unmodifiable.

  • Comparison is done using <=> method of key objects. So all types of keys in RBTree should be comparable each other or an arbitrary Proc might be set by RBTree#readjust.

RBTree has a few searching methods that Hash doesn’t have. They are RBTree#lower_bound, RBTree#upper_bound and RBTree#bound. See document of each method for details.

Pretty printing is available for RBTree by using pp.rb. The output of pp is easier than p to read. Just call Kernel#pp for the object.

MultiRBTree that allows duplicates of keys is also available.

Method Summary

Methods inherited from MultiRBTree

#==, [], #[], #[]=, #_dump, _load, #bound, #clear, #clone, #cmp_proc, #default, #default=, #default_proc, #delete, #delete_if, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #first, #has_key?, #has_value?, #include?, #index, #initialize, #initialize_copy, #inspect, #invert, #key?, #keys, #last, #length, #lower_bound, #member?, #merge, #merge!, new, #pop, #pretty_print, #pretty_print_cycle, #readjust, #reject, #reject!, #replace, #reverse_each, #select, #shift, #size, #store, #to_a, #to_hash, #to_rbtree, #to_s, #update, #upper_bound, #value?, #values, #values_at

Constructor Details

This class inherits a constructor from MultiRBTree