By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

c++ trees

P: 1
can anyone help? i think there is an error wif the insert and search part. no .cpp file everything must be done in this .h file.

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

// There is no implementation file (*.cpp) if you want to use class template...
// Everything must be defined in the *.h file.

template <class T1,class T2>
class BSTNode {
public:
BSTNode(T1 _Key,T2 _Value) {
Key = _Key;
Value = _Value;
LeftSubTree = NULL;
RightSubTree = NULL;
}

// These variables are purposely set to be public...
T1 Key;
T2 Value;
BSTNode<T1,T2>* LeftSubTree;
BSTNode<T1,T2>* RightSubTree;
};

template <class T1,class T2>
class BST {
public:
BST() {
root = NULL;
}

void INSERT(T1 _Key,T2 _Value) {
root = INSERT(root,_Key,_Value);
}

BSTNode<T1,T2>* SEARCH(T1 _Key) {
return SEARCH(root,_Key);
}

int SIZE() {
return SIZE(root);
}

void Print_Inorder() {
Print_Inorder(root);
}


private:
BSTNode<T1,T2>* INSERT(BSTNode<T1,T2>* Node,T1 _Key,T2 _Value) {
if (Node== NULL)
{
Node=new BSTNode(T1 _Key,T2 _Value);
return Node;
}
else
if(T1 _Key->Key<Node->Key)
T1 _Key->LeftSubTree = insert(Node->LeftSubTree,T1 _Key,T2 _Value);
else
if(Node>T1 _Key->Key)
T1 _Key->RightSubTree = insert(Node->RightSubTree,T1 _Key,T2 _Value);
else
return T1 _Key;
}

BSTNode<T1,T2>* SEARCH(BSTNode<T1,T2>* Node,T1 _Key) {
if(T1 _Key==NULL)
return null;
if(T1 _Key==Node->Key)
return T1 _Key;
else
if(T1 _Key<Node->Key )
return search(Node->LeftSubTree,T1 _Key);
else
return search(Node->RightSubTree,T1 _Key);
}

int SIZE(BSTNode<T1,T2>* Node) {
if(Node==NULL)
return 0;
else
return 1+SIZE(Node->LeftSubTree)+SIZE(Node->RightSubTree)
}

void Print_Inorder(BSTNode<T1,T2>* Node) {
// Don't tweak this function please, otherwise you will get different output!
if (Node == NULL)
return;

Print_Inorder(Node->LeftSubTree);
cout << "Hobby: " << Node->Key << "." << endl;
cout << " Names:";
for (vector<string>::iterator i=Node->Value.begin(); i!=Node->Value.end(); i++)
cout << " " << *i;
cout << "." << endl;
Print_Inorder(Node->RightSubTree);
}

...
Mar 13 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.