# Structure

`Tree` objects define a tree structure by implementing the local movement methods

> - `parent_position()`

> - `first_child_position()`

> - `last_child_position()`

> - `next_sibling_position()`

> - `prev_sibling_position()`

Each of which takes and returns a position object of arbitrary type (fixed for the Tree) as done in urwids ListWalker API. Apart from this, a Tree is assumed to define a dedicated position tree.root that is used as fallback initially focussed element, and define the `__getitem__()` method to return its content (usually a Widget) for a given position.

Note that `Tree` only defines a tree structure, it does not necessarily have any decoration around its contained Widgets.

There is a ready made subclass called `SimpleTree` that offers the tree API for a given nested tuple structure. If you write your own classes its a good idea to subclass `Tree` and just overwrite the above mentioned methods as the base class already offers a number of derivative methods.

## API

class urwidtrees.tree.Tree

  Base class for a tree strucures that can be displayed by `TreeBox` widgets. An instance defines a structure by defining local transformations on positions. That is, by overwriting

  > - next_sibling_position

  > - prev_sibling_position

  > - parent_position

  > - first_child_position

  > - last_child_position

  that compute the next position in the respective direction. Also, they need to implement method __getitem__ that returns a `Widget` for a given position.

  The type of objects used as positions may vary in subclasses and is deliberately unspecified for the base class.

  This base class already implements methods based on the local transformations above. These include `depth()`, `last_decendant()` and `[next|prev]_position` that computes next/previous positions in depth-first order.

  depth(pos)

    determine depth of node at pos

  first_ancestor(pos)

    position of pos’s ancestor with depth 0. Usually, this should return the root node, but a `Tree` might represent a forrest - have multiple nodes without parent.

  first_child_position(pos)

    returns the position of the first child of the node at pos, or None if none exists.

  first_sibling_position(pos)

    position of first sibling of pos

  is_leaf(pos)

    checks if given position has no children

  last_child_position(pos)

    returns the position of the last child of the node at pos, or None if none exists.

  last_decendant(pos)

    position of last (in DFO) descendant of pos

  last_sibling_position(pos)

    position of last sibling of pos

  next_position(pos)

    returns the next position in depth-first order

  next_sibling_position(pos)

    returns the position of the next sibling of the node at pos, or None if none exists.

  parent_position()

    returns the position of the parent node of the node at pos or None if none exists.

  positions(reverse=False)

    returns a generator that walks the positions of this tree in DFO

  prev_position(pos)

    returns the previous position in depth-first order

  prev_sibling_position(pos)

    returns the position of the previous sibling of the node at pos, or None if none exists.

class urwidtrees.tree.SimpleTree(treelist)

  Walks on a given fixed acyclic structure given as a list of nodes; every node is a tuple (content, children), where content is a urwid.Widget to be displayed at that position and children is either None or a list of nodes.

  Positions are lists of integers determining a path from the root node with position (0,).

  static depth(pos)

    more performant implementation due to specific structure of pos

  first_child_position(pos)

    returns the position of the first child of the node at pos, or None if none exists.

  last_child_position(pos)

    returns the position of the last child of the node at pos, or None if none exists.

  next_sibling_position(pos)

    returns the position of the next sibling of the node at pos, or None if none exists.

  static parent_position(pos)

    returns the position of the parent node of the node at pos or None if none exists.

  static prev_sibling_position(pos)

    returns the position of the previous sibling of the node at pos, or None if none exists.