integer to user-defined type

Hi all, Got a problem I need to resolve. How I would I convert this Binary Search Tree to accept user-defined types instead of integers:

Templates maybe? how would I implement them?
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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

class studentRegister
{
    private: 
        struct Node
        {
			Node * left;
			Node * right;
           int data;
        };
        Node * root;
    
    public:
        studentRegister()
        {
           root = NULL;
        }
       
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(Node *);
        void print_preorder();
        void preorder(Node *);
        void print_postorder();
        void postorder(Node *);
        void insert(int);
        void remove(int);
};


Also is this structure suitable for a SET Abstract Data Type
or is there a more efficient/less complex structures?

thanks in advance
You need to use a template and replace int with the template parameter
Like so:
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
template <typename DT>
class studentRegister
{
    private:
        struct Node
        {
           Node * left;
           Node * right;
           <DT> data;
        };
        Node * root;
    
    public:
        studentRegister()
        {
           root = NULL;
        }
       
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(Node *);
        void print_preorder();
        void preorder(Node *);
        void print_postorder();
        void postorder(Node *);
        void insert(int);
        void remove(int);
};
insert and remove should take a DT
Here my program simplified a little more:

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
89
90
91
92
93
94
95
96
//Student register Program
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

template <typename DT>
class studentRegister
{
    private:
        struct Node
        {
           Node * left;
           Node * right;
           <DT> data;
        };
        Node * root;
    
    public:
        studentRegister()
        {
           root = NULL;
        }
       
        bool isEmpty() const { return root==NULL; } //inline function
        void insert(DT);
       
};

// Smaller elements go left
// larger elements go right
void studentRegister::insert(DT)
{
    Node * t = new Node ;
    Node * parent;
    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
        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;
    }
}



int main()

{
    studentRegister reg;

    int ch;
	string name;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Student Register Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>name;
                    reg.insert(name);
                    break;
        
           case 2 : 
                    return 0;
                    
       }
    }
}


I get the following errors:
------ Build started: Project: assignment, Configuration: Debug Win32 ------
Compiling...
binarytree.cpp
binarytree.cpp(17) : error C2059: syntax error : '<'
binarytree.cpp(14) : see reference to class template instantiation 'studentRegister<DT>::Node' being compiled
binarytree.cpp(30) : see reference to class template instantiation 'studentRegister<DT>' being compiled
binarytree.cpp(17) : error C2238: unexpected token(s) preceding ';'
inarytree.cpp(34) : error C2955: 'studentRegister' : use of class template requires template argument list
binarytree.cpp(11) : see declaration of 'studentRegister'
binarytree.cpp(34) : error C2065: 'DT' : undeclared identifier
binarytree.cpp(35) : error C2448: 'studentRegister<DT>::insert' : function-style initializer appears to be a function definition
binarytree.cpp(70) : error C2133: 'reg' : unknown size
binarytree.cpp(70) : error C2512: 'studentRegister' : no appropriate default constructor available
binarytree.cpp(87) : error C2662: 'studentRegister<DT>::insert' : cannot convert 'this' pointer from 'studentRegister' to 'studentRegister<DT> &'
Reason: cannot convert from 'studentRegister' to 'studentRegister<DT>'
Conversion requires a second user-defined-conversion operator or constructor


where am i going wrong??
feedback will be very much appreciated
17
18
<DT> data; // remove the < >
 



You need to add the template stuff to your function definition:
34
35
template <typename DT>
    void studentRegister<DT>::insert(DT/*missing parameter name*/)


1
2
studentRegister<string> reg; // you were missing the template argument, - I assumed string from line 87 -
 


Thanks for that, still getting a few bugs though

heres my new code:
note I changed the template argument to integer

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
89
90
91
92
93
94
95
96
97
//Student register Program
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

template <typename DT>
class studentRegister
{
    private:
        struct Node
        {
           Node * left;
           Node * right;
           DT data;
        };
        Node * root;
    
    public:
        studentRegister()
        {
           root = NULL;
        }
       
        bool isEmpty() const { return root==NULL; } //inline function
        void insert(DT);
       
};

// Smaller elements go left
// larger elements go right
template <typename DT>
void studentRegister<DT>::insert(DT student)
{
    Node * t = new Node ;
    Node * parent;
    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
        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;
    }
}



int main()

{
    studentRegister<int> reg;

    int ch;
	int number;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Student Register Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter number to be inserted : ";
                    cin>>number;
                    reg.insert(number);
                    break;
        
           case 2 : 
                    return 0;
                    
       }
    }
}


Im getting the following errors:

------ Build started: Project: assignment, Configuration: Debug Win32 ------
Compiling...
binarytree.cpp
binarytree.cpp(39) : error C2065: 'd' : undeclared identifier
binarytree.cpp(36) : while compiling class template member function 'void studentRegister<DT>::insert(DT)'
with
[
DT=int
]
binarytree.cpp(71) : see reference to class template instantiation 'studentRegister<DT>' being compiled
with
[
DT=int
]
Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\assignment\assignment\Debug\BuildLog.htm"
assignment - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

hey I debugged it, thanks for the help though bazzy
Topic archived. No new replies allowed.