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*);
};

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.