Tree

Inherits: object

Module: string_tree

Note

in this class “path” always refers to a string of the titles of each node joined by a period. Example: “Grandparent.Parent.Child” is a path to the “Child” TreeNode

Brief Description

Represents a simple tree system for a non-linear parent-child string system such as a dialogue system.

Instance Methods

Tree TreeNode ( )
TreeNode add_node ( str path, str title, str string )
TreeNode get_node ( str path )
TreeNode remove_node ( str path )
str get_string ( str path )
list get_children ( str path )
list get_biological_children ( str path )
None add_adopted_child ( str path, str child )
list get_adopted_children ( str path )
TreeNode remove_adopted_child ( str path )
list get_lineage ( str path )
list remove_lineage ( str path )

Description

A tree system that stores in a parent-child relationship by title. Strings are added to the root and to the roots children and so on. Example: “root.foo.bar”

Instance Variables

  • list tree - A list of TreeNodes that this tree contains.
  • TreeNode root - The root node of this tree, with the title “root”.

Instance Method Descriptions

Creates a Tree instance

  • TreeNode add_node ( str path, str title, str string )

Creates a node using the title and string, and adds it to the parent node path then returns its instance.

Returns the TreeNode at the path

Removes and returns the node at the path. This will add the children of the node as children of the nodes parent. Note that if one of the children of this node is the same as a child of the parents node it will raise a ValueError

  • str get_string ( str path )

Returns the contents of the TreeNode at path.

Returns the children of the TreeNode at path.

  • list get_biological_children ( str path )

Returns the node at path's children that are directly created from the node.

  • None add_adopted_child ( str path, str child )

Adds an adopted child to the node at path. Child is a path to a node that already exists as a child of another node but is also the indirect child of this node

  • list get_adopted_children ( str path )

Returns the node at path’s children that are not created from the node, but are added to this node using add_adopted_child ( ) from the Tree or TreeNode.

  • TreeNode remove_adopted_child ( str path )

Removes and returns the adopted child from the node at path. Child is the path to the existing child.

  • list get_lineage ( str path )

Returns the lineage of the node at path. This includes all biological children, their biological children, and so on.

  • list remove_lineage ( str path )

Removes and returns the lineage of the node at path. This includes all biological children, their biological children, and so on.

Supported Magic Methods

Iterates through this Tree’s tree list

  • str __repr__ ( )

Returns the representation of this Tree’s tree list

  • str __str__ ( )

Returns this Tree’s tree list with each node on a new line and casting each node to a string