简单二叉树

// Algorithm.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <functional>

#define D_LEFT 0
#define D_RIGHT 1
template <class T>
struct BNode
{
    BNode() :pLNode(NULL), pRNode(NULL), pValue(NULL) {}
    BNode* pLNode; // 左子树
    BNode* pRNode; // 右子树
    T* pValue; // 值
};

template<class T>
class BTree
{
public:
    BTree():root(NULL){}

    void Insert(T* pValue)
    {
        BNode<T>* pNode = new BNode<T>();
        pNode->pValue = pValue;
        // 如果树为空
        if(NULL == root)
        {
            root = pNode;
            return;
        }
        
        int iInsertFlag = 0; // 插入方向,左还是右
        BNode<T>* pParent = NULL;
        BNode<T>* pTem = root;
        while(pTem)
        {
            pParent = pTem;
            if(*pNode->pValue < *pTem->pValue)
            {
                iInsertFlag = D_LEFT;
                pTem = pTem->pLNode;
            }
            else
            {
                iInsertFlag = D_RIGHT;
                pTem = pTem->pRNode;
            }
        }

        if(pParent)
        {
            switch(iInsertFlag)
            {
            case D_LEFT:
                pParent->pLNode = pNode;
                break;
            case D_RIGHT:
                pParent->pRNode = pNode;
                break;
            default:
                break;
            }
        }
    }

    // 先访问 根->左->右
    // 前序遍历
    void DLR(BNode<T>* root,std::function<void (T*)> _callback)
    {
        // 空树
        if(NULL == root)
        {
            return;
        }

        _callback(root->pValue);

        if(root->pLNode != NULL)
            DLR(root->pLNode,_callback);
        if(root->pRNode != NULL)
            DLR(root->pRNode,_callback);
    }

    // 先访问 左->根->右
    // 中序遍历
    void LDR(BNode<T>* root, std::function<void(T*)> _callback)
    {
        // 空树
        if(NULL == root)
        {
            return;
        }

        if(root->pLNode != NULL)
            LDR(root->pLNode, _callback);

        _callback(root->pValue);

        if(root->pRNode != NULL)
            LDR(root->pRNode, _callback);
    }

    // 先访问 左->右->根
    // 后序遍历
    void LRD(BNode<T>* root, std::function<void(T*)> _callback)
    {
        // 空树
        if(NULL == root)
        {
            return;
        }

        if(root->pLNode != NULL)
            LRD(root->pLNode, _callback);

        if(root->pRNode != NULL)
            LRD(root->pRNode, _callback);

        _callback(root->pValue);
    }
public:
    BNode<T>* root;
};

int main()
{

    BTree<int> bTree;

    int a[5] = { 2,5,1,1,3 };
    bTree.Insert(&a[0]);
    bTree.Insert(&a[1]);
    bTree.Insert(&a[2]);
    bTree.Insert(&a[3]);
    bTree.Insert(&a[4]);

    // 打印前序遍历
    printf("前序遍历:\n");
    bTree.DLR(bTree.root, [](int* a)
    {
        printf("%d ", *a);
    });

    printf("\n\n中序遍历:\n");
    // 打印中序遍历
    bTree.LDR(bTree.root, [](int* a)
    {
        printf("%d ", *a);
    });

    printf("\n\n后续遍历:\n");
    // 打印中序遍历
    bTree.LRD(bTree.root, [](int* a)
    {
        printf("%d ", *a);
    });

    getchar();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值