Help With Binary Search Tree

Hello all, this is the first time I've posted in this forum so i'll get straight to the point. I'm trying to create a template binary search tree and I'm getting all these vague errors that I have no clue how to solve and I was wondering if anyone could help me out. I've narrowed it down to my findMax and findMin functions but i can't figure it out any further than that. any help would be great. Thanks in advanced.


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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
template<class T>
class BinarySearchTree{

	private:

		struct BinaryNode{

			T data;
			BinaryNode *left;
			BinaryNode *right;

			BinaryNode(const T &data, BinaryNode *left, BinaryNode *right);
		};

		BinaryNode *root;

		void insert(const T &data, BinaryNode *&root) const;
		void remove(const T &data, BinaryNode *&root) const;
		BinaryNode *findMin(BinaryNode *root)const;
		BinaryNode *findMax(BinaryNode *root)const;

	public:

		BinarySearchTree();
		BinarySearchTree(const BinarySearchTree &obj);
		~BinarySearchTree();

		void insert(const T &data);
		void remove(const T &data);
		const T &findMin()const;
		const T &findMax()const;

		const BinarySearchTree &operator=(const BinarySearchTree &obj);
};

template<class T>
BinaryNode *BinarySearchTree<T>::findMin(BinaryNode *root)const{

	if(root == NULL){
		return NULL;
	}
	if(root->left == NULL){
		return root;
	}

	return findMin(root->left);
}

template<class T>
BinaryNode *BinarySearchTree<T>::findMax(BinaryNode *root)const{

	if(root != NULL){

		while(root->right != NULL){
			root = root->right;
		}
	}

	return root;
}

template<class T>
void BinarySearchTree<T>::insert(const T &data, BinaryNode *&root)const{

	if(root == NULL){

		root = new BinaryNode(data, NULL, NULL);
	}
	else if(data < root->data){
		insert(data, root->left);
	}
	else if(root->data < data){
		insert(data, root->right);
	}
}

template<class T>
void BinarySearchTree<T>::remove(const T &data, BinaryNode *&root)const{

	if(root == NULL){

		return;
	}
	if(data < root->data){
		remove(data, root->left );
	}
	else if(root->data < data){
		remove(data, root->right);
	}
	else if(root->left != NULL && root->right != NULL){

		root->data = findMin(root->right)->data;
		remove(root->data, root->right);
	}
	else{

		BinaryNode *oldNode = root;
		root = (root->left != NULL)? root->left : root->right;
		delete oldNode;
	}
}

template<class T>
void BinarySearchTree<T>::insert(const T &data){

	insert(data, root);
}

template<class T>
void BinarySearchTree<T>::remove(const T &data){

	remove(data, root);
}

template<class T>
const T &BinarySearchTree<T>::findMin()const{

	return findMin(root);
}

template<class T>
const T &BinarySearchTree<T>::findMax()const{

	return findMax(root);
}




and here is are the errors I'm getting from this header file.

1>------ Build started: Project: Programming Assignment 2, Configuration: Debug Win32 ------
1> main.cpp
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2065: 'T' : undeclared identifier
: error C2923: 'BinarySearchTree' : 'T' is not a valid template type argument for parameter 'T'
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2086: 'int BinaryNode' : redefinition
: see declaration of 'BinaryNode'
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2065: 'T' : undeclared identifier
: error C2923: 'BinarySearchTree' : 'T' is not a valid template type argument for parameter 'T'
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2086: 'int BinaryNode' : redefinition
: see declaration of 'BinaryNode'

1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
BinaryNode is defined in the scope of the class BinarySearchTree. So when in lines 37 and 50 you write BinaryNode, it is undefined yet (you didn't enter the BinarySearchTree scope yet). There are two ways to solve this problem. First, provide a full name for BinaryNode:
1
2
template<class T>
typename BinarySearchTree<T>::BinaryNode*  BinarySearchTree<T>::findMin(BinaryNode *root)const 


Second, use C++11 trailing return type:
1
2
template<class T>
auto BinarySearchTree<T>::findMax(BinaryNode *root)const -> BinaryNode*

The trick is that with trailing return type compiler already knows the scope (BinarySearchTree<T>) and can correctly resolve the name BinaryNode defined within BinarySearchTree.
Thanks so much, been trying to figure out what was going on for about 3 hours now. Really helped a lot. Its for a homework assignment and its due in less than 24 hours. You have my thanks.
Topic archived. No new replies allowed.