码迷,mamicode.com
首页 > 其他好文 > 详细

STL实现二叉树遍历

时间:2017-10-08 14:45:16      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:friend   stl   private   class   tno   binary   add   pac   遍历   

#include<iostream>
using namespace std;
template<class Type>
class BSTree;
template<class Type>
class BinaryNode
{
    friend class BSTree<Type>;
    public:
        BinaryNode():left(NULL),right(NULL){}
        BinaryNode(const Type& value):data(value),left(NULL),right(NULL){}
    private:
        Type data;
        BinaryNode *left;
        BinaryNode *right;
};
template<class Type>
class BSTree{
    BinaryNode<Type>*root;
    public:
        BSTree():root(NULL){}
        BinaryNode<Type>* GetRoot() const{return root;}
        Type AddValue(const Type& value);
        void printData_NLR(const BinaryNode<Type>*startNode);//前序遍历
        void printData_LNR(const BinaryNode<Type>*startNode);//中序遍历
        void printData_LRN(const BinaryNode<Type>*startNode);//后序遍历
        void Cout()
        {
        }
};
template<class Type>
Type BSTree<Type>::AddValue(const Type& value)
{
    if(root==NULL)
    {
        root=new BinaryNode<Type>(value);
    }
    else
    {
        BinaryNode<Type>* node=root;
        while(1)
        {
            if(value>node->data)
            {
                if(node->right==NULL)
                {
                    node->right=new BinaryNode<Type>(value);
                    break;
                }
                else
                {
                    node=node->right;
                }
            }
                else
                {
                    if(node->left==NULL)
                    {
                        node->left=new BinaryNode<Type>(value);
                        break;
                    }else
                    {
                        node=node->left;
                    }
                }
        }
    }
}
//前序遍历
template<class Type>
void BSTree<Type>::printData_NLR(const BinaryNode<Type>* startNode)
{
    if(startNode==NULL)
    {
        return ;
    }
    else
    {
        cout<<startNode->data<<" ";
        printData_NLR(startNode->left);
        printData_NLR(startNode->right);
    }
}
//中序遍历
template<class Type>
void BSTree<Type>::printData_LNR(const BinaryNode<Type> *startNode)
{
    if(startNode==NULL)
    {
        return ;
    }
    else
    {
        printData_LNR(startNode->left);
        cout<<startNode->data<<" ";
        printData_LNR(startNode->right);
    }
}
//后序遍历
template<class Type>
void BSTree<Type>::printData_LRN(const BinaryNode<Type> *startNode)
{
    if(startNode==NULL)
    {
        return ;
    }
    else
    {
        printData_LRN(startNode->left);
        printData_LRN(startNode->right);
        cout<<startNode->data<<" ";
    }
}
int main()
{
    BSTree<int> tree;
    //填充数据
    tree.AddValue(7);
    tree.AddValue(4);
    tree.AddValue(10);
    tree.AddValue(1);
    tree.AddValue(5);
    tree.AddValue(-1);
    tree.AddValue(9);
    tree.AddValue(13);
    tree.AddValue(12);
    tree.AddValue(11);
    tree.AddValue(14);
    tree.AddValue(19);
    cout<<"前序遍历:根节点->左子树->右子树"<<endl;
    tree.printData_NLR(tree.GetRoot());
    cout<<"\n中序遍历:左子树->根节点->右子树"<<endl;
    tree.printData_LNR(tree.GetRoot());
    cout<<"\n后序遍历:左子树->右子树->根节点"<<endl;
    tree.printData_LRN(tree.GetRoot());
    return 0;
}

 

STL实现二叉树遍历

标签:friend   stl   private   class   tno   binary   add   pac   遍历   

原文地址:http://www.cnblogs.com/enyala/p/7637292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!