#ifndef H_Tree
#define H_Tree
#include<iostream>
#include<cassert>
using namespace std;
template<class Type>
struct nodeType{
Type info;
nodeType<Type>*llink;
nodeType<Type>*rlink;
};
template<class Type>
class TreeType{
public:
const TreeType<Type>& operator=(const TreeType<Type>&);
bool isEmpty();
void inorderTraversal();
void preorderTraversal();
void postorderTraversal();
int treeHeight();
int treeNodeCount();
int treeLeavesCount();
void destroyTree();
TreeType(const TreeType<Type>& otherTree);
TreeType();
~TreeType();
protected:
nodeType<Type>*root;
private:
void copyTree(nodeType<Type>* &copiedTreeRoot,nodeType<Type>* otherTreeRoot);
void inorder(nodeType<Type>*p);
void preorder(nodeType<Type>*p);
void postorder(nodeType<Type>*p);
int height(nodeType<Type>*p);
int nodeCount(nodeType<Type>*p);
int leavesCount(nodeType<Type>*p);
void destroy(nodeType<Type>* &p);
int max(int x,int y);
};
template<class Type>
bool TreeType<Type>::isEmpty()
{
return(root==0);
}
template<class Type>
TreeType<Type>::TreeType()
{
root=NULL;
}
template<class Type>
void TreeType<Type>::inorderTraversal()
{
inorder(root);
}
template<class Type>
void TreeType<Type>::preorderTraversal()
{
preorder(root);
}
template<class Type>
void TreeType<Type>::postorderTraversal()
{
postorder(root);
}
template<class Type>
int TreeType<Type>::treeHeight()
{
return height(root);
}
template<class Type>
int TreeType<Type>::treeNodeCount()
{
return nodeCount(root);
}
template<class Type>
int TreeType<Type>::treeLeavesCount()
{
return leavesCont(root);
}
template<class Type>
void TreeType<Type>::inorder(nodeType<Type>*p)
{
if(p!=NULL)
{
inorder(p->llink);
cout<<p->info<<" ";
inorder(p->rlink);
}
}
template<class Type>
void TreeType<Type>::preorder(nodeType<Type>*p)
{
if(p!=NULL)
{
cout<<p->info<<" ";
preorder(p->llink);
preorder(p->rlink);
}
}
template<class Type>
void TreeType<Type>::postorder(nodeType<Type>*p)
{
if(p!=NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<" ";
}
}
template<class Type>
int TreeType<Type>::height(nodeType<Type>*p)
{
if(p==NULL)
return 0;
else
return(1+max(height(p->llink),height(p->rlink)));
}
template<class Type>
int TreeType<Type>::max(int x,int y)
{
if(x>=y)
return x;
else
return y;
}
template<class Type>
int TreeType<Type>::nodeCount(nodeType<Type>*p)
{
int count=0;
if(p==NULL)
return 0;
else{
return nodeCount(p->llink)+nodeCount(p->rlink);
}
}
template<class Type>
int TreeType<Type>::leavesCount(nodeType<Type>*p)
{
if(p->llink==NULL&&p->rlink==NULL)
return 1;
else if(p==NULL)
return 0;
else
return (leavesCount(p->llink)+leavesCount(p->rlink));
}
template<class Type>
void TreeType<Type>::copyTree(nodeType<Type>* &copiedTreeRoot,nodeType<Type>* otherTreeRoot)
{
if(otherTreeRoot==NULL)
copiedTreeRoot==NULL;
else{
copiedTreeRoot=new nodeType<Type>;
copiedTreeRoot->info=otherTreeRoot->info;
copyTree(copiedTreeRoot->llink,otherTreeRoot->llink);
copyTree(copiedTreeRoot->rlink,otherTreeRoot->rlink);
}
}
template<class Type>
void TreeType<Type>::destroy(nodeType<Type>* &p)
{
if(p!=NULL)
{
destroy(p->llink);
destroy(p->rlink);
delete p;
p=NULL;
}
}
template<class Type>
void TreeType<Type>::destroyTree(){
destroy(root);
}
template<class Type>
TreeType<Type>::TreeType(const TreeType<Type>& otherTree)
{
if(otherTree.root==NULL)
root=NULL:
else
copyTree(root,otherTree.root);
}
template<class Type>
TreeType<Type>::~TreeType()
{
destroy(root);
}
template<class Type>
const TreeType<Type>&TreeType<Type>::operator=(const TreeType<Type>& otherTree)
{
if(this!=&otherTree){
if(root!=NULL)
destroy(root);
if(otherTree.root==NULL)
root=NULL;
else
copyTree(root,otherTree.root);
}
return *this;
}
#endif