template struct node { T inf; node *Left, *Right; }; template class tree { public: tree(); ~tree(); tree(tree const &); tree& operator=(tree const &); bool empty() const; T RootTree() const; tree LeftTree() const; tree RightTree() const; void Create3(T const &, tree const &, tree const &); void print() const { pr(root); cout << endl; } void Create() { CreateTree(root); } private: node *root; void DeleteTree(node* &) const; void Copy(node * &, node * const &) const; void CopyTree(tree const &); void pr(const node *) const; void CreateTree(node * &) const; }; template tree::tree() { root = NULL; } template tree::~tree() { DeleteTree(root); } template tree::tree(tree const & r) { CopyTree(r); } template tree& tree::operator=(tree const & r) { if(this != &r) { DeleteTree(root); CopyTree(r); } return *this; } template void tree::DeleteTree(node * &p) const { if(p) { DeleteTree(p->Left); DeleteTree(p->Right); delete p; p = NULL; } } template void tree::CopyTree(tree const & r) { Copy(root, r.root); } template void tree::Copy(node * & pos, node * const & r) const { pos = NULL; if(r) { pos = new node; pos->inf = r->inf; Copy(pos->Left, r->Left); Copy(pos->Right, r->Right); } } template bool tree::empty() const { return NULL == root; } template T tree::RootTree() const { return root->inf; } template tree tree::LeftTree() const { tree t; Copy(t.root, root->Left); return t; } template tree tree::RightTree() const { tree t; Copy(t.root, root->Right); return t; } template void tree::pr(const node* p) const { if(p) { pr(p->Left); cout << p->inf << " "; pr(p->Right); } } template void tree::Create3(T const & x, tree const & l, tree const & r) { if(root) DeleteTree(root); root = new node; root->inf = x; Copy(root->Left, l.root); Copy(root->Right, r.root); } template void tree::CreateTree(node * & pos) const { T x; char c; cout << "root: "; cin >> x; pos = new node; pos->inf = x; pos->Left = NULL; pos->Right = NULL; cout << "Left Tree of: " << x << " y/n? "; cin >> c; if(c == 'y') CreateTree(pos->Left); cout << "Right Tree of: " << x << " y/n? "; cin >> c; if(c == 'y') CreateTree(pos->Right); }