//Here is the function of inserting nodes.I tried using strcmp but its still not working.

void BinarySearchTree::insert(char* d)  
    {
            tree_node* t = new tree_node;
            tree_node* parent;
        strcpy(t->data,d);
            t->left = NULL;
            t->right = NULL;
            parent = NULL;
          // is this a new tree?
          if(isEmpty()) root = t;
          else
          {
                //Note: ALL insertions are as leaf nodes
                tree_node* curr;
                curr = root;
                // Find the Node's parent
            while(curr)
            {
				cout<<"current"<<curr->data<<endl;
                parent = curr;
				if(strcmp(t->data,curr->data)==0)
				{
					cout<<"duplicat nodes";
					exit(1);
				}
                if(strcmp(t->data,curr->data)>0) curr = curr->right;
                else curr = curr->left;
            }

            if(strcmp(t->data,parent->data)<0)
                   parent->left = t;
            else
                   parent->right = t;
          }
          count++;
    }

Dani AI

Generated

A few short, practical points that usually fix "strcmp not working" when duplicates still get inserted.

First, common root causes: the strings being compared are not normalized (trailing newline, extra spaces, different case), the C-string buffers are not properly null-terminated or too small, or you allocate the new node before checking for duplicates (then either you leak memory or your logic uses the freshly-copied value in ways that confuse the traversal). Also avoid terminating the whole program on a duplicate — exit(1) hides the bug and is not a correct way to signal "already present".

A safer pattern is: traverse the tree comparing the incoming key (do not rely on a freshly-allocated node for comparisons), detect equality and return/abort the insertion, and only allocate a new node once you know where it belongs. Using std::string removes most C-string pitfalls. Example:

bool BinarySearchTree::insert(const std::string &key) {
    if (!root) { root = new tree_node(key); ++count; return true; }
    tree_node *parent = nullptr, *curr = root;
    while (curr) {
        int cmp = key.compare(curr->data);
        if (cmp == 0) return false;               // duplicate
        parent = curr;
        curr = (cmp > 0) ? curr->right : curr->left;
    }
    if (key < parent->data) parent->left  = new tree_node(key);
    else                   parent->right = new tree_node(key);
    ++count;
    return true;
}

Troubleshooting checklist: print values with visible delimiters (e.g. printf("'%s'\n", s)) to catch hidden whitespace/newlines; normalize case if you want case-insensitive equality; prefer std::string or safe copy routines to avoid buffer overruns; initialize child pointers to nullptr in the node constructor; and if you must allocate early, free the node immediately on duplicate detection instead of calling exit. This also follows 's advice about proper initialization and avoids the abrupt termination that masks problems for .

//Here is the function of inserting nodes.I tried using strcmp but its still not working.

What do you mean by "still not working"?

Do you really want to use exit() there? I.e. your program gets terminated when duplicate data is encountered.

A suggestion regarding initialization of a tree_node

struct tree_node
{
  // default constructor
  tree_node() 
  : left(NULL), right(NULL)
  {
    // pointers are now initialized to NULL
  }
};

// So that would make somewhat cleaner code ..
tree_node* t = new tree_node;
// t->left and t->right are now automatically NULL
...

Then you might expand that even little more and also pass the data through the constructor.

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.