Tree

class rnamake.tree.Tree[source]

Bases: object

General implementation of a tree. Do not call directly!

Attributes:
nodes : list of TreeNode objects
all the nodes in the tree, used for fast access by index
level : int
the current tree level, used for quickly deleting sections of the tree
index: int
the current node index, always the length of the number of nodes in the tree
last_node: TreeNode object
the last node added to the tree
current_node: TreeNode object
the current node during iteration
decrease_level()[source]

Decreases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

get_node(index)[source]
Parameters:index (int) – the node index that you want
Returns:TreeNode object
Raises:exceptions.TreeIndexException
Examples:
>>> t = TreeDynamic()
>>> t.add_data(10)
#get node of index '0' which is the first one
>>> print t.get_node(0).data
10
next_level()[source]

Increases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

remove_node(node=None, index=None)[source]

removes a node with a given index

Parameters:
  • node (TreeNode) – the actual node object you want to remove
  • index (int) – the index of the node you want to remove
Returns:

None

Raises:

exceptions.TreeIndexException, exceptions.TreeException

remove_node_level(level=None)[source]

remove all nodes with a given level, this is useful if you are unsure how many nodes have been added from a given point. if level is not specified level will be set to current level

Parameters:level (int) – the node minimum node level you want to remove
Returns:None
class rnamake.tree.TreeDynamic[source]

Bases: rnamake.tree.Tree

a Tree that has a dynamic number of children. i.e. each node does NOT have a predefined number of children. Each node starts with 0 children and are added over time, there is no max to the number of children each node can have

Attributes:
nodes : list of TreeNode objects
all the nodes in the tree, used for fast access by index
level : int
the current tree level, used for quickly deleting sections of the tree
index: int
the current node index, always the length of the number of nodes in the tree
last_node: TreeNode object
the last node added to the tree
current_node: TreeNode object
the current node during iteration
Examples:
>>> t = TreeDynamic()
>>> t.add_data(0)
>>> t.add_data(1)
>>> t.add_data(2, parent_index=0)
>>> t.add_data(3, parent_index=0)

>>> len(t.get_node(0).children)
3
add_data(data, parent_index=-1)[source]

add a new element of data to the tree

Parameters:
  • data (can be anything) – Item to be added to tree
  • parent_index (int) – index of node that this element should be connected too
Returns:

index of added node

Return type:

int

Raises:

exceptions.TreeIndexException

decrease_level()

Decreases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

get_node(index)
Parameters:index (int) – the node index that you want
Returns:TreeNode object
Raises:exceptions.TreeIndexException
Examples:
>>> t = TreeDynamic()
>>> t.add_data(10)
#get node of index '0' which is the first one
>>> print t.get_node(0).data
10
next_level()

Increases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

remove_node(node=None, index=None)

removes a node with a given index

Parameters:
  • node (TreeNode) – the actual node object you want to remove
  • index (int) – the index of the node you want to remove
Returns:

None

Raises:

exceptions.TreeIndexException, exceptions.TreeException

remove_node_level(level=None)

remove all nodes with a given level, this is useful if you are unsure how many nodes have been added from a given point. if level is not specified level will be set to current level

Parameters:level (int) – the node minimum node level you want to remove
Returns:None
class rnamake.tree.TreeNode(data, index, level, n_children=0)[source]

Bases: object

An abstract class to define the behavior of a tree node or an element of data that has connectivity relationships to other elements of data of the same type. This class is never called directly.

Parameters:
  • data (anything) – The element that you wish to store in the tree
  • index (int) – The way for you to find this element of data again through Tree.get_node()
  • level (int) – A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
  • n_children (int) – The number of connections this node can have, in TreeDynamic this does nothing.
Attributes:
data : anything
The element of data being stored in tree
index: int
The way for you to find this element of data again through Tree.get_node()
level: int
A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
children: list of TreeNodes
Nodes whose parent is this node
parent: TreeNode
the parent of the current node
available_children_pos()[source]

gets all of the children positions that are not filled yet for this node

Returns:a list of end positions not yet filled
Return type:list of ints
available_pos(pos)[source]

checks to see if a child position is filled or not. returns 0 is filled or not available and 1 if its avialable to be used.

Parameters:pos – the position in children list you want to check
Returns:the avialability of the position
Return type:int
parent_end_index()[source]

gets the position of this node in the children list of the parent. i.e. what number child is this not in reference to the parent. will return -1 if this node has no parent.

Returns:the child position this node is in reference to its parent
Return type:int
parent_index()[source]

gets the node index of parent if parent is None returns -1

Returns:node index of parent
Return type:int
class rnamake.tree.TreeNodeDynamic(data, index, level)[source]

Bases: rnamake.tree.TreeNode

A TreeNode container that is specific for TreeDynamic trees. That is it can be connected to an unlimited number of other nodes. There is no reason to instantiate this class outside TreeDynamic.

Parameters:
  • data (anything) – The element that you wish to store in the tree
  • index (int) – The way for you to find this element of data again through Tree.get_node()
  • level (int) – A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
Attributes:
data : anything
The element of data being stored in tree
index: int
The way for you to find this element of data again through Tree.get_node()
level: int
A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
children: list of TreeNodes
Nodes whose parent is this node
parent: TreeNode
the parent of the current node
add_child(node)[source]

adds a new node as a child to the current node

Parameters:node (TreeNodeDynamic) – the node you want to as a child to this node
Returns:None
available_children_pos()

gets all of the children positions that are not filled yet for this node

Returns:a list of end positions not yet filled
Return type:list of ints
available_pos(pos)

checks to see if a child position is filled or not. returns 0 is filled or not available and 1 if its avialable to be used.

Parameters:pos – the position in children list you want to check
Returns:the avialability of the position
Return type:int
parent_end_index()

gets the position of this node in the children list of the parent. i.e. what number child is this not in reference to the parent. will return -1 if this node has no parent.

Returns:the child position this node is in reference to its parent
Return type:int
parent_index()

gets the node index of parent if parent is None returns -1

Returns:node index of parent
Return type:int
remove_child(node)[source]

removes a node as a child from the current ndoe

Parameters:node (TreeNodeDynamic) – the node want to remove as a child to this node. must already be a child
Returns:
class rnamake.tree.TreeNodeStatic(data, index, level, n_children)[source]

Bases: rnamake.tree.TreeNode

A TreeNode container that is specific for TreeStatic trees. That is it can be connected to an unlimited number of other nodes. There is no reason to instantiate this class outside TreeStatic.

Parameters:
  • data (anything) – The element that you wish to store in the tree
  • index (int) – The way for you to find this element of data again through Tree.get_node()
  • level (int) – A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
  • n_children (int) – The number of connections this node can have, in TreeDynamic this does nothing.
Attributes:
data : anything
The element of data being stored in tree
index: int
The way for you to find this element of data again through Tree.get_node()
level: int
A way of grouping nodes together, increasing the level and then removing all nodes of a given level is a quick way to batch remove nodes
children: list of TreeNodes
Nodes whose parent is this node
parent: TreeNode
the parent of the current node
add_child(node, pos)[source]

adds a new node a a child at a specific position in this nodes child’s list.

Parameters:
  • node (TreeNodeStatic) – the node you want to add as a child of this node
  • pos (int) – the position in this nodes children’s list you want to place this new child
Returns:

None

available_children_pos()

gets all of the children positions that are not filled yet for this node

Returns:a list of end positions not yet filled
Return type:list of ints
available_pos(pos)

checks to see if a child position is filled or not. returns 0 is filled or not available and 1 if its avialable to be used.

Parameters:pos – the position in children list you want to check
Returns:the avialability of the position
Return type:int
copy()[source]

deep copies tree node and also tries to deep copy data. Will work if data element has a function copy().

Returns:a deep copy of node
Return type:TreeNodeStatic
parent_end_index()

gets the position of this node in the children list of the parent. i.e. what number child is this not in reference to the parent. will return -1 if this node has no parent.

Returns:the child position this node is in reference to its parent
Return type:int
parent_index()

gets the node index of parent if parent is None returns -1

Returns:node index of parent
Return type:int
remove_child(node)[source]

Remove a child at a specific position in child list

Parameters:node (TreeNodeStatic) – the node to remove
Returns:None
class rnamake.tree.TreeStatic[source]

Bases: rnamake.tree.Tree

A Tree that has a static number of children. i.e. each node has a predefined number of children. Each node starts with 0 children and are added over time, there is no max to the number of children each node can have

Attributes:
nodes : list of TreeNode objects
all the nodes in the tree, used for fast access by index
level : int
the current tree level, used for quickly deleting sections of the tree
index: int
the current node index, always the length of the number of nodes in the tree
last_node: TreeNode object
the last node added to the tree
current_node: TreeNode object
the current node during iteration
add_data(data, n_children=1, parent_index=-1, parent_child_index=-1)[source]

Adds a new node to the tree given specfied data.

Parameters:
  • data (anything) – element to add to tree
  • n_children (int) – number of children the node can have in max
  • parent_index (int) – index of parent to connect to, optional
  • parent_child_index (int) – child position to connect to parent, optional
Returns:

index of new node

Return type:

int

Raises:

exceptions.TreeIndexException

decrease_level()

Decreases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

get_node(index)
Parameters:index (int) – the node index that you want
Returns:TreeNode object
Raises:exceptions.TreeIndexException
Examples:
>>> t = TreeDynamic()
>>> t.add_data(10)
#get node of index '0' which is the first one
>>> print t.get_node(0).data
10
next_level()

Increases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism

remove_node(node=None, index=None)

removes a node with a given index

Parameters:
  • node (TreeNode) – the actual node object you want to remove
  • index (int) – the index of the node you want to remove
Returns:

None

Raises:

exceptions.TreeIndexException, exceptions.TreeException

remove_node_level(level=None)

remove all nodes with a given level, this is useful if you are unsure how many nodes have been added from a given point. if level is not specified level will be set to current level

Parameters:level (int) – the node minimum node level you want to remove
Returns:None