I’m trying to code 2-3 tree with string keys but compare method doesn’t work in searchNode().
I don’t understand why I can’t compare key with class field. Maybe there is another way to compare std::string variables. Help me please
#include <iostream>
#include <string>
class TwoThreeTree
{
public:
TwoThreeTree() : root_(nullptr) {}
bool searchKey(const std::string& key) const;
private:
struct Node
{
std::string key1_, key2_;
Node* left_;
Node* middle_;
Node* right_;
Node(std::string key1, std::string key2 = nullptr,
Node* left = nullptr, Node* middle = nullptr, Node* right = nullptr) :
key1_(key1),
key2_(key2),
left_(left),
middle_(middle),
right_(right)
{ }
};
Node* root_{ nullptr };
Node* searchNode(const std::string& key) const
{
if (root_ == nullptr)
{
return false;
}
Node* current = root_;
int c = key.compare(" ");
while (current != nullptr && (current->key1_ != key || current->key2_ != key))
{
if (current->left_.compare(key) < 0 && key.compare(current->right_) < 0)
{
current = current->middle_;
}
else if (key.compare((current->right_)) > 0)
{
}
}
}
};
New contributor
TiltedOutlow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2