Tree¶
Represents a simple tree.
Created on 6 Mar 2014
@author: paulross
-
class
cpip.util.Tree.
DuplexAdjacencyList
¶ Represents a set of parent/child relationships (and their inverse) as Adjacency Lists.
-
__init__
()¶ Constructor.
-
__weakref__
¶ list of weak references to the object (if defined)
-
_add
(theMap, k, v)¶ Adds the key/value to the existing map.
Parameters: - theMap (
dict({})
) – Existing map. - k (
str
) – Key. - v (
str
) – Value.
Returns: NoneType
- theMap (
-
_treeFromEither
(theObj, theMap)¶ Creates a tree from the object.
Parameters: - theObj (
str
) – The object to create a tree from. - theMap (
dict({str : [list([str])]})
) – The map of str/str.
Returns: cpip.util.Tree.Tree
– The final tree.- theObj (
-
_treeFromMap
(theMap, theStack, theTree)¶ Creates a
Tree
from a dict of list of strings.Parameters: - theMap (
dict({str : [list([str])]})
) – The dictionary. - theStack (
list([str])
) – The stack of strings. - theTree (
cpip.util.Tree.Tree
) – An existing Tree.
Returns: NoneType
- theMap (
-
add
(parent, child)¶ Adds the parent/child to both internal maps.
Parameters: - parent (
str
) – Parent. - child (
str
) – Child.
Returns: NoneType
- parent (
-
allChildren
¶ Returns an unordered list of objects that have at least one parent.
-
allParents
¶ Returns an unordered list of objects that have at least one child.
-
children
(parent)¶ Returns all immediate children of a given parent.
-
hasChild
(child)¶ Returns True if the given child has any parents.
Parameters: child ( str
) – The childReturns: bool
– True if the argument is a child is in the map.
-
hasParent
(parent)¶ Returns True if the given parent has any children.
-
parents
(child)¶ Returns all immediate parents of a given child.
-
treeChildParent
(theObj)¶ Returns a Tree() object where the links are the relationships between child and parent.
Cycles are not reproduced i.e. if
a -> b
andb -> c
andc -> a
then:treeChildParent('a') # returns ['a', 'c', 'b',] treeChildParent('b') # returns ['b', 'a', 'c',] treeChildParent('c') # returns ['c', 'b', 'a',]
Parameters: theObj ( str
) – The object to create a tree from.Returns: cpip.util.Tree.Tree
– The final tree.
-
treeParentChild
(theObj)¶ Returns a Tree() object where the links are the relationships between parent and child.
Cycles are not reproduced i.e. if
a -> b
andb -> c
andc -> a
then:treeChildParent('a') # returns ['a', 'c', 'b',] treeChildParent('b') # returns ['b', 'a', 'c',] treeChildParent('c') # returns ['c', 'b', 'a',]
Parameters: theObj ( str
) – The object to create a tree from.Returns: cpip.util.Tree.Tree
– The final tree.
-
-
class
cpip.util.Tree.
Tree
(obj)¶ Represents a simple tree of objects.
-
__init__
(obj)¶ Constructor, takes any object.
Parameters: obj ( object
) – Any object, usually a string.Returns: NoneType
-
__weakref__
¶ list of weak references to the object (if defined)
-
_branches
(thisBranch)¶ <insert documentation for function>
Parameters: thisBranch ( NoneType, list([str])
) – Current branch, None initially.Returns: list([list([str])])
– List of branches.
-
addChild
(obj)¶ Add a child.
Parameters: obj ( str
) – Child.Returns: NoneType
-
branches
()¶ Returns all the possible branches through the tree as a list of lists of self._obj.
Returns: list([list([str])])
– List of branches.
-
youngestChild
¶ The latest child to be added, may raise IndexError if no children.
Returns: cpip.util.Tree.Tree
– The youngest child.
-