The avltree module provides a Tree class. A tree object is much like a dictionary objects, it maps keys to values. But instead of the keys having to be hashable they have to be comparable to each other. Care must be taken not to mutate objects that are currently used as a key in a tree. Use of immutable objects as keys is recommended.
[iterable]) |
The following operations are defined on trees (where a and b are trees, k is a key, and v and x are arbitrary objects):
Operation | Result | Notes |
---|---|---|
len(a) |
the number of items in a | |
a[k] |
the value of a with key k | (1) |
a[k1:k2:step] |
returns an iterator over the tree's keys | (3) |
a[k] = v |
set a[k] to v |
|
del a[k] |
remove a[k] from a |
(1) |
a.clear() |
remove all items from a |
|
a.copy() |
a (shallow) copy of a |
|
a.has_key(k) |
True if a has a key k, else False |
|
k |
Equivalent to a.has_key(k) | |
k not in a |
Equivalent to not a.has_key(k) |
|
a.items(k1,k2,step) |
a copy of a's list of (key, value) pairs | (3) |
a.keys(k1,k2,step) |
a copy of a's list of keys | (3) |
a.update([b]) |
updates (and overwrites) key/value pairs from b | (9) |
a.fromkeys(seq[, value]) |
Creates a new tree with keys from seq and values set to value | (7) |
a.values(k1,k2,step) |
a copy of a's list of values | (3) |
a.get(k[, x]) |
a[k] if k in a ,
else x |
(4) |
a.setdefault(k[, x]) |
a[k] if k in a ,
else x (also setting it) |
(5) |
a.pop(k[, x]) |
a[k] if k in a ,
else x (and remove k) |
(8) |
a.popitem() |
remove and return the (key, value) pair with the smallest key | (6) |
a.iteritems(k1,k2,step) |
return an iterator over (key, value) pairs | (3) |
a.iterkeys(k1,k2,step) |
return an iterator over the tree's keys | (3) |
a.itervalues(k1,k2,step) |
return an iterator over the tree's values | (3) |
Notes:
(value, key)
pairs using zip():
"pairs = zip(a.values(), a.keys())". The same
relationship holds for the iterkeys() and
itervalues() methods: "pairs = zip(a.itervalues(),
a.iterkeys())" provides the same value for pairs
.
Another way to create the same list is "pairs = [(v, k) for (k,
v) in a.iteritems()]".
The parameters in these functions are start, stop and step. All produces values will correspond with keys that are between start (included) and stop (not included). They will be produced in order except if the step is negative when they will be produced in reverse order.
None
is returned.
None
.