I am trying to pass data into a class object and the put that class object into a binary tree. My code was mostly give from the professor, but I am stuck on this part. He gave us the binarytree.h and employee.h. I had to create the emp_tree.cpp.
Since the code is too long to post here is the meat of the program.
public:
struct TreeNode
{
T value;
TreeNode *left;
TreeNode *right;
};
int leafCount;
TreeNode *root;
void insert(TreeNode *&, TreeNode *&);
void destroySubTree(TreeNode *);
void deleteNode(T, TreeNode *&);
void makeDeletion(TreeNode *&);
void displayInOrder(TreeNode *);
void displayPreOrder(TreeNode *);
void displayPostOrder(TreeNode *);
int countNodes(TreeNode *&);
void countLeaves(TreeNode *);
int getTreeHeight(TreeNode *);
int numAtLevel(TreeNode *, int);code]
So here is my code for the main.cpp:
[code]/*
#include <iostream>
#include "Employee.h"
#include "BinaryTree.h"
using namespace std;
int main()
{
int num = 0;
// create a BinaryTree object
// put data into an employee object
// put emplyee object into tree
EmployeeInfo item;
BinaryTree<EmployeeInfo> tree;
cout << "Inserting nodes. ";
item.setID(1021);
item.setName("John Williams");
tree.insertNode(emp.obj);
// Display the workforce.
cout << "Here is the workforce:\n\n";
tree.displayInOrder();
// Get an ID number to search for.
cout << "\nEnter an employee number: ";
cin >> num;
// Search for the employee in the tree.
EmployeeInfo *ptr = tree.searchNode(num);
if (ptr)
{
cout << "Employee was found:\n" << *ptr;
}
else
{
cout << "That employee was not found.\n\n";
}
return 0;
} */
I cannot for the life of me get the data into employee object and get the employee object into the tree.
I think you might be looking for something a bit like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
EmployeeInfo item;
BinaryTree<EmployeeInfo> tree;
cout << "Inserting nodes. ";
item.setID(1021);
item.setName("John Williams");
BinaryTree<EmployeeInfo>::TreeNode node; // TreeNode is an INNER CLASS of BinaryTree
node.value = item; // assign my employee to the TreeNode's value (see struct TreeNode)
tree.insertNode(node); // Now insert the TreeNode into the tree!
WHen I try that I get this error:
Error 1 error C2664: 'BinaryTree<T>::insertNode' : cannot convert parameter 1 from 'BinaryTree<T>::TreeNode' to 'EmployeeInfo' c:\emp_tree.cpp 51 test2
Well I got it to work. My only question now is how do I add more than one record? I am thinking while loop, but not sure if thats the best way. Here is how I got it to work: