网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

二叉树的递归和非递归遍历

// 本次练习的是  二叉树的  递归和非递归  遍历   以及二叉树的  节点数  高度  叶子节点数   和查找功能  
//如果要是变量在函数栈回归时不回归原值,则可以用引用
//


#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
using namespace std;


template
struct BinaryTreeNode
{
    BinaryTreeNode(const T& date)
    :_date(date)
    , _left(NULL)
    , _right(NULL)
    {}

    T _date;
    BinaryTreeNode* _left;
    BinaryTreeNode* _right;
};

template
class BinaryTree
{
public:
    BinaryTree(const T* date,size_t size)
    {
        size_t index = 0;      
        _root = _CreateBinaryTree(date, index, size);
    }

    ~BinaryTree()
    {
        Destory(_root);
    }

    void PreOrder()
    {
        _PreOrder(_root);
        cout << endl;
    }

    void MiddleOrder()
    {
        _MiddleOrder(_root);
        cout << endl;
    }

    void LastOrder()
    {
        _LastOrder(_root);
        cout << endl;
    }

    void Size()
    {
        cout<<_Size(_root)<    }

    void Hight()
    {
        cout << _Hight(_root) << endl;
    }

    void LeafOfNumber()
    {
        cout<< _LeafOfNumber(_root)<    }

    BinaryTreeNode* Find(const T& data)
    {
        return _Find(_root,data);
    }

    void PrevOrder_Non_R()
    {
        _PrevOrder_Non_R(_root);
        cout << endl;
    }

    void MiddleOrder_Non_R()
    {
        _MiddleOrder_Non_R(_root);
        cout << endl;
    }

    void LastOrder_Non_R()
    {
        _LastOrder_Non_R(_root);
        cout << endl;
    }

    void Destory(BinaryTreeNode* root)
    {
        if (root != NULL)
        {
            Destory(root->_left);
            Destory(root->_right);
            delete root;
        }
    }

protected:

    BinaryTreeNode* _CreateBinaryTree(const T* date, size_t& index, size_t size)//注意   index 要用  引用
    {
        if (index >= size || date[index] == '#')
        {    
            return NULL;
        }
            
        BinaryTreeNode* root = new BinaryTreeNode(date[index]);  
        root->_left = _CreateBinaryTree(date, ++index, size);               //要用前置加加而不能用后置加加  因为后置加加会产生一个临时变量
        root->_right = _CreateBinaryTree(date, ++index, size);  
        
        return root;  
    }

    void _PreOrder(BinaryTreeNode* root)
    {
        if (root == NULL)
        {
            return;
        }

        cout << root->_date << "  ";
        
        _PreOrder(root->_left);
        _PreOrder(root->_right);
    }

    void _MiddleOrder(BinaryTreeNode* root)
    {
        if (root == NULL)
        {
            return;
        }

        _MiddleOrder(root->_left);
        cout << root->_date<<"  ";
        _MiddleOrder(root->_right);
    }

    void _LastOrder(BinaryTreeNode* root)
    {
        if (root == NULL)
        {
            return;
        }

        _LastOrder(root->_left);
        _LastOrder(root->_right);
        cout << root->_date << "  ";
    }

    int _Size(BinaryTreeNode* root)
    {
        if (root == NULL)
        {
            return 0;
        }

        return _Size(root->_left) + _Size(root->_right) + 1;
    }

    int _Hight(BinaryTreeNode* root)      //求二叉树的高度
    {
        if (root == NULL)
        {
            return 0;
        }

        int left = _Hight(root->_left) ;
        int right = _Hight(root->_right);

        return (left > right ? (left + 1) :( right + 1));
    }

    int _LeafOfNumber(BinaryTreeNode* root)
    {
        if (root == NULL)
        {
            return 0;
        }

        if (root->_left == NULL && root->_right == NULL)
        {
            return 1;
        }
        return _LeafOfNumber(root->_left) + _LeafOfNumber(root->_right);
        
    }

    BinaryTreeNode* _Find(BinaryTreeNode* root,const T& data)
    {
        if (root == NULL)
        {
            return NULL;
        }

        if (root->_date == data)
        {
            return root;
        }

        BinaryTreeNode* left =  _Find(root->_left,data);
        if (left != NULL)
        {
            return left;
        }
        BinaryTreeNode* right =  _Find(root->_right,data);
        if (right != NULL)
        {
            return right;
        }
    }
    /*
    void _PrevOrder_Non_R(BinaryTreeNode* root)
    {
        stack*> s;
        BinaryTreeNode* cur = root;

        while (!s.empty() || cur != NULL)
        {
            s.push(cur);
            BinaryTreeNode* top = s.top();
            cout << top->_date << "  ";

            cur = top->_left;
            
            if (cur == NULL)
            {
                s.pop();
                if (s.top()->_right != NULL)
                {
                    cur = s.top()->_right;
                }
                else
                {
                    s.pop();
                }        
            }
        }
    }
    */

    void _PrevOrder_Non_R(BinaryTreeNode* root)
    {
        stack* > s;       //栈中每个节点都会存放一次     下面只需把一个节点的 根  左 右 访问一次就行
        BinaryTreeNode* cur = root;

        if (cur != NULL)
        {
            s.push(cur);
        }

        while (!s.empty())
        {
            BinaryTreeNode* top = s.top();
            cout << top->_date << "  ";
            s.pop();

            if (top->_right != NULL)
            {
                s.push(top->_right);
            }

            if (top->_left != NULL)
            {
                s.push(top->_left);
            }
        }
    }
    void _MiddleOrder_Non_R(BinaryTreeNode* root)
    {
        stack* > s;
        BinaryTreeNode* cur = root;

        while (!s.empty() || cur != NULL)
        {
            while (cur != NULL)        
            {
                s.push(cur);
                cur = cur->_left;
            }

            if (!s.empty())
            {
                BinaryTreeNode* top = s.top();
                s.pop();
                cout << top->_date << "  ";
                cur = top->_right;
            }
        }
    }

    void _LastOrder_Non_R(BinaryTreeNode* root)
    {
        stack*> s;
        BinaryTreeNode* cur = root;
        BinaryTreeNode* prev = NULL;

        while (cur != NULL || !s.empty())
        {
            while (cur != NULL)
            {
                s.push(cur);
                cur = cur->_left;    //此时左已遍历完  
            }
            
            BinaryTreeNode* top = s.top();

            if (top->_right == NULL || top->_right == prev)  //这两种情况都表示  1. 右  已遍历完
            {
                cout << top->_date << "  ";
                prev = top;
                s.pop();
            }
            else       // 2.右还没有遍历完
            {
                cur = top->_right;
            }
        }                    
        cout << endl;
    }


protected:
    BinaryTreeNode* _root;
};


void Test1()
{
    int arr[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
    
    BinaryTree t2(arr, 10);
    cout << "前序:" << endl;
    t2.PreOrder();
    t2.PrevOrder_Non_R();

    cout << "中序:" << endl;
    t2.MiddleOrder();
    t2.MiddleOrder_Non_R();

    cout << "后序:" << endl;
    t2.LastOrder();
    t2.LastOrder_Non_R();

    cout << "节点数:" << endl;
    t2.Size();
    
    cout << "高度:" << endl;
    t2.Hight();

    cout << "叶子节点数:" << endl;
    t2.LeafOfNumber();

    BinaryTreeNode* find = t2.Find(3);
    cout << find->_date << endl;
}


int main()
{
    Test1();
    return 0;
}二叉树的递归和非递归遍历

创新互联专业网站制作、成都做网站,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文营销等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。


网站标题:二叉树的递归和非递归遍历
网页URL:http://bjjierui.cn/article/pgsspj.html

其他资讯