数据结构算法--二叉树

/**/
//    //
//   二叉树数据结构  BinTree.h       //
//    //
/**

#include<iostream.h>
template<class Type>class BinTree;

template<class Type>
class TreeNode
{
protected:
    friend class BinTree<Type>;
    TreeNode():lchild(NULL),rchild(NULL)......{}
    Type data;
    TreeNode *lchild;  //左,右子树
    TreeNode *rchild;
};

template<class Type>
class BinTree
{
    friend void BinTree_PRE(BinTree<Type>& BinTreeOPP);  //友元函数
    friend void BinTree_INO(BinTree<Type>& BinTreeOPP);
    friend void BinTree_POS(BinTree<Type>& BinTreeOPP);
    friend void BinTree_Destroy(BinTree<Type>& BinTreeOPP);
public:
    BinTree():root(NULL)......{}
    void CreatTree();               //创建二叉树,主过程
    void CreatTree(TreeNode<Type>* child,int k); //子过程
    void PreTree(TreeNode<Type> *point);     //先序遍历二叉树
    void InoTree(TreeNode<Type> *point);  //中序遍历二叉树
    void PosTree(TreeNode<Type> *point);  //后序遍历二叉树
    void Destroy(TreeNode<Type> *point);     //销毁二叉树
    bool ISEmpty();
protected:
    TreeNode<Type>* root;
};

template<class Type>
void BinTree<Type>::CreatTree()
{
    CreatTree(root,1);
}

template<class Type>
void BinTree<Type>::CreatTree(TreeNode<Type>* child,int k)
{
    TreeNode<Type>* point;
    point=new TreeNode<Type>;
    cout<<"输入节点数据项 :";
    cin>>point->data;
    switch(k)
    {
    case 1: root=point; break;
    case 2: child->lchild=point;break;
    case 3: child->rchild=point;break;
    }

    char temp;
    cout<<"该"<<point->data<<"节点是否有左子树 Y / 任意键 :";
    cin>>temp;
    if(temp=='y'||temp=='Y')
    {
        CreatTree(point,2);
    }

    cout<<"该"<<point->data<<"节点是否有右子树 Y / 任意键 :";
    cin>>temp;
    if(temp=='y'||temp=='Y')
    {
        CreatTree(point,3);
    }
}

template<class Type>
void BinTree<Type>::PreTree(TreeNode<Type> *point)
{
    if(point!=NULL)
    {
        cout<<" "<<point->data;
        PreTree(point->lchild);
        PreTree(point->rchild);
    }
}

template<class Type>
void BinTree<Type>::InoTree(TreeNode<Type> *point)
{
    if(point!=NULL)
    {

        InoTree(point->lchild);
        cout<<" "<<point->data;
        InoTree(point->rchild);
    }
}

template<class Type>
void BinTree<Type>::PosTree(TreeNode<Type> *point)
{
    if(point!=NULL)
    {

        PosTree(point->lchild);
        PosTree(point->rchild);
        cout<<" "<<point->data;
    }
}

template<class Type>
bool BinTree<Type>::ISEmpty()
{
    return root==NULL;
}

template<class Type>
void BinTree<Type>::Destroy(TreeNode<Type> *point)
{
    if(point!=NULL)
    ...{
        Destroy(point->lchild);
        Destroy(point->rchild);
        delete point;
    }
}

/**//
//    //
//   二叉树功能函数 BinTree.cpp//
//    //
/**/
#include<iostream.h>
#include"BinTree.h"

const int INT =13;
const double FLOAT= 13.33;
const char CHAR ='a';

template<class Type>
void BinTree_CREAT(BinTree<Type>& BinTreeOPP)
{
    BinTreeOPP. CreatTree();
}

template<class Type>
void BinTree_PRE(BinTree<Type>& BinTreeOPP)
{
    if(!BinTreeOPP.ISEmpty())
    {
        cout<<"先序遍历二叉树 : ";
        BinTreeOPP. PreTree(BinTreeOPP.root);
    }
    else
    {
        cout<<"二叉树已经为空!"<<endl;
    }
}

template<class Type>
void BinTree_INO(BinTree<Type>& BinTreeOPP)
{
    if(!BinTreeOPP.ISEmpty())
    {
        cout<<"中序遍历二叉树 : ";
        BinTreeOPP. InoTree(BinTreeOPP.root);
    }
    else
    {
        cout<<"二叉树已经为空!"<<endl;
    }
}

template<class Type>
void BinTree_POS(BinTree<Type>& BinTreeOPP)
{
    if(!BinTreeOPP.ISEmpty())
    {
        cout<<"后序遍历二叉树 : ";
        BinTreeOPP. PosTree(BinTreeOPP.root);
    }
    else
    {
        cout<<"二叉树已经为空!"<<endl;
    }
}

template<class Type>
void BinTree_Destroy(BinTree<Type>& BinTreeOPP)
{
    BinTreeOPP.Destroy(BinTreeOPP.root);
    BinTreeOPP.root=NULL;
    cout<<"二叉树已经销毁!"<<endl;
}

template<class Type>
void BinTree_THREAD(BinTree<Type>& BinTreeOPP)
{
    if(BinTreeOPP.ISThread())
    {
        cout<<"该二叉树已经线索化!!"<<endl;
    }
    else
    {
        BinTreeOPP.ThreadTree();
    }
}

template<class Type>
void BinTree_THROUGH(BinTree<Type>& BinTreeOPP)
{
    BinTreeOPP.Through();
}

template<class Type>
void BinTreeINI(Type temp)
{
    BinTree<Type> BinTreeOPP;

    do
    {
        cout<<"树的操作: "<<endl
            <<" 1) 构造二叉数"<<endl
            <<" 2) 先序遍历二叉树"<<endl
            <<" 3) 中序遍历二叉树"<<endl
            <<" 4) 后序遍历二叉树"<<endl
            <<" 5) 销毁二叉树  "<<endl
            <<" X) 退出二叉树操作"<<endl;
        int item;
        cin>>item;
        switch(item)
        {
            case 1: BinTree_CREAT(BinTreeOPP); break; //构造二叉数
            case 2: BinTree_PRE(BinTreeOPP);  break; //先序遍历二叉树
            case 3: BinTree_INO(BinTreeOPP); break;  //中序遍历二叉树
            case 4: BinTree_POS(BinTreeOPP); break;   //后序遍历二叉树
            case 5: BinTree_Destroy(BinTreeOPP);break;   //求树的深度
            default: return ;
        }
    }while(true);
}

void BINTREE()
{
    int item;
    cout<<"清选择数据类型: 1) 整型 2) 浮点型 3) 字符型 X) 退出: ";

    cin>>item;
    switch(item)
    {
        case 1: BinTreeINI(INT); break; //根据不同的用户需要选择数据类型
        case 2: BinTreeINI(FLOAT); break;
        case 3: BinTreeINI(CHAR); break;
        default: return ; break;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值