Core methods for pure python trees implementation
The methods for an immutable Tree class.
-
breadth()
- Breadth first traversal of a tree.
-
child(index=0)
- Returns nth child (by default first), is it exists
-
childs()
- Returns an iterator on all childs of self, or an empty iterator if self has no childs
-
copy(cls=None)
- Shallow copy of a tree.
An extra class argument can be given to copy with a different
(tree) class. No checks are made on the class.
-
debug(depth=0)
- Returns an detailed representation of the tree fro debug purposes
-
depth()
- Depth of self, the distance to self’s root
-
dist(element, **kwargs)
- Returns distance from self to element, 0 means self==element, None if no path exists
up keyword set to True means it will search ascendants(parent and parents of parent) self for element, default is False
down keyword set to True means it will search descendants(childs and childs of childs) of self for element, default is False
If neither up nor down is specified, will search both directions
signed keyword set to true means returns negative distance when element is upwards self, positive when it’s downwards
-
formatted(returnList=False)
- Returns an indented string representation of the tree
-
get(value, default=())
- Identical to the __getitem__ method but will return a default value instead of raising KeyError
if nor result is found
-
hasChilds()
-
height()
- Get maximum downward depth (distance from element to furthest leaf downwards of it) of the tree
-
isElement()
-
issubtree(other)
-
leaves()
- Get an iterator on all leaves under self
-
level(dist=0)
- Get an iterator on all elements at the specified distance of self, negative distance means up, positive means down
-
next
- Next tree in the siblings order, or None is self doesn’t have siblings
-
parent
- The parent tree of that tree, or None if tree isn’t a subtree
-
parents()
- Returns an iterator on path from element to top root, starting with first parent, empty iterator means self is root
-
path(element=None, **kwargs)
- Returns an iterator of the path to specified element if found, including starting element,
empty iterator means no path found.
For trees where duplicate values are allowed, shortest path to an element of this value is returned.
element can be an ancestor or a descendant of self, if no element is specified, will return path from self’s root to self
up keyword set to True means it will search ascendants(parent and parents of parent) self for element, default is False
down keyword set to True means it will search descendants(childs and childs of childs) of self for element, default is False
If neither up nor down is specified, will search both directions
order keyword defines in what order the path will be returned and can be set to ‘top’, ‘bottom’ or ‘self’, order by default is ‘top’
‘top’ means path will be returned from ancestor to descendant
‘bottom’ means path will be returned from descendant to ancestor
‘self’ means path will be returned from self to element
-
postorder()
- Postorder traversal of a tree.
-
preorder()
- The standard preorder traversal iterator.
-
root()
- Root node of self, if self is a subtree, will travel up to top most node of containing tree
-
siblings()
- Returns an iterator on self siblings, not including self and starting with self next sibling,
if self has no siblings (self has no parent or is unique child) then returns an empty iterator
-
size()
- Returns the number of elements (nodes) in the tree.
-
top(index=0)
- The nth top node of self (by default first)
-
tops()
- Iterator on the top nodes of self, the subtrees that have no parent in self,
will yield only self if self isn’t a forest
-
value
- Value of the top element of that tree
-
view()
- Shortcut for print(self.formatted())