#include #include #include template class BinaryTree; template class BinaryNode{ //结点结构 friend class BinaryTree; public: BinaryNode():left(0),right(0){} BinaryNode(Type item,BinaryNode *L=0,BinaryNode *R=0):data(item),left(L),right(R){} ~BinaryNode(){} Type GetData()const {return data;} BinaryNode *GetLeft() const {return left;} BinaryNode *GetRight() const {return right;} void SetData(const Type &item) {data = item;} void SetLeft(BinaryNode *L) {left = L;} void SetRight(BinaryNode *R) {right = R;} int Size(const BinaryNode *T) const; int Hight(const BinaryNode *T) const; void PrintPreOrder() const; //先序遍历(输出) void PrintPostOrder() const; //后序遍历(输出) void PrintInOrder() const; //中序遍历(输出) BinaryNode *Duplicate() const; private: Type data; BinaryNode *left, *right; }; template BinaryNode *BinaryNode::Duplicate() const{ BinaryNode *root = new BinaryNode(data); if(left != 0) root->left = left->Duplicate(); if(right != 0) root->right = right->Duplicate(); return root; } template void BinaryNode::PrintPreOrder() const { //先序遍历(输出) cout << setw(3) << data; if(left != 0) left->PrintPreOrder(); if(right != 0) right->PrintPreOrder(); } template void BinaryNode::PrintInOrder() const { //中序遍历(输出) if(left != 0) left->PrintInOrder(); cout << setw(3) << data; if(right != 0) right->PrintInOrder(); } template void BinaryNode::PrintPostOrder() const { //后序遍历(输出) if(left != 0) left->PrintPostOrder(); if(right != 0) right->PrintPostOrder(); cout << setw(3) << data; } template Type Max(const Type u, const Type v){ if(u > v) return u; else return v; } template int BinaryNode::Size(const BinaryNode *T) const{ if(T == o) return 0; else return 1 + Size(T->left) + Size(T->right); } template int BinaryNode::Hight(const BinaryNode *T) const{ if(T == o) return 0; else return 1 + Max(Hight(T->left) + Hight(T->right)); } template class BinaryTree{ public: BinaryTree():root(0){} BinaryTree(const Type &value){root = new BinaryNode(value);} ~BinaryTree(){DelTree(root);} bool IsEmpty() const {return root == 0;} const BinaryNode *Getroot() const {return root;} void MakeEmpty(){DelTree(root); root = 0;} const BinaryTree &operator=(const BinaryTree &T); private: BinaryNode *root; BinaryTree(const BinaryTree &); void DelTree(BinaryNode *T); }; template const BinaryTree &BinaryTree::operator=(const BinaryTree &T){ if(this != &T){ DelTree(root); if(T.root != 0) root = T.root->Duplicate(); } return *this; } template BinaryTree::BinaryTree(const BinaryTree &T){ root = T.root->Duplicate(); } template void BinaryTree::DelTree(BinaryNode *T){ if(T != 0){ DelTree(T->left); DelTree(T->right); delete T; } } void main(void) { ; //按实际情况给出 }