数据结构——二叉树2(c++)

BinaryTreeNode.h

/*  BinaryTreeNode.h  */ 


template <class T> class BinaryTree; 

template <class T> 
class BinaryTreeNode { 
friend class BinaryTree<T>; //声明二叉树为结点类的友元类,便于访问私有数据成员 
private: 
    T  info;                                //二叉树结点数据域 
    BinaryTreeNode<T>* left;                //二叉树结点指向左子树的指针 
    BinaryTreeNode<T>* right;               //二叉树结点指向左子树的指针 

public: 
    BinaryTreeNode();                           //缺省构造函数 
    BinaryTreeNode(const T& inf);               //给定数据的构造函数 
    BinaryTreeNode(const T& inf,BinaryTreeNode<T>* l, BinaryTreeNode<T>* r);//给定了结点值和左右子树的构造函数 
    T  value() const;                           //返回当前结点的数据 
    BinaryTreeNode<T>*  leftchild() const;      //返回当前结点左子树 
    BinaryTreeNode<T>*  rightchild() const;     //返回当前结点右子树 
    void  setLeftchild(BinaryTreeNode<T>*) ;    //设置当前结点的左子树 
    void  setRightchild(BinaryTreeNode<T>*) ;   //设置当前结点的右子树 
    void  setValue(const T& val);               //设置当前结点的数据域 
    bool  isLeaf() const;               //判定当前结点是否为叶结点,若是返回true 
    BinaryTreeNode<T>& operator = (const BinaryTreeNode<T>& Node){this = Node;};//重载赋值操作符 
}; 


//****** BinaryTreeNode Implementation *******// 

template<class T> 
BinaryTreeNode<T>::BinaryTreeNode()  { 
    left = right = NULL; 
} 

template<class T> 
BinaryTreeNode<T>::BinaryTreeNode(const T& inf)  {  //给定数据的构造函数 
    info = inf; 
    left = right = NULL; 
} 

template<class T> 
BinaryTreeNode<T>::BinaryTreeNode(const T& inf,BinaryTreeNode* l, BinaryTreeNode* r)  {//给定数据的左右指针的构造函数 
    info = inf; 
    left = l; 
    right = r; 
} 

template<class T> 
T  BinaryTreeNode<T>::value() const  { 
    return info;  
}    

template<class T> 
BinaryTreeNode<T>*  BinaryTreeNode<T>::leftchild() const  { //返回当前结点指向左子树的指针 
    return left; 
}                                                

template<class T> 
BinaryTreeNode<T>*  BinaryTreeNode<T>::rightchild() const  { //返回当前结点指向右子树的指针 
    return right;                                
}            

template<class T> 
void  BinaryTreeNode<T>::setLeftchild(BinaryTreeNode<T>* subroot)  { //设置当前结点的左子树 
    left = subroot; 
} 

template<class T> 
void  BinaryTreeNode<T>::setRightchild(BinaryTreeNode<T>* subroot)  { //设置当前结点的右子树 
    right = subroot; 
} 

template<class T> 
void  BinaryTreeNode<T>::setValue(const T& val)  {  //设置当前结点的数据域 
    info = val;  
}                                    

template<class T> 
bool  BinaryTreeNode<T>::isLeaf() const  {  //判定当前结点是否为叶结点,若是返回true 
    return (left == NULL) && (right == NULL);  
} 

BinaryTree.h

//************BinaryTree.h****************// 

#include <stack> 
#include <queue> 
#include "BinaryTreeNode.h" 

enum Tags{Left,Right};          //枚举类型 

template <class T> 
class StackElement  {           //StackElement,用于非递归方式遍历二叉树 
public: 
    BinaryTreeNode<T>* pointer; 
    Tags tag; 
};

using namespace std; 

template <class T> 
class BinaryTree { 
private: 
    BinaryTreeNode<T>*  root;                       //二叉树根结点 
    T flag;                                         //CreateBinaryTree();创建树时空子树标志
public: 
    BinaryTree(){root = NULL;};                     //构造函数 
    ~BinaryTree() {DeleteBinaryTree(root);};       //析构函数 
    bool isEmpty() const{return ( root? false : true); };//判定二叉树是否为空树 
    BinaryTreeNode<T>* Root(){return root;};        //返回二叉树根结点  
    void setEmptySubtreeFlag(T Value){flag=Value;}  //设置空子树标志

    BinaryTreeNode<T>* createBinaryTree();          //输入前序序列创建二叉树,返回指向所创建的二叉树的根结点的指针                
    BinaryTreeNode<T>* seqToLinked(T A[ ],int i,int n);
                                                    //由顺序存储的n个结点的完全二叉树,建立二叉链表存储的二叉树
    int Level(BinaryTreeNode<T>* root);             //求二叉树深度(根结点处于第一层)
    int LeafNum(BinaryTreeNode<T>* root);           //求二叉树的叶子数

    void DeleteBinaryTree(BinaryTreeNode<T>* root); //删除二叉树或其子树 
    void Visit(T Value) {cout << Value<<" ";};           //访问 
    void PreOrder(BinaryTreeNode<T>* root);         //前序周游二叉树或其子树
}; 

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::seqToLinked(T A[ ],int i,int n)
{   
    BinaryTreeNode<T> * tmp;
    tmp=new BinaryTreeNode<T>(A[i]);
    if(2*i+1<n)
    tmp->left =seqToLinked(A,2*i+1,n);              //递归创建左子树
    if(2*i+2<n)
    tmp->right=seqToLinked(A,2*i+2,n);              //递归创建右子树
    return root=tmp;

}     

template<class T> 
int BinaryTree<T>::Level(BinaryTreeNode<T>* root){  //求二叉树深度(根结点处于第一层)

    int depth=0;    //深度
    int temp1=0;
    int temp2=0;
    if(root==NULL)
    {
        return 0;
    }
    else
    {
        temp1=Level(root->leftchild());
        temp2=Level(root->rightchild());
        depth=temp1>temp2?temp1:temp2;
        depth++;

        return depth;
    }

}

template<class T> 
int BinaryTree<T>::LeafNum(BinaryTreeNode<T>* root){    //求二叉树的叶子数
    if(root==NULL)
    {
        return 0;
    }
    else if(root->leftchild()==NULL && root->rightchild()==NULL)
    {
        return 1;
    }
    else
    {
        return LeafNum(root->leftchild())+LeafNum(root->rightchild());  //左子树的叶子结点+右子树的叶子结点
    }

}

template<class T> 
BinaryTreeNode<T>*  BinaryTree<T>:: createBinaryTree()  { 
    //按前序构造二叉链表表示的二叉树root 
    BinaryTreeNode<T> * N;
    T ch;
    cin>>ch;
    if(ch==flag)N=NULL;
    else{
        N = new BinaryTreeNode<T>(ch);              //创建新树 
        N->left =createBinaryTree();                //递归创建左子树
        N->right=createBinaryTree();                //递归创建右子树
    }
    return root=N;
}

template<class T> 
void BinaryTree<T>:: DeleteBinaryTree(BinaryTreeNode<T>* root)  { //以后序周游的方式删除二叉树 
    if(root)  { 
        DeleteBinaryTree(root->left);               //递归删除左子树 
        DeleteBinaryTree(root->right);              //递归删除右子树 
        delete root;                                //删除根结点 
    } 
} 



template<class T> 
void BinaryTree<T>::PreOrder (BinaryTreeNode<T>* root)  {  //递归前序周游二叉树 
    if(root!=NULL)
    {
        Visit(root->info);
        PreOrder(root->leftchild());
        PreOrder(root->rightchild());
    }

} 

BinaryTree.cpp

//BinaryTree.cpp
#include <iostream>    
#include "BinaryTree.h"   

int main() {   

    BinaryTree<char> a;
    char value;
    char A[]={'0','1','2','3','4','5','6','7','8','9'}; //完全二叉树顺序存储,不用第1个元素,使用时下标从1开始,用后9个元素
    //利用数组A创建二叉链式存储表示的二叉树
    BinaryTreeNode<char> * root=a.seqToLinked(A,0,10);
    cout<<"先序遍历此二叉树:";
    a.PreOrder(root);
    cout<<endl;
    int depth;  //深度
    int leafNum;    //叶子结点数
    depth=a.Level(root);
    cout<<"此二叉树的深度为:"<<depth<<endl;
    leafNum=a.LeafNum(root);
    cout<<"此二叉树的叶子结点数为:"<<leafNum<<endl;
    return 0;
}  


这里写图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值