I have to create a bst in which i have to add string values in the nodes..I have to display the no of nodes inserted each time..When I insert small string like..FA or B...i am getting the correct value in count[count is the variable which I have taken to count the number of nodes in bst]..But when I enter a larger string value like a2b3bergrk the count is not working properly...its intial value which is set to zero is changed to some garbage value..like 8543245 instead of 0.

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)
            {
                parent = curr;
                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;
          }
		 
		  cout<<count<<endl;
		  
          count++;
		  
    }

class BinarySearchTree
{
        private:
        int count;
		int n;
        struct tree_node
			{
			   tree_node* left;
			   tree_node* right;
			   char data[10];
			   
			};
        tree_node* root;

        public:
			BinarySearchTree()
			{
			   root = NULL;
				count = 0;
				 n=1;
			 
			}
        void insert(char*);
};

Dani AI

Generated

A concise diagnosis and practical fixes based on the thread: reported that short strings worked but a longer input made a counter show a garbage value; correctly suggested checking how the input, node storage, and counter are defined. Those symptoms are characteristic of writing past a fixed-size C buffer: overrunning a buffer corrupts nearby memory and yields undefined behavior (which can manifest as a suddenly wrong counter). See the general discussion of undefined behavior and the risks of unbounded C-string copies: Undefined behavior and strcpy pitfalls.

Practical steps to fix and harden the code:

  • Replace raw fixed-size char arrays with std::string for node data and function parameters. That removes manual buffer sizing and most overrun risks. See std::string basics.
  • If C-style buffers are unavoidable, always allocate based on actual input length or use bounded-copy patterns and explicitly null-terminate. Avoid unchecked strcpy-style calls.
  • Use an unsigned integer type like size_t for counts, initialize it clearly, and keep increment logic simple and local so corruption is easier to spot.

Troubleshooting checklist:

  • Reproduce the failure with a minimal test that uses a long string.
  • Run under AddressSanitizer or Valgrind to catch overruns: AddressSanitizer and Valgrind.
  • Compile with warnings enabled (-Wall -Wextra) and treat warnings as errors during development.
    These steps will both fix the immediate bug and make the tree implementation safer going forward.

Recommended Answers

All 2 Replies

I have to create a bst in which i have to add string values in the nodes..I have to display the no of nodes inserted each time..When I insert small string like..FA or B...i am getting the correct value in count[count is the variable which I have taken to count the number of nodes in bst]..But when I enter a larger string value like a2b3bergrk the count is not working properly...its intial value which is set to zero is changed to some garbage value..like 8543245 instead of 0.

Need to know the definition of the

  • string input variable
  • string variable in the node
  • value being set to 0
  • count value
  • node itself

I found my mistake..

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.