| Home | Trees | Indices | Help |
|---|
|
|
object --+
|
MetricTree
Base class for metric space indexes which have tree-like properties. It's implementation is not complete enough to make it actually usable, but it should make it quite easy to implement other indexing and searching strategies. In an ideal case, all you have to do is to derive from this class, implement the methods construct, _get_child_candidates and optionally insert and make children an attribute or property returning all child nodes of the current node. If you need an __init__ method, you should call super(YourClass, self).__init__(objects, func, parent) in its end as well.
Instances of this class (and its subclasses, of course) are supposed to be recursive data structures. This means that they may be treated like the root of a tree whether they have a parent or not. This means every node has:
None for root nodes)Note that in many (most? some?) cases, implementors don't have to fiddle with these values directly. Instead, there are some helper methods like _incr_node_counter and _incr_size that may be called at appropriate times. If in doubt, just implement what you strictly need first and then take a look whether node, size and height counting works as expected (and whether you care).
As a bonus, this class implements some of python's magic methods so that you can iterate over its instances' content and use the in operator to test for the existence of an object using the distance function.
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
Inherited from |
|||
|
|||
_parent = NoneThe parent of this node. |
|||
_values = list()A list of values in this subtree. |
|||
_size = 0The number of objects in this subtree. |
|||
_func = 0The distance function used for indexing and searching. |
|||
_height = 0
|
|||
_num_nodes = 0
|
|||
parent = property(__parent, __set_parent)
|
|||
|
|||
|
Inherited from |
|||
|
|||
|
Return a list of all objects in this subtree whose distance to
If this metric tree is empty, an empty list is returned, regardless of whether this tree has been previously constructed or not. If calling the distance function fails for any reason, UnindexableObjectError will be raised. |
Perform a k-nearest-neighbour search and return a sorted list of
at most num objects together with their distance to the
query object ((object, distance) tuples). Sorting is done by
the distance (ascending). Of course, For obvious reasons, this method cannot return more objects than are currently present in the tree. That means, if num > len(self), only len(self) pairs of `(object, distance) will be returned. If this metric tree is empty, an empty list is returned, regardless of whether this tree has been previously constructed or not. If several objects in this tree share the same maximum distance
to If calling the distance function fails for any reason, UnindexableObjectError will be raised. |
|
(Re)Index this space with the given objects using the distance function func. Previous contents will be discarded. objects has to be a sequence or an iterable. The distance function func needs to be applicable to all objects contained in objects. If calling the distance function fails for any reason, UnindexableObjectError will be raised. |
Insert a single object into the metric tree. Returns self, i.e. the tree itself. This method may not be implemented by all subclasses of
MetricTree since not all data structures allow to do this
efficiently. If calling the distance function fails for any reason, UnindexableObjectError will be raised. |
|
|
A sequence of this node's children. The possible number of children per node is implementation-dependent. Leaf nodes return an empty sequence. |
A sequence of this node's children. The possible number of children per node is implementation-dependent. Leaf nodes return an empty sequence. |
The number of nodes in this tree. This may be different from the number of objects contained in this tree in cases where two or more of these objects are considered equal by the distance function in use (i.e., for two objects p and q, calling self._func(p, q) returns 0 and when the tree is empty, i.e. there is one node (the root node) but it doesn't contain any values. |
The number of nodes in this tree. This may be different from the number of objects contained in this tree in cases where two or more of these objects are considered equal by the distance function in use (i.e., for two objects p and q, calling self._func(p, q) returns 0 and when the tree is empty, i.e. there is one node (the root node) but it doesn't contain any values. |
The height of this tree. Empty trees have a height of 0, trees containing one or more objects have a height >= 1. |
The height of this tree. Empty trees have a height of 0, trees containing one or more objects have a height >= 1. |
Set the parent of this node. Parent references are stored as "weak references" to avoid circular references which the garbage collector cannot dissolve by itself. |
Increment the size counter for this node and all its parents recursively. Should be called whenever an object is inserted into the tree. |
Increment the node counter for this node and all its parents recursively. Should be called whenever a new child of this node is created. |
|
Apply this node's distance function to the given object and one of this node's values. Raises UnindexableObjectError when distance computation fails. |
A generator that yields all objects in this node and its children by doing recursive pre-order traversal. Implementors might choose to use another traversal method which better suits their data structure. Note that objects are returned in no specific order. This implementation will always return objects in the same order
as long as the tree's content does not change and the
implementation of children always returns the children in the
same order. If children is not implemented at all,
|
A generator that yields all objects in this node and its children by doing recursive pre-order traversal. Implementors might choose to use another traversal method which better suits their data structure. Note that objects are returned in no specific order. This implementation will always return objects in the same order
as long as the tree's content does not change and the
implementation of children always returns the children in the
same order. If children is not implemented at all,
|
A generator that yields all nodes in this subtree by doing recursive pre-order traversal. Implementors might choose to use another traversal method which better suits their data structure. Note that objects are returned unordered. This implementation will always return nodes in the same order
as long as the tree's content does not change and the
implementation of children always returns the children in the
same order. If children is not implemented at all,
|
Search for objects with a distance of zero to Note that this does not check for object identity! Instead, the definition of equality is delegated to the distance function in use. |
repr(x)
|
|
|||
_valuesA list of values in this subtree. All objects in this list are considered equal by the distance function in use.
|
_funcThe distance function used for indexing and searching. It has to accept any two objects from the list of objects you are indexing as arguments and return a non-negative integer (or long).
|
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Thu Oct 30 21:29:54 2008 | http://epydoc.sourceforge.net |