Binary Tree

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.

Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/////////////////////////////
// EmployeeInfo class
/////////////////////////////
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <string>
#include "BinaryTree.h"
using namespace std;

// Forware declaration of the Employee class.
class EmployeeInfo;

// Function Prototypes for Overloaded Stream Operators.
// Needed for some compilers.
ostream &operator << (ostream &, EmployeeInfo &);

class EmployeeInfo
{

private:
		int empID;
		string name;
	
public:
	friend class BinaryTree<EmployeeInfo>;

	EmployeeInfo(int id = 0, char* n = "None" ) 
	{
		empID = id;
		name = n;
	}

	void setID(int n)
	{
		empID = n;
	}

	void setName(char *str)
	{
		name = str;
	}

	void setName(string str)
	{
		name = str;
	}

	int getID()
	{
		return this->empID;
	}

	bool operator==(int value)
	{
		return this->getID() == value;
	}

	string getName()
	{
		return name;
	}

	bool operator==(const EmployeeInfo& emp)
	{
		return this->empID == emp.empID;
	}

	bool operator< (const EmployeeInfo& emp)
	{
		return this->empID < emp.empID;
	}

	bool operator> (const EmployeeInfo& emp)
	{
		return this->empID > emp.empID;
	}

	friend ostream &operator<<(ostream &strm, EmployeeInfo &obj)
	{
		strm << "ID Number: " << obj.empID << "\tName: " << obj.name << endl;
		return strm;
	}

};

#endif


functions and class for binaryTree.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.

Thank you in adance.
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! 
Last edited on
By making TreeNode an inner class of BinaryTree it shares its template parameter. So you can declare a correctly typed TreeNode like this:

1
2
3
// correctly typed TreeNode that contains items of type MyType

BinaryTree<MyType>::TreeNode node; 


Notice that the member variable 'value' from TreeNode is typed after the template parameter T?

So the value member variable will be of whatever type you passed to the enclosing class' template parameter.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
   EmployeeInfo item;
    BinaryTree<EmployeeInfo> tree;
    
   


    cout << "Inserting nodes. ";
    item.setID(1021);
    item.setName("John Williams");
    BinaryTree<EmployeeInfo>::TreeNode node;
    node.value = item;
    tree.insertNode(item);
Topic archived. No new replies allowed.