### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````template 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 BinaryNode *BinarySearchTree::findMin(BinaryNode *root)const{ if(root == NULL){ return NULL; } if(root->left == NULL){ return root; } return findMin(root->left); } template BinaryNode *BinarySearchTree::findMax(BinaryNode *root)const{ if(root != NULL){ while(root->right != NULL){ root = root->right; } } return root; } template void BinarySearchTree::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 void BinarySearchTree::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 void BinarySearchTree::insert(const T &data){ insert(data, root); } template void BinarySearchTree::remove(const T &data){ remove(data, root); } template const T &BinarySearchTree::findMin()const{ return findMin(root); } template const T &BinarySearchTree::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:
 ``12`` ``````template typename BinarySearchTree::BinaryNode* BinarySearchTree::findMin(BinaryNode *root)const ``````

Second, use C++11 trailing return type:
 ``12`` ``````template auto BinarySearchTree::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.