what I am trying to do is store same letter to same node. For example: let say I have person, people is sent to be put into a node. This will create two node. I want to some thing like

P: Person People

Person and People are same note

When I run this on Visual Studio I got

C2440 type cast: cannt convert from IntBinaryTree:treeNode to Char;
C2264 std:: basic_string....

void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
{
    string combineworld;
    stringstream sk;
    string newsk;
    string word = nodePtr->value;
    char key = word.at(1);
    sk << key;
    sk >> newsk;
    cout << "newsk : " << key << endl;


    if (nodePtr == NULL)
    {
            nodePtr = newNode;
    }
    stringstream sk2;
    string newsk2;
    string word2 = newNode->value;

    char key2 = word2.at(1);
    sk << key2;
    sk >> newsk2;
    cout << "newsk2 : " << key << endl;

    if (newsk == newsk2)
    {
        combineworld.append(nodePtr, newNode);
    }

    else if (newNode->value < nodePtr->value)
        insert(nodePtr->left, newNode);     // Search the left branch
    else
    insert(nodePtr->right, newNode);    // Search the right branch
}


void IntBinaryTree::insertNode(string num)
{
    TreeNode *newNode;      // Pointer to a new node.

    // Create a new node and store num in it.
    newNode = new TreeNode;
    newNode->value = num;
    newNode->left = newNode->right = NULL;

    // Insert the node.
    insert(root, newNode);
}

Dani AI

Generated

Brief diagnosis and a practical redesign. The current insert routine reads node data before ensuring the node exists and then tries to compare/concatenate using string conversions — that leads to null-dereference and type-mismatch problems. If the intent is to treat words that start with the same letter (for example "Person" and "People") as a single tree entry, make the node’s key that letter and store all matching words in a container inside the node instead of trying to shove two TreeNode objects into one place.

A compact, safe node + insert pattern (store many words per key):

struct TreeNode {
    char key;
    std::vector<std::string> words;
    TreeNode* left = nullptr;
    TreeNode* right = nullptr;
    TreeNode(char k, const std::string& w) : key(k) { words.push_back(w); }
};

void insert(TreeNode*& node, const std::string& s) {
    if (s.empty()) return;
    char k = std::tolower(static_cast<unsigned char>(s[0])); // normalize
    if (!node) { node = new TreeNode(k, s); return; }
    if (k == node->key) node->words.push_back(s);
    else if (k < node->key) insert(node->left, s);
    else insert(node->right, s);
}

Practical tips and alternatives. Check string length before indexing and always test node == nullptr before using node->.... Compare characters directly (no stringstream round-trips); use std::string(1,ch) only when you really need a string. Prefer storing a vector/list of words in the node rather than concatenating them into one string. For a simpler solution, an associative container like std::map<char, std::vector<std::string>> will handle grouping automatically. As suggested, templates generalize the tree; ’s point about char vs string confusion is spot on — use the appropriate type for the key and container. Finally, consider smart pointers or a proper destructor to avoid leaks.

Recommended Answers

All 2 Replies

Not sure what you mean by IntBinaryTree when your data are in strings?
A better tree design will be template BinaryTree<int> ? BinaryTree<strings> ?

As far as I understand your code (sorry, still learning C++), the problem is that you use a string, instead of an array of chars, and when you pass it to the tree, you pass it as a string, instead of a char ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.