//Binary Tree
//Compiled with Microsoft Visual C++ 6.0
typedef enum {one=1,two} timesPushed;
#include <string>
#include "CTStack.h" //my simple stack
using namespace std; //basic_string<...>
template <class T>
class BinTreeNode {
public:
BinTreeNode(T dat,BinTreeNode<T> *left=0,BinTreeNode<T> *right=0)
{
data=dat;
llink=left;
rlink=right;
}
BinTreeNode():llink()),rlink(0){}
BinTreeNode *llink,*rlink;
T data;
};
template <class T>
class BinTree {
public:
BinTreeNode<T> *root;
BinTree():root(0){}
~BinTree(){destroy();}
bool creat(const T *pre_,const T *in_)
{
string pre(pre_),in(in_);
return creat(pre,in);
}
bool creat(basic_string<T> &pre,basic_string<T> &in)
{
BinTreeNode<T> *curNode,*newNode;
stack<BinTreeNode<T> *> nodes;
stack<unsigned> position;
unsigned inPos,prePos,prelen,inlen,curPos;
prelen=pre.length();
inlen=in.length();
if (prelen!=inlen) return false;
if (prelen*inlen==0) return true;
for (inPos=0;inPos<inlen;++inPos)
{
if (in[inPos]==pre[0]) break;
}
if (inPos==inlen)
return false;
root=new BinTreeNode<T>(pre[0]);
nodes.push(curNode=root);
position.push(curPos=inPos);
for (prePos=1;prePos<prelen;++prePos)
{
for (inPos=0;inPos<inlen;++inPos)
{
if (in[inPos]==pre[prePos]) break;
}
if (inPos==inlen)
return false;
newNode=new BinTreeNode<T>(pre[prePos]);
if (inPos<curPos)
nodes.gettop()->llink=newNode;
else {
while
(!position.isEmpty()&&inPos>position.gettop())
{
curNode=nodes.pop();
curPos=position.pop();
}
curNode->rlink=newNode;
}
nodes.push(newNode);
position.push(curPos=inPos);
}
return true;
}
bool postOrderTraverse(bool (*visitor)(BinTreeNode<T> *))
{
stack<BinTreeNode<T> *> pointers;
stack<timesPushed> seen;
BinTreeNode<T> *curPtr=root;
timesPushed pushed;
while (curPtr!=0||!pointers.isEmpty())
{
if (curPtr!=0) {
pointers.push(curPtr);
seen.push(one);
curPtr=curPtr->llink;
}
else {
curPtr=pointers.pop();
pushed=seen.pop();
if (pushed==one) {
pointers.push(curPtr);
seen.push(two);
curPtr=curPtr->rlink;
}
else {
if (!(*visitor)(curPtr))
return false;
curPtr=0;
}
}
}
return true;
}
private:
bool deleteHelper(BinTreeNode<T> *ptr)
{
delete ptr;
return ture;
}
};
Compiling...
TemplateFunctionPointers.cpp
F:\Algorithms\Experiments\TemplateFunctionPointers .cpp(104):
error C2664: 'postOrderTraverse' : cannot convert parameter 1 from 'bool
(class BinTreeNode *)' to 'bool (__cdecl *)(class BinTreeNode *)'
None of the functions with this name in scope match the target type
F:\Algorithms\Experiments\TemplateFunctionPointers .cpp(103) : while
compiling class-template member function 'void __thiscall
BinTree::destroy(void)'
Error executing cl.exe.