an ordered tree in a vector of ( parent , value ) in preorder
More...
#include <ADS/tree/PreorderArrayTree.h>
|
| using | Item = T |
| | the type of the values
|
|
| | PreorderArrayTree (void)=default |
| | the empty tree
|
| | PreorderArrayTree (const T &v) |
| | a tree of one node of value \( v \)
|
| bool | empty (void) const |
| | true if the tree has no node
|
| int | size (void) const |
| | the number of nodes, \( n \)
|
| int | root (void) const |
| | the root, 0, or NIL if the tree is empty
|
| int | parent (int i) const |
| | the parent of \( i \), NIL for the root
|
| const T & | read (int i) const |
| | the value of \( i \)
|
| void | write (int i, const T &v) |
| | writes \( v \) in \( i \)
|
| int | leftmostChild (int i) const |
| | the first child, \( i + 1 \) if its parent is \( i \), NIL otherwise
|
| int | rightSibling (int i) const |
| | the next sibling, the place after the subtree of \( i \) if it has the same parent, NIL otherwise
|
| bool | isLeaf (int i) const |
| | true if \( i \) has no child
|
| int | subtreeEnd (int i) const |
| | the place after the subtree of \( i \)
|
| void | insertChild (int i, const PreorderArrayTree &t) |
| | a copy of \( t \) as the first child of \( i \); \( t \) may be this tree, copied as it was before the call
|
| void | insertSibling (int i, const PreorderArrayTree &t) |
| | a copy of \( t \) as the next sibling of \( i \), not the root; \( t \) may be this tree, copied as it was before the call: the splice of insertChild() at the end of the subtree of \( i \), under the parent of \( i \)
|
| void | remove (int i) |
| | removes the subtree of \( i \), the places after it shifted back and their parents renumbered
|
| auto | values (void) const |
| | the values of the nodes in preorder, which is the order of the array, random access
|
|
| static constexpr int | NIL = -1 |
| | no node
|
template<class T>
class ADS::PreorderArrayTree< T >
an ordered tree in a vector of ( parent , value ) in preorder
◆ Item
◆ PreorderArrayTree() [1/2]
◆ PreorderArrayTree() [2/2]
a tree of one node of value \( v \)
◆ empty()
true if the tree has no node
◆ insertChild()
a copy of \( t \) as the first child of \( i \); \( t \) may be this tree, copied as it was before the call
In pseudocode (beyond the book), the cells of \( t \), of \( k \) nodes, spliced in at the place \( i + 1 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insertChild}( i , t ) \cr
2 & \quad a \gets i + 1;\ C \gets \text{a copy of the cells of } t \cr
3 & \quad \textbf{for}\ j \gets a\ \textbf{to}\ n - 1\ \textbf{do}
\ \textbf{if}\ p[ j ] \ge a\ \textbf{then}\ p[ j ] \gets p[ j ] + k
\cr
4 & \quad C[ 0 ].\mathit{parent} \gets i;\ \textbf{for}\ j \gets 1
\ \textbf{to}\ k - 1\ \textbf{do}\ C[ j ].\mathit{parent} \gets C[ j
].\mathit{parent} + a \cr
5 & \quad \text{insert } C \text{ at the place } a
\end{array}}
\]
- Precondition
- \( i \) is a node
◆ insertSibling()
a copy of \( t \) as the next sibling of \( i \), not the root; \( t \) may be this tree, copied as it was before the call: the splice of insertChild() at the end of the subtree of \( i \), under the parent of \( i \)
- Precondition
- \( i \) is a node other than the root
◆ isLeaf()
true if \( i \) has no child
- Precondition
- \( i \) is a node
◆ leftmostChild()
the first child, \( i + 1 \) if its parent is \( i \), NIL otherwise
- Precondition
- \( i \) is a node
◆ parent()
the parent of \( i \), NIL for the root
- Precondition
- \( i \) is a node
◆ read()
the value of \( i \)
- Precondition
- \( i \) is a node
◆ remove()
removes the subtree of \( i \), the places after it shifted back and their parents renumbered
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{remove}( i ) \cr
2 & \quad e \gets \mathrm{subtreeEnd}( i );\ k \gets e - i \cr
3 & \quad \text{erase the places } [ i , e ) \cr
4 & \quad \textbf{for}\ j \gets i\ \textbf{to}\ n - 1\ \textbf{do}
\ \textbf{if}\ p[ j ] \ge e\ \textbf{then}\ p[ j ] \gets p[ j ] - k
\end{array}}
\]
- Precondition
- \( i \) is a node
◆ rightSibling()
the next sibling, the place after the subtree of \( i \) if it has the same parent, NIL otherwise
- Precondition
- \( i \) is a node
◆ root()
the root, 0, or NIL if the tree is empty
◆ size()
the number of nodes, \( n \)
◆ subtreeEnd()
the place after the subtree of \( i \)
In pseudocode (beyond the book), \( p \) the parents: the subtree goes on while the parents are in it, i.e., not before \( i \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{subtreeEnd}( i ) \cr
2 & \quad e \gets i + 1 \cr
3 & \quad \textbf{while}\ e < n\ \textbf{and}\ p[ e ] \ge i
\ \textbf{do}\ e \gets e + 1 \cr
4 & \quad \textbf{return}\ e
\end{array}}
\]
- Precondition
- \( i \) is a node
◆ values()
the values of the nodes in preorder, which is the order of the array, random access
◆ write()
writes \( v \) in \( i \)
- Precondition
- \( i \) is a node
◆ NIL
The documentation for this class was generated from the following file: