Core methods for pure python trees implementation
Additionnal methods for a Mutable Tree class.
-
add(element, parent=None, next=None)
- Add an element to self. parent and next element can be specified.
Element will be added as a child of parent, parent can be any element or subtree of self:
if parent is specified as a value there must exactly one match in self or an exception will be raised
if parent is None, element will be added as a sibling of self’s top node(s)
if next is not none, element will be added before next in the childs of parent, else as a last childs
-
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
-
graft(element, parent=None, next=None)
- Attach element to self.
If parent is secified, will be grafted as last child of parent (or before child ‘next’),
if parent is not in self, will raise an exception,
if parent is None will be grafted at top level of self, besides any existing root(s).
If next is specified, self will be grafted before next in the list of parent’s childs,
if next is not in parent’s childs, will raise an exception,
if next is None, self will be grafted as last child under parent.
-
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
-
pop(element)
- Delete top node of self and reparent all it’s subtrees under it’s current parent.
If self was a root all it’s subtrees become separate trees of a forest under the ‘None’ root.
self will now have the new parent as top node
-
postorder()
- Postorder traversal of a tree.
-
preorder()
- The standard preorder traversal iterator.
-
prune(element)
- Ungrafts element from self, with everything under it
-
remove(element)
- Remove element from self, along with everything under it, will raise an exception if element is not in self
-
reroot(element)
- Reroot self so that element is self new top node
-
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.
-
sort(*args)
-
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())