I am trying to insert elements in a binary search tree.I am getting the following errors:

In member function 'void bst::insert(char*)':
error: cannot convert 'char*' to 'node*' in assignment
error: cannot convert 'node*' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

In function 'int main()':
error: 'A' was not declared in this scope

//code//

#include<iostream>
using namespace std;

class node
{
	public:

	char string[100];
	node *left_child;
	node *right_child;
	node();
	~node();
};

node::node()
{
    left_child = NULL;
	right_child = NULL;
}

node::~node()
{

}
class bst
{
	public:

		void insert(char *str);
		//void display_preorder(char *str);
};

void bst::insert(char *str)
{
	node *obj;
	obj = new node();
	if (strcmp(obj->string,str)<0)
	{
            if (obj->right_child == NULL)
            {
				obj->right_child = str;
            }

			else if(strcmp(obj->right_child,str)<0)
		{
				obj->right_child = str;
		}

		else 
		{
			obj->left_child = str;
		}
	}
}

int main()
{
	bst *obj1;
	obj1 = new bst();
	obj1->insert(A);
	obj1->insert(B);
    return 0;
}

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

In member function 'void bst::insert(char*)':
error: cannot convert 'char*' to 'node*' in assignment

On lines 41 and 46 you are trying to assign str, which is a char*, to obj->right_child, which is a node*. On line 51 you are doing the same thing with the left_child pointer. The compiler doesn't know how to convert a char* to a node*. You have to create a node, using the str as the node's string member, and then set the child pointer to point to that node.

error: cannot convert 'node*' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

The strcmp function takes two strings as arguments. On line 44 you are trying to use obj->right_child (a node*) as the first argument. You should be using obj->right_child.string.

In function 'int main()':
error: 'A' was not declared in this scope

You can't pass the letter A to a function that wants a string. The compiler thinks A is a variable which you haven't declared. You need to pass "A" (in quotes) which the compiler will recognize as a string.


You have more problems than that: on line 36 you create obj, a new, empty node, and are using that in all of the operations in your insert() function. Of course, obj will always be empty since you just created it, and when the function finishes it will cease to exist since it is a local object. Your bst class needs at least a node pointer that you can initialize to NULL, and then use it to point to the first node that you add to the tree. Thereafter, you use that root node as the starting point to find the correct place to add new nodes.
(Your insert() function will need more work in order to accomplish that.)

Few more doubts while creating bst:

1.I am trying to read data from a text file "bst.txt" which contains nodes in the format:

p
s
q
l
m

My code just read the data from the text file but it is not inserting the nodes in a bst format.


2.I have to also provide an option where if the text file is not provided data is read from stdin.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

    public:
        BinarySearchTree()
        {
           root = NULL;
		   count = 0;
        }
        bool isEmpty() const { return root==NULL; }
        void print_preorder();
        void preorder(tree_node*);
        void insert(char*);
		int num_nodes();
  
};

int BinarySearchTree::num_nodes()
{
	return count;
}

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(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }

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

void BinarySearchTree::print_preorder()
{
  preorder(root);
}

void BinarySearchTree::preorder(tree_node* p)
{
    if(p != NULL)
    {
        cout<<p->data<<endl;
		cout<<"+--";
        if(p->left) preorder(p->left);
		cout<<endl<<"\\--"<<endl;
        if(p->right) preorder(p->right);
    }
    else return;
}

int main()
{
   BinarySearchTree b;
    char item[10];
	FILE *fp;
	fp = fopen("bst.txt", "r");
	while(!feof((fp)))
	{
	fscanf(fp, "%s", &item);
	b.insert(item);
	 b.print_preorder();
  cout<<endl;
	}
	int d =b.num_nodes();
cout<<"number of nodes is"<<d; 
	return 0;
}

Few more doubts while creating bst:

1.I am trying to read data from a text file "bst.txt" which contains nodes in the format:

p
s
q
l
m

My code just read the data from the text file but it is not inserting the nodes in a bst format.


2.I have to also provide an option where if the text file is not provided data is read from stdin.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

    public:
        BinarySearchTree()
        {
           root = NULL;
		   count = 0;
        }
        bool isEmpty() const { return root==NULL; }
        void print_preorder();
        void preorder(tree_node*);
        void insert(char*);
		int num_nodes();
  
};

int BinarySearchTree::num_nodes()
{
	return count;
}

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(t->data > curr->data) curr = curr->right;
        else curr = curr->left;
    }

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

void BinarySearchTree::print_preorder()
{
  preorder(root);
}

void BinarySearchTree::preorder(tree_node* p)
{
    if(p != NULL)
    {
        cout<<p->data<<endl;
		cout<<"+--";
        if(p->left) preorder(p->left);
		cout<<endl<<"\\--"<<endl;
        if(p->right) preorder(p->right);
    }
    else return;
}

int main()
{
   BinarySearchTree b;
    char item[10];
	FILE *fp;
	fp = fopen("bst.txt", "r");
	while(!feof((fp)))
	{
	fscanf(fp, "%s", &item);
	b.insert(item);
	 b.print_preorder();
  cout<<endl;
	}
	int d =b.num_nodes();
cout<<"number of nodes is"<<d; 
	return 0;
}

I have found the mistake now the data from the text file is inserted properly.

Please tell how should i make the program understand whether to take the data from a file or stdin.

For ex

if a.out [file]
where a.out is the executable of the program.if file name is not specified the data should be read from stdin.

I know type of stdin is FILE but please explain how to implement it.

This article has been dead for over six months. Start a new discussion instead.