C++创建二叉树(一)

本文实现的二叉树功能包含:

//  拷贝构造函数
//  重载 operator=
//  创建二叉树(5种创建方法)
//  递归遍历  (先序,中序,后序) 
//  
//接下来的文章将实现:非递归遍历二叉树,创建二叉树(先中序,中后序)
//              二叉树的结点个数,深度,查找结点,查找双亲等

附一张二叉树结构图
这里写图片描述

二叉树模板类的定义

//只给出了public方法
template<class Type>
class BinaryTree
{
protected:
    typedef struct BtNode//二叉树结点
    {
        Type data;
        BtNode* leftchild;
        BtNode* rightchild;
    };

public:
    BinaryTree();
    BinaryTree(const Type &x);    
    BinaryTree(const BinaryTree &tree);     //拷贝构造函数
    BinaryTree & operator=(BinaryTree &obj);//重载operator方法
    ~BinaryTree();
public:
    void MakeTree(const Type *&str);//内部包含5种创建方法
    void PreOrder() const;          //先序遍历(递归)
    void InOrder()  const;          //中序遍历 (递归)
    void PastOrder()const;          //后序遍历 (递归)

private:
    BtNode* root;
    Type    RefValue;//叶子结点标志
};

代码实现

#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;

template<class Type>
class BinaryTree
{
protected:
    typedef struct BtNode
    {
        Type data;
        BtNode* leftchild;
        BtNode* rightchild;
    };
    typedef BtNode* PBtree;

public:
    BinaryTree():root(NULL),RefValue(NULL){}
    BinaryTree(const Type &x):root(NULL),RefValue(x){}
    BinaryTree(const BinaryTree &tree)
    {
        root = Copy(tree.root);
    }

    BinaryTree & operator=(BinaryTree &obj)
    {
        if(this != &obj)
        {
            root = Copy(obj.root);
        }
        return *this;
    }

    ~BinaryTree(){}

public:
    void MakeTree(const Type *&str)
    {
        if(NULL == str)
        {
            root = NULL;
        }
        else
        {//5种创建二叉树的方法:中间三种方法终端输入
            root = Create(str);
            //Create1(root);
            //root = Create2();
            //Create3(&root);
            //root = Create4(&str);
        }
    }

    void PreOrder()const
    {
        cout <<endl<< "先序遍历(递归)  : ";
        PreOrder(root);
        cout << endl;
    }
    void InOrder()const
    {
        cout  << "中序遍历(递归)  : ";
        InOrder(root);
        cout << endl;
    }
    void PastOrder()const
    {
        cout  << "后序遍历(递归)  : ";
        PastOrder(root);
        cout << endl;
    }

private:
    static BtNode* Buynode()
    {
        BtNode *s = (BtNode*)malloc(sizeof(BtNode));
        assert(s != NULL);
        memset(s,0,sizeof(BtNode));

        return s;
    }

    static void Freenode(BtNode *p)
    {
        free(p);
    }

    BtNode *Create(const Type *&str)
    {
        if(*str == RefValue)return NULL;
        else
        {
            BtNode *s = Buynode();
            s->data = *str;
            s->leftchild = Create(++str);
            s->rightchild = Create(++str);

            return s;
        }
    }

    void Create1(BtNode *&p)
    {
        Type x;
        cin>>x;
        if(x == RefValue) p = NULL;
        else
        {
            p = Buynode();
            p->data = x;
            Create1(p->leftchild);
            Create1(p->rightchild);
        }
    }

    BtNode *Create2()
    {
        Type x;
        BtNode *s=NULL;
        cin>>x;

        if(x != RefValue)
        {
            s = Buynode();
            s->data = x;
            s->leftchild = Create2();
            s->rightchild= Create2();
        }
        return s;
    }

    void Create3(BtNode **p)
    {
        Type x;
        cin>>x;

        if(x == RefValue) p=NULL;
        else
        {
            *p= Buynode();
            (*p)->data = x;
            //Create3(&(*p)->leftchild); //两种写法都可以
            Create3(&(**p).leftchild);
            Create3(&(**p).rightchild);
        }
    }

    BtNode *Create4(const Type **str)
    {
        BtNode *s = NULL;
        if(**str != RefValue)
        {
            s = Buynode();
            s->data = **str;
            s->leftchild = Create4(&++*str);
            s->rightchild= Create4(&++*str);
        }
        return s;
    }

    static void PreOrder(BtNode *p)
    {
        if(NULL != p)
        {
            cout<<p->data<<"  ";
            PreOrder(p->leftchild);
            PreOrder(p->rightchild);
        }
    }

    static void InOrder(BtNode *p)
    {
        if(NULL != p)
        {
            InOrder(p->leftchild);
            cout<<p->data<<"  ";
            InOrder(p->rightchild);
        }   
    }

    static void PastOrder(BtNode *p)
    {
        if(NULL != p)
        {
            PastOrder(p->leftchild);
            PastOrder(p->rightchild);
            cout<<p->data<<"  ";
        }
    }

    static BtNode *Copy(const BtNode *p)
    {
        BtNode *newroot;

        if(NULL == p)return NULL;
        else
        {
            newroot = Buynode();
            newroot->data = p->data;
            newroot->leftchild  = Copy(p->leftchild);
            newroot->rightchild = Copy(p->rightchild);

            return newroot;
        }
    }

private:
    BtNode* root;
    Type    RefValue;
};

int main()
{
    BinaryTree<char> mytree('#');
    char *str="ABC##DE##F##G#H##";

    mytree.MakeTree(str);
    mytree.PreOrder();
    mytree.InOrder();
    mytree.PastOrder();

    BinaryTree<char> youtree(mytree);//拷贝构造
    youtree.PreOrder();
    youtree.InOrder();
    youtree.PastOrder();

    BinaryTree<char> histree;
    histree=mytree;                  //赋值语句(重载operator=方法)
    histree.PreOrder();
    histree.InOrder();
    histree.PastOrder();

    return 0;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值