64,645
社区成员
发帖
与我相关
我的任务
分享
//先序遍历
template <class type>
void BinaryTree<type>::PreOrder(BinTreeNode<type>* current/*,void(*Operation)(BinTreeNode<type>* )*/)
{
if(current!=NULL)
{
// Operation(current);
cout<<current->data<<'\t';
PreOrder(current->LeftChild);
PreOrder(current->RightChild);
}
}
//主函数
myTree.PreOrder(myTree.root);
//先序遍历
template <class type>
void BinaryTree<type>::PreOrder(BinTreeNode<type>* current/*,void(*Operation)(BinTreeNode<type>* )*/)
{
if(current!=NULL)
// Operation(current);
cout<<current->data<<'\t';
PreOrder(current->LeftChild);
PreOrder(current->RightChild);
}
//主函数
myTree.PreOrder(myTree.root);
//
void Fun(BinTreeNode<char>* p)
{
cout<<p->data<<' ';
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<Fun<<endl;
void (*Operation) (BinTreeNode<char>* p); //定义函数指针
Operation=Fun;
BinaryTree<char> myTree;
// cout<<&myTree<<endl;
// cout<<myTree.root<<endl;
myTree.InOrder(myTree.root,Fun);
return 0;
}
template <class type> class BinaryTree
{
public:
BinTreeNode<type>* root; //根结点
public:
BinaryTree(){ root=CreateBinTree( );}
//创建一棵二叉树
template <class type>BinTreeNode<type>* BinaryTree<type>::CreateBinTree()
{
type datax;
cin>>datax;
BinTreeNode<type>* p;
if(datax=='*') //读*时相应结点为空
p=NULL;
else
{
p=new BinTreeNode<type>;
p->data=datax;
p->LeftChild=CreateBinTree();
p->RightChild=CreateBinTree();
}
return p;
}
//中序遍历
template <class type>
void BinaryTree<type>::InOrder(BinTreeNode<type>* current,void(*Operation)(BinTreeNode<type>* ))
{
InOrder(current->LeftChild,Operation);
if(current!=NULL)
Operation(current);
InOrder(current->RightChild,Operation);
}
template<class T>
void BiTree::PreOder(BiNode<T> *root)
{
if(root==NULL) return;
else
{
cout<<root->data;
PreOder(root->lchild);
PreOder(root->rchild);
}
}
template<class T>
struct BiNode
{
T data;
BiNode<T> *lchild,rchild;
};
class BiTree
{
public:
BiTree(BiNode<T> *root);
private:
BiNode<T> *root;
void Creat(BiNode<T> *root)
};
template<class T>
BiTree::BiTree(BiNode<T> *root)
{
root=Creat();
}
BiNode<T> *BiTree::Creat()
{
cin>>ch;
if(ch=='#') return NULL;
else
{
root=new BiNode<T>;
root->data=ch;
root->lchild=Creat();
root->rchild=Creat();
}
}
type datax; //如果type 是int,'*'也能够读取近来?
cin>>datax;
BinTreeNode <type>* p;
if(datax=='*') //读*时相应结点为空
p=NULL;
int _tmain(int argc, _TCHAR* argv[])
{
cout < <Fun < <endl;
void (*Operation) (BinTreeNode <int>* p); //定义函数指针
Operation=Fun; //Fun函数的地址传递给Operation
BinaryTree <int> myTree;
cout < <"please input the node data:('*' stands for null)" < <endl;
myTree.CreateBinTree();
myTree.PreOrder(myTree.root,Operation); //将Fun函数通过Operation传递进去
return 0;
}