H
hn.ft.pris
I have the following code:
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template <typename T> class Tree{
private:
Tree<T> *left;
Tree<T> *right;
T data;
public:
Tree(const T& item, Tree<T>* lptr, Tree<T>*
rptr):data(item),left(lptr),right(rptr){};
Tree<T>* Left() const;
Tree<T>* Right() const;
};
#endif
#######################################
Tree.cpp implemented Tree<T>* Left() const and Tree<T>* Right() const
###############FILE Tree.cpp ###########
#include "Tree.h"
#include <iostream>
using namespace std;
template <typename T> Tree<T>* Tree<T>::Left() const{
return left;
}
template <typename T> Tree<T>* Tree<T>::Right() const{
return right;
}
########################################
Tree_Scan.h defines a "inorder" scan function on the binary search tree
################## FILE Tree_Scan.h#########
#ifndef TREE_SCAN_H
#define TREE_SCAN_H
#include "Tree.h"
//using namespace std;
template <typename T> void inorder(Tree<T>*);
#endif
###########################################
Tree_Scan.cpp implemented "inorder" function
#################FILE Tree_Scan.cpp#########
#include "Tree.h"
#include "Tree_Scan.h"
#include <iostream>
using namespace std;
template <typename T> void inorder(Tree<T>* tree){
if(tree != NULL){
inorder(tree->left());
cout << (*tree).data << endl;
inorder(tree ->right());
}
}
###########################################
In the main function, first a binary search tree is built and then
perform an "inorder" scan
on it
##################FILE main.cpp#############
#include "Tree.h"
#include "Tree_Scan.h"
#include <string>
using namespace std;
int main(){
Tree<string> *g = new Tree<string>("G", NULL, NULL);
Tree<string> *h = new Tree<string>("H", NULL, NULL);
Tree<string> *i = new Tree<string>("I", NULL, NULL);
Tree<string> *f = new Tree<string>("F", NULL, NULL);
Tree<string> *d = new Tree<string>("D", NULL, g);
Tree<string> *b = new Tree<string>("B", d, NULL);
Tree<string> *e = new Tree<string>("E", h, i);
Tree<string> *c = new Tree<string>("C", e, f);
Tree<string> *a = new Tree<string>("A", b, c);
inorder(a);
return 1;
}
##################################################
When linking a got a link error, complain that there is something wrong
with "inorder(a)", I've
checked my program several times and find no problem, why it fails?
Thanks for helping me!
Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template <typename T> class Tree{
private:
Tree<T> *left;
Tree<T> *right;
T data;
public:
Tree(const T& item, Tree<T>* lptr, Tree<T>*
rptr):data(item),left(lptr),right(rptr){};
Tree<T>* Left() const;
Tree<T>* Right() const;
};
#endif
#######################################
Tree.cpp implemented Tree<T>* Left() const and Tree<T>* Right() const
###############FILE Tree.cpp ###########
#include "Tree.h"
#include <iostream>
using namespace std;
template <typename T> Tree<T>* Tree<T>::Left() const{
return left;
}
template <typename T> Tree<T>* Tree<T>::Right() const{
return right;
}
########################################
Tree_Scan.h defines a "inorder" scan function on the binary search tree
################## FILE Tree_Scan.h#########
#ifndef TREE_SCAN_H
#define TREE_SCAN_H
#include "Tree.h"
//using namespace std;
template <typename T> void inorder(Tree<T>*);
#endif
###########################################
Tree_Scan.cpp implemented "inorder" function
#################FILE Tree_Scan.cpp#########
#include "Tree.h"
#include "Tree_Scan.h"
#include <iostream>
using namespace std;
template <typename T> void inorder(Tree<T>* tree){
if(tree != NULL){
inorder(tree->left());
cout << (*tree).data << endl;
inorder(tree ->right());
}
}
###########################################
In the main function, first a binary search tree is built and then
perform an "inorder" scan
on it
##################FILE main.cpp#############
#include "Tree.h"
#include "Tree_Scan.h"
#include <string>
using namespace std;
int main(){
Tree<string> *g = new Tree<string>("G", NULL, NULL);
Tree<string> *h = new Tree<string>("H", NULL, NULL);
Tree<string> *i = new Tree<string>("I", NULL, NULL);
Tree<string> *f = new Tree<string>("F", NULL, NULL);
Tree<string> *d = new Tree<string>("D", NULL, g);
Tree<string> *b = new Tree<string>("B", d, NULL);
Tree<string> *e = new Tree<string>("E", h, i);
Tree<string> *c = new Tree<string>("C", e, f);
Tree<string> *a = new Tree<string>("A", b, c);
inorder(a);
return 1;
}
##################################################
When linking a got a link error, complain that there is something wrong
with "inorder(a)", I've
checked my program several times and find no problem, why it fails?
Thanks for helping me!