Tree.h

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值