#include "iostream"
using namespace std;
template<class T>
class BinaryTree;
template<class T>
class BinaryTreeNode
{
friend class BinaryTree<T>;
friend void visit(BinaryTreeNode<T> *);
public:
BinaryTreeNode() { left = right = 0 ;}
BinaryTreeNode(const T& e){ data = e; left=right=0;}
BinaryTreeNode(const T& e,BinaryTreeNode *l,BinaryTreeNode *r)
{
data = e;
left = l;
right = r;
}
private:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
};
template<class T>
class BinaryTree
{
public:
BinaryTree() { root = 0; };
~BinaryTree(){};
bool isEmpty() const { return ((root)?false:true);}
bool Root(T&x) const;
void makeTree(const T& element,BinaryTree<T> &left, BinaryTree<T>& rigth);
void breakTree( T&element, BinaryTree<T> &left, BinaryTree<T> & rigth);
void preOrder( void(*visit)(BinaryTreeNode<T> *u))
{
preOrder(visit,root);
}
void inOrder( void(*visit) (BinaryTreeNode<T> *u))
{
inOrder(visit,root);
}
private:
BinaryTreeNode<T> *root;
void preOrder( void(*visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void inOrder( void(*visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
};
template<class T>
bool BinaryTree<T>::Root(T &x) const
{
if(root)
{
x = root->data;
return true;
}
else return false;
}
template<class T>
void BinaryTree<T>::makeTree(const T&element, BinaryTree<T> &left, BinaryTree<T> & rigth)
{
root = new BinaryTreeNode<T>(element,left.root,rigth.root);
left.root = right.root = 0;
}
template<class T>
void BinaryTree<T>::breakTree( T &element, BinaryTree<T> &left, BinaryTree<T> & rigth)
{
if(!root) { cout<<"空树"<<endl; exit(1); }
element = root->data;
left.root = root->left;
rigth.root = root->right;
delete root;
root = 0;
}
//先序列遍历二叉树
template<class T>
void BinaryTree<T>::preOrder( void(*visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)
{
if(t)
{
visit(t);
preOrder(visit,t->left);
preOrder(visit,t->right);
}
}
//中序列遍历二叉树
template<class T>
void BinaryTree<T>::inOrder(void(*visit)(BinaryTreeNode<T> *u),BinaryTreeNode<T> *t)
{
if(t)
{
inOrder(visit,t->left);
Visit(t);
inOrder(visit,t->right);
}
}
/*
int count = 0;
BinaryTree<int> a,x,y,z;
template<class T>
void ct(BinaryTreeNode<T> *t) { count++;}
int main()
{
y.makeTree(1,a,a);
z.makeTree(2,a,a);
x.makeTree(3,y,z);
y.makeTree(4,x,a);
y.preOrder(ct);
cout<< count <<endl;
system("pause");
return 0;
}
*/
哪里语法出错了
[此贴子已经被作者于2005-11-4 12:46:55编辑过]