Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Naive_Suffix_Tree Class Reference

Naive compressed suffix tree (didactic implementation). More...

#include <Suffix_Structures.H>

Collaboration diagram for Aleph::Naive_Suffix_Tree:
[legend]

Classes

struct  Node
 One tree node. More...
 

Public Member Functions

 Naive_Suffix_Tree (const std::string_view text="")
 Construct a naive suffix tree.
 
 Naive_Suffix_Tree (const Naive_Suffix_Tree &other)=default
 Copy the text and the nodes of another tree.
 
 Naive_Suffix_Tree (Naive_Suffix_Tree &&other)
 Take the text and the nodes of another tree, which is left as the tree of the empty text, as built by default.
 
Naive_Suffix_Tree & operator= (const Naive_Suffix_Tree &other)=default
 Copy the text and the nodes of another tree.
 
Naive_Suffix_Tree & operator= (Naive_Suffix_Tree &&other) noexcept
 Exchange this tree with another, which is left with the previous text and nodes of this one.
 
void swap (Naive_Suffix_Tree &other) noexcept
 Exchange in O(1) the text and the nodes of this tree and another.
 
void build (const std::string_view text)
 Build or rebuild the suffix tree for text.
 
bool contains (const std::string_view pattern) const
 Return true if the pattern appears in the text.
 
Array< size_t > find_all (const std::string_view pattern) const
 Return all occurrences of a pattern.
 
size_t node_count () const noexcept
 Return the number of nodes in the tree.
 
size_t text_size () const noexcept
 Return the text size (without terminal marker).
 
const Array< Node > & nodes () const noexcept
 Return the internal node array for inspection/debugging.
 

Static Public Attributes

static constexpr size_t npos = std::numeric_limits<size_t>::max()
 

Private Member Functions

size_t create_node (const size_t start, const size_t end, const size_t parent, const size_t suffix_index)
 
size_t find_child_by_first_char (const size_t node, const char c) const
 
void replace_child (const size_t parent, const size_t old_child, const size_t new_child)
 
void insert_suffix (const size_t suffix_start)
 
void collect_leaf_suffixes (const size_t node, Array< size_t > &out) const
 

Static Private Member Functions

static char choose_terminal (const std::string_view s)
 

Private Attributes

std::string text_
 
size_t original_size_ = 0
 
Array< Node > nodes_
 

Detailed Description

Naive compressed suffix tree (didactic implementation).

This is intentionally simple and educational (O(n^2) construction), not Ukkonen's linear-time algorithm.

Note
Construction is O(n^2) in time and space. This class is meant for teaching and small inputs; for production use prefer the suffix array + LCP approach or an external linear-time library.
A unique terminal byte is appended internally via choose_terminal(). If every byte value (0–255) appears in the input, construction will throw std::domain_error.
A growing std::vector copies these objects instead of moving them: their move constructor is not noexcept, because it leaves the source empty and usable, which allocates, and std::vector moves only what cannot throw. The containers of Aleph do not copy them to grow: Array moves its items, and DynList and DynArray never relocate them.
Examples
suffix_tree_example.cc.

Definition at line 248 of file Suffix_Structures.H.

Constructor & Destructor Documentation

◆ Naive_Suffix_Tree() [1/3]

Aleph::Naive_Suffix_Tree::Naive_Suffix_Tree ( const std::string_view  text = "")
inlineexplicit

Construct a naive suffix tree.

Parameters
[in]textThe text to build the tree from. If empty, the tree will only contain the terminal character.
Exceptions
std::domain_errorIf all 256 byte values are present in text, leaving no unique sentinel character available.
Complexity
O(n^2) where n is text.size().

Definition at line 393 of file Suffix_Structures.H.

References build().

◆ Naive_Suffix_Tree() [2/3]

Aleph::Naive_Suffix_Tree::Naive_Suffix_Tree ( const Naive_Suffix_Tree &  other)
default

Copy the text and the nodes of another tree.

Parameters
[in]othertree to copy.
Exceptions
std::bad_allocif the text or the nodes cannot be allocated.

◆ Naive_Suffix_Tree() [3/3]

Aleph::Naive_Suffix_Tree::Naive_Suffix_Tree ( Naive_Suffix_Tree &&  other)
inline

Take the text and the nodes of another tree, which is left as the tree of the empty text, as built by default.

The implicit move took the text and the nodes, the root among them, but copied the text size: the moved-from tree then reported the size of a text that it no longer had, and its searches read a missing root.

Parameters
[in,out]othertree to move from.
Exceptions
std::bad_allocif the tree left to other cannot be allocated; other is then unchanged.
Note
Complexity: O(1), but it allocates the tree left to other.

Definition at line 416 of file Suffix_Structures.H.

References swap().

Member Function Documentation

◆ build()

void Aleph::Naive_Suffix_Tree::build ( const std::string_view  text)
inline

Build or rebuild the suffix tree for text.

Parameters
[in]textThe text to build the tree from.
Exceptions
std::domain_errorIf all 256 byte values are present in text, leaving no unique sentinel character available.
Complexity
O(n^2) time and O(n^2) space in the worst case, where n is text.size().

Definition at line 457 of file Suffix_Structures.H.

References choose_terminal(), insert_suffix(), nodes_, original_size_, and text_.

Referenced by Naive_Suffix_Tree(), and Moved_From_Case< Naive_Suffix_Tree >::use().

◆ choose_terminal()

static char Aleph::Naive_Suffix_Tree::choose_terminal ( const std::string_view  s)
inlinestaticprivate

Definition at line 271 of file Suffix_Structures.H.

References ah_domain_error.

Referenced by build().

◆ collect_leaf_suffixes()

void Aleph::Naive_Suffix_Tree::collect_leaf_suffixes ( const size_t  node,
Array< size_t > &  out 
) const
inlineprivate

◆ contains()

bool Aleph::Naive_Suffix_Tree::contains ( const std::string_view  pattern) const
inline

Return true if the pattern appears in the text.

Parameters
[in]patternThe substring pattern to search for.
Returns
True if pattern is found in the text, false otherwise. If pattern is empty, returns true.
Exceptions
None.
Complexity
O(m) where m is pattern.size().

Definition at line 481 of file Suffix_Structures.H.

References Aleph::and, find_child_by_first_char(), k, nodes_, npos, and text_.

Referenced by TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().

◆ create_node()

size_t Aleph::Naive_Suffix_Tree::create_node ( const size_t  start,
const size_t  end,
const size_t  parent,
const size_t  suffix_index 
)
inlineprivate

◆ find_all()

Array< size_t > Aleph::Naive_Suffix_Tree::find_all ( const std::string_view  pattern) const
inline

Return all occurrences of a pattern.

Parameters
[in]patternThe substring pattern to search for.
Returns
An array containing the starting positions of all occurrences of pattern in the text, sorted in ascending order. Returns an empty array if no occurrences are found.
Note
If pattern is empty, all positions [0, text.size()] are returned.
Exceptions
None.
Complexity
O(m + k log k) where m is pattern.size() and k is the number of occurrences.
Examples
suffix_tree_example.cc.

Definition at line 532 of file Suffix_Structures.H.

References Aleph::suffix_structures_detail::all_positions(), Aleph::and, collect_leaf_suffixes(), find_child_by_first_char(), k, nodes_, npos, original_size_, Aleph::suffix_structures_detail::sort_matches(), and text_.

Referenced by Moved_From_Case< Naive_Suffix_Tree >::is_empty(), main(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().

◆ find_child_by_first_char()

size_t Aleph::Naive_Suffix_Tree::find_child_by_first_char ( const size_t  node,
const char  c 
) const
inlineprivate

Definition at line 299 of file Suffix_Structures.H.

References nodes_, npos, Aleph::Array< T >::size(), and text_.

Referenced by contains(), find_all(), and insert_suffix().

◆ insert_suffix()

void Aleph::Naive_Suffix_Tree::insert_suffix ( const size_t  suffix_start)
inlineprivate

◆ node_count()

size_t Aleph::Naive_Suffix_Tree::node_count ( ) const
inlinenoexcept

Return the number of nodes in the tree.

Returns
Total number of nodes (internal and leaves).
Exceptions
None.
Complexity
O(1).
Examples
suffix_tree_example.cc.

Definition at line 582 of file Suffix_Structures.H.

References nodes_.

Referenced by main(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().

◆ nodes()

const Array< Node > & Aleph::Naive_Suffix_Tree::nodes ( ) const
inlinenoexcept

Return the internal node array for inspection/debugging.

Returns
A constant reference to the underlying Array of nodes.
Exceptions
None.
Complexity
O(1).

Definition at line 605 of file Suffix_Structures.H.

References nodes_.

Referenced by TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().

◆ operator=() [1/2]

Naive_Suffix_Tree & Aleph::Naive_Suffix_Tree::operator= ( const Naive_Suffix_Tree &  other)
default

Copy the text and the nodes of another tree.

Parameters
[in]othertree to copy.
Returns
this tree.
Exceptions
std::bad_allocif the text or the nodes cannot be allocated.

◆ operator=() [2/2]

Naive_Suffix_Tree & Aleph::Naive_Suffix_Tree::operator= ( Naive_Suffix_Tree &&  other)
inlinenoexcept

Exchange this tree with another, which is left with the previous text and nodes of this one.

Parameters
[in,out]othertree to move from.
Returns
this tree.
Note
Complexity: O(1).

Definition at line 434 of file Suffix_Structures.H.

References swap().

◆ replace_child()

void Aleph::Naive_Suffix_Tree::replace_child ( const size_t  parent,
const size_t  old_child,
const size_t  new_child 
)
inlineprivate

Definition at line 309 of file Suffix_Structures.H.

References nodes_.

Referenced by insert_suffix().

◆ swap()

void Aleph::Naive_Suffix_Tree::swap ( Naive_Suffix_Tree &  other)
inlinenoexcept

Exchange in O(1) the text and the nodes of this tree and another.

Parameters
[in,out]othertree to exchange with.

Definition at line 443 of file Suffix_Structures.H.

References nodes_, original_size_, and text_.

Referenced by Naive_Suffix_Tree(), and operator=().

◆ text_size()

size_t Aleph::Naive_Suffix_Tree::text_size ( ) const
inlinenoexcept

Return the text size (without terminal marker).

Returns
The size of the original text used to build the tree. This corresponds to the original_size_ member.
Exceptions
None.
Complexity
O(1).

Definition at line 594 of file Suffix_Structures.H.

References original_size_.

Referenced by Moved_From_Case< Naive_Suffix_Tree >::is_empty(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().

Member Data Documentation

◆ nodes_

◆ npos

constexpr size_t Aleph::Naive_Suffix_Tree::npos = std::numeric_limits<size_t>::max()
staticconstexpr

◆ original_size_

size_t Aleph::Naive_Suffix_Tree::original_size_ = 0
private

Definition at line 268 of file Suffix_Structures.H.

Referenced by build(), collect_leaf_suffixes(), find_all(), swap(), and text_size().

◆ text_

std::string Aleph::Naive_Suffix_Tree::text_
private

The documentation for this class was generated from the following file: