Anonymous
Ошибка сегментации для вставки в BST
Сообщение
Anonymous » 18 ноя 2024, 04:01
Итак, для этого класса мне нужно написать функции, включая функцию рекурсивного поиска, функцию вставки и функцию оператора[], которые в целом помогают вставлять в BST
Код: Выделить всё
#include
#include
#include
#include
#include
#include
template
struct BST
{
using KeyValue_Pair = std::pair;
struct Node
{
Node() = default;
Node( KeyValue_Pair const & pair ) : _pair{ pair } {}
KeyValue_Pair _pair = { Key{}, Value{} };
Node * _left = nullptr;
Node * _right = nullptr;
Node * _parent = nullptr;
};
Node * _root = nullptr;
std::size_t _size = 0;
Node * find( Key const & key )
{
return find(key, _root); // Delegate the call to the private helper function
}
Node* find(Key const& key, Node* current) {
// Base Case: Node not found
if (current == nullptr) {
return nullptr;
}
// Visit: Node found
if (key == current->_pair.first) {
return current;
}
// Recurse: Search left or right subtree
if (key < current->_pair.first) {
return find(key, current->_left);
} else {
return find(key, current->_right);
}
}
Node * insert( KeyValue_Pair const & pair ) {
Node * newNode = new Node( pair );
if(_root == nullptr){
_root = newNode;
++_size;
return newNode;
}
Node * current = _root;
Node * parent = nullptr;
while(current != nullptr){
parent = current;
auto comp = std::compare_weak_order_fallback( pair.first, current->_pair.first );
if( comp == 0 ) return current;
else if(comp < 0 ) current = current->_left;
current = current->_right;
}
auto comp = std::compare_weak_order_fallback( pair.first, current->_pair.first );
if( comp < 0 ) parent->_left = newNode;
else parent->_right = newNode;
newNode->_parent = parent;
++_size;
return newNode;
}
Value & operator[]( Key const & key )
{
return insert( { key, Value{} } )->_pair.second;
}
//everything under this comment already came with the code
void print()
{
auto count = 0uz;
print( _root, count );
}
void print( Node * current, std::size_t & count ) // in-order traversal
{
// Base case
if( current == nullptr ) return;
// Recurse left
print( current->_left, count );
// Visit
auto && [key, value] = current->_pair;
std::cout _right, count );
}
~BST() noexcept
{ clear(); }
};
int main()
{
BST myTree;
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79198437/segmentation-fault-for-inserting-into-an-bst[/url]
1731891688
Anonymous
Итак, для этого класса мне нужно написать функции, включая функцию рекурсивного поиска, функцию вставки и функцию оператора[], которые в целом помогают вставлять в BST [code]#include #include #include #include #include #include template struct BST { using KeyValue_Pair = std::pair; struct Node { Node() = default; Node( KeyValue_Pair const & pair ) : _pair{ pair } {} KeyValue_Pair _pair = { Key{}, Value{} }; Node * _left = nullptr; Node * _right = nullptr; Node * _parent = nullptr; }; Node * _root = nullptr; std::size_t _size = 0; Node * find( Key const & key ) { return find(key, _root); // Delegate the call to the private helper function } Node* find(Key const& key, Node* current) { // Base Case: Node not found if (current == nullptr) { return nullptr; } // Visit: Node found if (key == current->_pair.first) { return current; } // Recurse: Search left or right subtree if (key < current->_pair.first) { return find(key, current->_left); } else { return find(key, current->_right); } } Node * insert( KeyValue_Pair const & pair ) { Node * newNode = new Node( pair ); if(_root == nullptr){ _root = newNode; ++_size; return newNode; } Node * current = _root; Node * parent = nullptr; while(current != nullptr){ parent = current; auto comp = std::compare_weak_order_fallback( pair.first, current->_pair.first ); if( comp == 0 ) return current; else if(comp < 0 ) current = current->_left; current = current->_right; } auto comp = std::compare_weak_order_fallback( pair.first, current->_pair.first ); if( comp < 0 ) parent->_left = newNode; else parent->_right = newNode; newNode->_parent = parent; ++_size; return newNode; } Value & operator[]( Key const & key ) { return insert( { key, Value{} } )->_pair.second; } //everything under this comment already came with the code void print() { auto count = 0uz; print( _root, count ); } void print( Node * current, std::size_t & count ) // in-order traversal { // Base case if( current == nullptr ) return; // Recurse left print( current->_left, count ); // Visit auto && [key, value] = current->_pair; std::cout _right, count ); } ~BST() noexcept { clear(); } }; int main() { BST myTree; std::cout Подробнее здесь: [url]https://stackoverflow.com/questions/79198437/segmentation-fault-for-inserting-into-an-bst[/url]