Recursive Backtracker
Module: terminaltexteffects.utils.spanningtree.algo.recursivebacktracker
Recursive backtracker spanning tree generator.
This module provides the RecursiveBacktracker spanning tree
generator. The algorithm builds a spanning tree over a graph of
EffectCharacter nodes using a depth-first recursive backtracker
approach implemented iteratively with an explicit stack.
The algorithm starts from a chosen starting character (or a random one from the terminal) and grows the tree by repeatedly linking to a randomly selected unvisited neighbor. Linked cells are added to the stack.
When a node has no unvisited neighbors the algorithm backtracks by popping the stack until it finds a node with unvisited neighbors.
RecursiveBacktracker
Bases: SpanningTreeGenerator
Recursive backtracker spanning tree generator.
Attributes:
| Name | Type | Description |
|---|---|---|
char_last_linked |
EffectCharacter | None
|
Character most recently linked into the tree.
During initialization this is the starting character. On later steps it is |
char_link_order |
list[EffectCharacter]
|
Characters in the order they were linked into the tree, beginning with the starting character. |
stack |
list[EffectCharacter]
|
Depth-first traversal stack. During initialization it contains the starting character. |
stack_last_popped |
EffectCharacter | None
|
Character popped off the stack on the last step. None if no character was popped during the last step. |
complete |
bool
|
Whether the algorithm is complete. |
Source code in terminaltexteffects/utils/spanningtree/algo/recursivebacktracker.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
__init__(terminal, starting_char=None, *, limit_to_text_boundary=False)
Initialize the algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminal
|
Terminal
|
TTE Terminal. |
required |
starting_char
|
EffectCharacter | None
|
Starting character for the tree
generation. When |
None
|
limit_to_text_boundary
|
bool
|
If True, the graph will not link to neighbors outside the text boundary. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Unable to find a starting character or the starting character is pre-linked. |
Source code in terminaltexteffects/utils/spanningtree/algo/recursivebacktracker.py
step()
Advance the traversal by one step.
Each step either links one unvisited neighbor and descends deeper into the tree, or pops the traversal stack to backtrack when the current character has no unvisited neighbors.
Note
complete becomes True only on a subsequent call after
the stack is already empty. If the last stack item is popped
during this call, the generator ends the step with an empty
stack and complete still set to False.
Raises:
| Type | Description |
|---|---|
ValueError
|
An unvisited neighbor contains pre-existing links. |