|
| | CursorTree (void)=default |
| | the empty tree
|
| | CursorTree (const T &v) |
| | a tree of one node of value \( v \)
|
| bool | empty (void) const |
| | true if the tree has no node
|
| int | root (void) const |
| | the place of the root, NIL if the tree is empty
|
| int | parent (int n) const |
| | the parent of \( n \), NIL for the root
|
| int | leftmostChild (int n) const |
| | the first child of \( n \), NIL for a leaf
|
| int | rightSibling (int n) const |
| | the next sibling of \( n \), NIL if there is none
|
| bool | isLeaf (int n) const |
| | true if \( n \) has no child
|
| bool | isLastSibling (int n) const |
| | whether \( n \) is the last of its siblings
|
| const T & | read (int n) const |
| | the value of \( n \)
|
| void | write (int n, const T &v) |
| | writes \( v \) in \( n \)
|
| int | size (void) const |
| | the number of nodes
|
| int | insertChild (int n, const T &v) |
| | a new first child of \( n \) of value \( v \), in \( O( 1 ) \)
|
| int | insertSibling (int n, const T &v) |
| | a new next sibling of \( n \), not the root, of value \( v \), in \( O( 1 ) \), as insertChild() with the sibling of \( n \) in place of its child
|
| int | insertChildTree (int n, const CursorTree &t, int s) |
| | a copy of the subtree of \( t \) in \( s \) as the first child of \( n \); \( t \) may be this tree, copied as it was before the call
|
| int | insertSiblingTree (int n, const CursorTree &t, int s) |
| | a copy of the subtree of \( t \) in \( s \) as the next sibling of \( n \); \( t \) may be this tree, copied as it was before the call
|
| void | remove (int n) |
| | removes the subtree of \( n \), detached from its parent, its places back to the free list
|
| std::vector< int > | previsit (void) const |
| | the nodes in previsit
|
| std::vector< int > | postvisit (void) const |
| | the nodes in postvisit
|
| std::vector< int > | invisit (int i) const |
| | the nodes in invisit with index \( i \), each node after its first \( i \) subtrees
|
| auto | nodes (void) const |
| | the nodes, in the order of their places, bidirectional
|
| auto | children (int n) const |
| | the children of \( n \), from the leftmost, forward
|
template<class T>
class ADS::CursorTree< T >
an ordered tree on a vector of nodes linked by their places
a new first child of \( n \) of value \( v \), in \( O( 1 ) \)
In pseudocode (section 5.2, with cursors), \( N \) the vector of the nodes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{insertChild}( n , v )\quad
\triangleright\ \text{the new leftmost child of } n \cr
2 & \quad c \gets \text{a place from the free list, or a new one} \cr
3 & \quad N[ c ] \gets ( v , n , \mathrm{NIL} , N[ n ].\mathit{child}
)\quad \triangleright\ \text{value, parent, child, sibling} \cr
4 & \quad N[ n ].\mathit{child} \gets c;\ \textbf{return}\ c
\end{array}}
\]
- Precondition
- \( n \) is a node
a copy of the subtree of \( t \) in \( s \) as the first child of \( n \); \( t \) may be this tree, copied as it was before the call
In pseudocode (section 5.3), the copy a previsit, then linked as in insertChild():
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{copy}( t , s , p ) \cr
2 & \quad c \gets \text{a new node under } p \text{, of value }
t.\mathrm{read}( s );\ \ell \gets \mathrm{NIL}\quad \triangleright
\ \ell \text{ the last copied child} \cr
3 & \quad \textbf{foreach}\ \text{child } u \text{ of } s \text{ in } t
\text{, in order}\ \textbf{do} \cr
4 & \qquad x \gets \mathrm{copy}( t , u , c ) \cr
5 & \qquad \textbf{if}\ \ell \ne \mathrm{NIL}\ \textbf{then}\ N[ \ell
].\mathit{sibling} \gets x\ \textbf{else}\ N[ c ].\mathit{child}
\gets x \cr
6 & \qquad \ell \gets x \cr
7 & \quad \textbf{return}\ c
\end{array}}
\]
- Precondition
- \( n \) is a node, \( s \) a node of \( t \)
the nodes in invisit with index \( i \), each node after its first \( i \) subtrees
In pseudocode (section 5.3), the visit of Tree.h on the places, the previsit and the postvisit being the same with the node first or last:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{invisit}( n , i )\quad \triangleright\ n
\text{ after its first } i \text{ subtrees} \cr
2 & \quad k \gets 0 \cr
3 & \quad \textbf{foreach}\ \text{child } u \text{ of } n
\text{, in order}\ \textbf{do} \cr
4 & \qquad \textbf{if}\ k = i\ \textbf{then}\ \text{append } n \cr
5 & \qquad \mathrm{invisit}( u , i );\ k \gets k + 1 \cr
6 & \quad \textbf{if}\ k \le i\ \textbf{then}\ \text{append } n
\end{array}}
\]
removes the subtree of \( n \), detached from its parent, its places back to the free list
In pseudocode (section 5.2, with cursors), \( O( d + m ) \) for \( d \) the siblings before \( n \) and \( m \) the nodes of its subtree:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{remove}( n ) \cr
2 & \quad p \gets N[ n ].\mathit{parent} \cr
3 & \quad \textbf{if}\ p \ne \mathrm{NIL}\ \textbf{then}\ \text{find } n
\text{ among the children of } p \text{ and unlink it}
\ \textbf{else}\ \mathit{root} \gets \mathrm{NIL} \cr
4 & \quad \text{put the places of the nodes of the subtree of } n
\text{ on the free list, children before parents}
\end{array}}
\]
- Precondition
- \( n \) is a node