Hello,
I am working on a binary search tree program.
In main, line 21, I am invoking the Binary_tree insert function. I get a compiler error on line 178 of the Binary_tree file.
The error says
"error: cannot convert 'const int*' to 'Binary_node<int>*' in assignment.
They both seem like ints to me. The root and newNode variables are of the same type, so I don't understand the problem.
//main
#include "utility.h"
#include "Binary_node.h"
#include "Binary_tree.h"
//#include "Search_tree.h"
using namespace std;
int main()
{
Binary_tree<int> tree;
tree.empty();
// tree.insert(1);
Binary_node<int> *c = new Binary_node<int>(45);
cout << "c data: " << c->data << endl;
tree.insert(1);
// Binary_node<int> *p;
// p = new Binary_node<int>(1);
// Binary_node<int> *c1 = new Binary_node<int>(1000);
// Binary_node<int> *c2 = new Binary_node<int>(1000);
// Binary_node<int> *c3 = new Binary_node<int>(1000);
// p->left = new Binary_node<int>(2);
// p->right= c1;
cout << "Hello world!" <<endl;
// cout <<p->right->data<< endl;
// cout<<p->right;
// return 0;
}
//Binary_tree.h
#include "utility.h"
//Binary_tree class interface
template <class Entry>
class Binary_tree {
public:
Binary_tree(){} //constructor
//Post: An empty binary tree has been created.
bool empty() const;
//Post: A result of true is returned if the binary tree is empty.
//Otherwise, false is returned.
void preorder(void (*visit)(Entry &));
//Post: The tree has been traversed in preorder sequence.
//Uses: The function recursive_preorder
void inorder(void (*visit)(Entry &));
//Post: The tree has been traversed in inorder sequence.
//Uses: The function recursive_inorder
void postorder(void (*visit)(Entry &));
//Post: The tree has been traversed in postorder sequence.
//Uses: The function recursive_postorder
void level_order(void (*visit) (Entry &));
//Post: The tree has been traversed in level order sequence.
//Uses: STL queue class
int size() const;
//Post: Returns the number of nodes in the tree
//Uses: The function recursive_size
void clear();
//Post: All nodes of tree have been deleted, root set to NULL
//Uses: The function recursive_clear
int height() const;
//Post: Returns the height of the tree,
//where empty tree has height 0, one-node
//tree has height 1, etc.
//Uses: The function recursive_height
void insert(const Entry &);
//Pre: Data of type Entry has been passed for insertion -
//note that the prototype need not include parameter names,
//only data types
//Post: Parameter has been inserted into the shortest
//subtree or into the left subtree if equal height
//Uses: The function recursive_insert
Binary_tree (const Binary_tree<Entry> &original);
//copy constructor
//Post: creates a deep copy of tree original
//Uses: The function recursive_copy
Binary_tree & operator =(const Binary_tree<Entry> &original);
// overloaded assignment operator
//Post: The calling tree is reset as a deep copy of tree pointed to by original
//Uses: The function recursive_copy
~Binary_tree(){} //destructor
protected:
//Auxiliary function prototypes - MORE NEED TO BE ADDED
void recursive_preorder(Binary_node<Entry> *sub_root,
void (*visit)(Entry &));
void recursive_inorder(Binary_node<Entry> *sub_root,
void (*visit)(Entry &));
void recursive_postorder(Binary_node<Entry> *sub_root,
void (*visit)(Entry &));
Binary_node<Entry>* recursive_copy(Binary_node<Entry>* sub_root);
//Pre: sub_root is NULL or points to a subtree of the Binary_tree
//Post: returns a pointer to a deep copy of tree pointed to by sub_root
int recursive_height(Binary_node<Entry>* sub_root);
//Single member variable
Binary_node<Entry> *root;
};
//========================================================================================================================================================
//========================================================================================================================================================
template<class Entry>
bool Binary_tree<Entry>::empty() const
{
cout << "empty()" << endl;
cout << "left: " << root->left << endl;
cout << "right: " << root->right << endl;
if (root->left == NULL)
{
cout << "left null" << endl;
}
else if (root->right == NULL)
{
cout << "right null" << endl;
}
bool boo;
return boo;
}
//Post: A result of true is returned if the binary tree is empty.
//Otherwise, false is returned.
template<class Entry>
void Binary_tree<Entry>::preorder(void (*visit)(Entry &))
{
}
//Post: The tree has been traversed in preorder sequence.
//Uses: The function recursive_preorder
template<class Entry>
void Binary_tree<Entry>::inorder(void (*visit)(Entry &))
{
}
//Post: The tree has been traversed in inorder sequence.
//Uses: The function recursive_inorder
template<class Entry>
void Binary_tree<Entry>::postorder(void (*visit)(Entry &))
{
}
//Post: The tree has been traversed in postorder sequence.
//Uses: The function recursive_postorder
template<class Entry>
void Binary_tree<Entry>::level_order(void (*visit) (Entry &))
{
}
//Post: The tree has been traversed in level order sequence.
//Uses: STL queue class
template<class Entry>
int Binary_tree<Entry>::size() const
{
return 0;
}
//Post: Returns the number of nodes in the tree
//Uses: The function recursive_size
template<class Entry>
void Binary_tree<Entry>::clear()
{
}
//Post: All nodes of tree have been deleted, root set to NULL
//Uses: The function recursive_clear
template<class Entry>
int Binary_tree<Entry>::height() const
{
return 0;
}
//Post: Returns the height of the tree,
//where empty tree has height 0, one-node
//tree has height 1, etc.
//Uses: The function recursive_height
template<class Entry>
void Binary_tree<Entry>::insert(const Entry &newNode)
{
root->left = &newNode;
}
//Pre: Data of type Entry has been passed for insertion -
//note that the prototype need not include parameter names,
//only data types
//Post: Parameter has been inserted into the shortest
//subtree or into the left subtree if equal height
//Uses: The function recursive_insert
template<class Entry>
Binary_tree<Entry>::Binary_tree (const Binary_tree<Entry> &original)
{
}
//copy constructor
//Post: creates a deep copy of tree original
//Uses: The function recursive_copy
//Auxiliary function prototypes - MORE NEED TO BE ADDED
template<class Entry>
void Binary_tree<Entry>::recursive_preorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))
{
}
template<class Entry>
void Binary_tree<Entry>::recursive_inorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))
{
}
template<class Entry>
void Binary_tree<Entry>::recursive_postorder(Binary_node<Entry> *sub_root, void (*visit)(Entry &))
{
}
//template<class Entry>
//void Binary_tree<Entry>::Binary_node<Entry>* recursive_copy(Binary_node<Entry>* sub_root)
//{
//
//}
//Pre: sub_root is NULL or points to a subtree of the Binary_tree
//Post: returns a pointer to a deep copy of tree pointed to by sub_root
template<class Entry>
int Binary_tree<Entry>::recursive_height(Binary_node<Entry>* sub_root)
{
int height = 0;
while(sub_root->left != NULL)
{
height++;
}
return height;
}
//Binary_node.h
//Binary_node struct interface
template <class Entry>
struct Binary_node {
//data members:
//Note that the default access in a
//struct is public, so these member
//variables are public. However,
//only private or protected member
//variables of trees will point to nodes.
Entry data;
Binary_node<Entry> *left;
Binary_node<Entry> *right;
//constructors:
Binary_node()
{
data = NULL;
left = NULL;
right = NULL;
}
Binary_node(const Entry &x)
{
data = x;
}
};