C++创建二叉树与遍历(递归与迭代)

最后又看了遍严蔚敏的数据结构,用C++又推 演了一下二叉树

//二叉树
#include <iostream>
#include <cassert>
#include <stack>
using namespace std;
template<typename TElemType>
class Node{
public:
    Node()
    {
        pLeft = NULL;
        pRight = NULL;
        data = 0;
    }
    ~Node(){}
    void SetPLeft(Node* left){ this->pLeft = left; }
    /*const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数;
        const对象的成员是不能修改的,而通过指针维护的对象确实可以修改的;
        const成员函数不可以修改对象的数据,不管对象是否具有const性质。编译时以是否修改成员数据为依据进行检查。*/
    Node* GetPLeft() const { return pLeft; }
    void SetPRight(Node* right){ this->pRight = right; }
    Node* GetPRight() const { return pRight; }
    void SetData(TElemType data){ this->data = data; }
    TElemType GetData() const { return data; }
private:
    Node* pLeft;
    Node* pRight;
    TElemType data;
};
template<typename TElemType>
class BiTree{
public:
    BiTree()
    {
        pRoot = NULL;
    }
    ~BiTree(){}
    //初始化二叉树
    void Init()
    {
        this->pRoot = new Node<TElemType>;
    }
    //创建二叉树
    void Create()
    {
        cout << "input:";
        TElemType data;
        cin >> data;
        assert(this->pRoot);
        pRoot->SetData(data);
        Node<TElemType>* pCurNode;//当前结点
        Node<TElemType>* pTmpNode;
        cout << "input(q,exit):";
        while (cin >> data&&cin.get()!='q')
        {
            pCurNode = pRoot;
            Node<TElemType>* pNode = new Node<TElemType>();
            pNode->SetData(data);
            do
            {
                pTmpNode = pCurNode;
                if (pNode->GetData() < pCurNode->GetData()) pCurNode = pCurNode->GetPLeft();
                else pCurNode = pCurNode->GetPRight();
            } while (pCurNode != NULL);
            if (pNode->GetData() < pTmpNode->GetData()) pTmpNode->SetPLeft(pNode);
            else pTmpNode->SetPRight(pNode);
            cout << "input(q,exit):";
            //delete pNode;//这里是不能删除堆上的内存的
        }
        
    }
    //先序遍历(根、左、右)
    void PreOrder(Node<TElemType>* pNode)
    {
        if (pNode != NULL)
        {
            cout << pNode->GetData() << ",";
            PreOrder(pNode->GetPLeft());
            PreOrder(pNode->GetPRight());
        }
    }
    //中序遍历(左、根、右)
    void InOrder(Node<TElemType>* pNode)
    {
        if (pNode != NULL)
        {
            InOrder(pNode->GetPLeft());
            cout << pNode->GetData() << ",";
            InOrder(pNode->GetPRight());
        }
    }
    //后序遍历(左、右、根)
    void PostOrder(Node<TElemType>* pNode)
    {
        if (pNode != NULL)
        {
            PostOrder(pNode->GetPLeft());
            PostOrder(pNode->GetPRight());
            cout << pNode->GetData() << ",";
        }
    }
    //输出
    void Print()
    {
        cout << "先序遍历:";
        PreOrder(pRoot);
        cout << endl;
        cout << "中序遍历:";
        InOrder(pRoot);
        cout << endl;
        cout << "后序遍历:";
        PostOrder(pRoot);
        cout << endl;
    }
    //先序遍历,迭代
    template<typename type>
    void PreOrderIterator()
    {
        stack< Node<type>* > s;
        Node<type>* pNode = pRoot;
        //assert(pNode);
        while (pNode != NULL || !s.empty())
        {
            while (pNode != NULL)
            {
                cout << pNode->GetData() << ",";
                s.push(pNode);
                pNode = pNode->GetPLeft();
            }
            if (!s.empty())
            {
                assert(s.top());
                pNode = s.top();
                s.pop();
                pNode = pNode->GetPRight();
            }
        }
    }
    //中序遍历,迭代
    void InOrderIterator()
    {
        stack< Node<TElemType>* > s;
        Node<TElemType>* pNode = pRoot;
        while (pNode != NULL || !s.empty())
        {
            while (pNode != NULL)
            {
                s.push(pNode);
                pNode = pNode->GetPLeft();
            }
            if (!s.empty())
            {
                assert(s.top());
                pNode = s.top();
                cout << pNode->GetData() << ",";
                s.pop();
                pNode = pNode->GetPRight();
            }
        }
    }
    //后序遍历,迭代
    void PostOrderIterator()
    {
        stack< Node<TElemType>* > s;
        Node<TElemType>* pNode = pRoot;
        Node<TElemType>* pPre = NULL;
        Node<TElemType>* pTop = NULL;
        while (pNode != NULL || !s.empty())
        {
            while (pNode != NULL)
            {
                s.push(pNode);
                pNode = pNode->GetPLeft();
            }
            if (!s.empty())
            {
                pTop = s.top();
                if (pTop->GetPRight() != NULL&&pTop->GetPRight() != pPre)
                {
                    pNode = pTop->GetPRight();
                }
                else
                {
                    cout << pTop->GetData() << " ";
                    pPre = pTop;
                    s.pop();
                }
            }
        }
    }
public:
    Node<TElemType>* pRoot;
};

int main()
{
    BiTree<int>* tree= new BiTree<int>;
    tree->Init();
    tree->Create();
    tree->Print();
    cout << "迭代,先序:";
    tree->PreOrderIterator<int>();
    cout << endl;
    cout << "迭代,中序:";
    tree->InOrderIterator();
    cout << endl;
    cout << "迭代,后序:";
    tree->PostOrderIterator();
    cout << endl;
    delete tree;
    system("pause");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不识君的荒漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值