二叉树的遍历

实现二叉树的递归和非递归遍历以及运用游标遍历


#include <iostream>
#include <stack>
using namespace std;

class TreeNode;
struct Element 
{
    TreeNode * node;
    bool isVisited;
};

class TreeNode
{
    friend class Tree;
    friend class Tree_Iterator;
public:
    TreeNode(int d=0,TreeNode * l=NULL,TreeNode * r=NULL):data(d),leftchild(NULL),rightchild(NULL)
    {
        
    }
private:
    int data ;
    TreeNode * leftchild;
    TreeNode * rightchild;
};


class Tree
{
     friend class Tree_Iterator;
public:
    Tree(TreeNode * r=NULL):root(r)
    {

    }
    void insert_data(int d)
    {
        if (!root)
        {
            root = new TreeNode(d);
            return;
        }
        TreeNode * temp = root;
        while (1)
        {
            if (temp->data == d)
            {
                return ;
            }
            else if(d>temp->data)
            {
                if (temp->rightchild)
                {
                    temp = temp->rightchild;
                }
                else
                {
                    temp->rightchild = new TreeNode(d);
                    return;
                }
            }
            else
            {
                if (temp->leftchild)
                {
                    temp = temp->leftchild;
                }
                else
                {
                    temp->leftchild = new TreeNode(d);
                    return;
                }
            }
        }      
    }
    void inorder_Recursion()
    {
        inorder_Recursion(root);
    }
    void inorder_Recursion(TreeNode * root)
    {
        if (root)
        {
            cout<<root->data<<" ";
            inorder_Recursion(root->leftchild);
            inorder_Recursion(root->rightchild);
        }
    }
    void preorder_Recursion()
    {
        preorder_Recursion(root);
    }
    void preorder_Recursion(TreeNode * root)
    {
        if (root)
        {
            preorder_Recursion(root->leftchild);
            cout<<root->data<<" ";
            preorder_Recursion(root->rightchild);
        }
    }
    void postoder_Recursion()
    {
        postoder_Recursion(root);
    }
    void postoder_Recursion(TreeNode * root)
    {
        if (root)
        {
            postoder_Recursion(root->leftchild);
            postoder_Recursion(root->rightchild);
            cout<<root->data<<" ";
        }
    }
    void preoder()
    {
        stack<TreeNode*> s;
        TreeNode * current_node = root;
        while (1)
        {
            while(current_node)
            {
                cout<<current_node->data<<" ";
                s.push(current_node);
                current_node = current_node->leftchild;
            }
            if (!s.empty())
            {
                current_node = s.top();
                s.pop();
                current_node = current_node->rightchild;
            }
            else
                return;
        }
    }
    void inoder()
    {
        stack<TreeNode*> s;
        TreeNode * current_node = root;
        while (1)
        {
            while(current_node)
            {
                s.push(current_node);
                current_node = current_node->leftchild;
            }
            if (!s.empty())
            {
                current_node = s.top();
                s.pop();
                cout<<current_node->data<<" ";
                current_node = current_node->rightchild;
            }
            else
                return;
        }
    }
    void postoder()
    {
        stack<Element> s;
        TreeNode * current_node = root;
        Element e ;
        while(1)
        {
            while(current_node)
            {
                e.node = current_node;
                e.isVisited = false;
                s.push(e);
                current_node = current_node->leftchild;
            }
            if (!s.empty())
            {
                e = s.top();
                s.pop();
                current_node = e.node;
                if (e.isVisited == true)
                {
                    cout<<current_node->data<<" ";
                    current_node = NULL;
                }
                else
                {
                    e.isVisited = true;
                    s.push(e);
                    current_node = current_node->rightchild;
                }
            }
            else
                return;
        }
    }
private:
    TreeNode * root;
};


class Tree_Iterator
{
public:
    Tree_Iterator(Tree * t):root(t->root),current_node(t->root)
    {
        
    }
    int in_next()
    {
        int result=-1;
        if (current_node)
        {
            result = current_node->data;
            pre_s.push(current_node);
            current_node = current_node->leftchild;
        }
        while (!pre_s.empty()&¤t_node==NULL)//此处是不同点,如果current_node为空,一直找下去,找到下一次要用的节点
        {
            current_node = pre_s.top();
            pre_s.pop();
            current_node = current_node->rightchild;//保证已经便利过得不会再被遍历       
        }
        return result;
    }
    int post_next()
    {
        int result = -1;
        Element e;
        while (1)
        {
            while (current_node)
            {
                e.isVisited=false;
                e.node=current_node;
                post_s.push(e);
                current_node = current_node->leftchild;
            }
            if (!post_s.empty())
            {
                e = post_s.top();
                post_s.pop();
                current_node = e.node;
                if (e.isVisited == false)
                {
                    e.isVisited = true;
                    post_s.push(e);
                    current_node = current_node->rightchild;
                }
                else
                {
                    result = current_node->data;
                    current_node = NULL;
                    return result;
                }
            }
            else
                return result;
        }

    }
    int pre_next()
    {
        int result=-1;
        while (current_node)
        {
            pre_s.push(current_node);
            current_node = current_node->leftchild;
        }
        if(!pre_s.empty())
        {
            current_node = pre_s.top();
            pre_s.pop();
            result = current_node->data;
            current_node = current_node->rightchild;       
        }
        return result;
    }
private:
    stack<TreeNode *> pre_s;
    stack<TreeNode *> in_s;
    stack<Element > post_s;
    TreeNode * root;
    TreeNode * current_node;
};

#include "binary_tree.h"

int main()
{
    Tree t;
    t.insert_data(5);
    t.insert_data(4);
    t.insert_data(6);
    t.insert_data(3);
    t.insert_data(7);
    
    t.preorder_Recursion();
    cout<<endl;
    t.inorder_Recursion();
    cout<<endl;
    t.postoder_Recursion();
    cout<<endl;
    t.preoder();
    cout<<endl;
    t.inoder();
    cout<<endl;
    t.postoder();
    cout<<endl;

    Tree_Iterator ite(&t);
    int i=0;
    while (i!=-1)
    {
        //cout<<(i=ite.in_next())<<" ";
        //cout<<(i=ite.pre_next())<<" ";
        cout<<(i=ite.post_next())<<" ";
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值