二叉树的前中后序遍历,及广度遍历实现

二叉树的前中后序遍历,及广度遍历实现

说明:代码使用codeblocks编译,C++实现,个人编写,如有错误,还望指正。

fengjingtu

#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>


using namespace std;

struct TreeNode{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int v):val(v),left(NULL),right(NULL){
    }
};
/*
构建这样一棵二叉树:

        1
       / \
      2   3
     /\   /\
    4  5  6 7
    /   \
    8    9

*/
void BuildTree(TreeNode* &root)
{
    root=new TreeNode(1);
    root->left=new TreeNode(2);
    root->right=new TreeNode(3);
    root->left->left=new TreeNode(4);
    root->left->right=new TreeNode(5);
    root->right->left=new TreeNode(6);
    root->right->right=new TreeNode(7);
    root->left->left->left=new TreeNode(8);
    root->left->right->right=new TreeNode(9);

    return ;
}
void DestroyTree(TreeNode* &root)
{
    if(root==NULL)
        return ;
    if(root->left)
        DestroyTree(root->left);
    if(root->right)
        DestroyTree(root->right);
    delete root;
    return ;

}

void preRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    cout<<root->val<<" ";
    if(root->left)
        preRecursionPrint(root->left);
    if(root->right)
        preRecursionPrint(root->right);
    return ;
}
void midRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    if(root->left)
        midRecursionPrint(root->left);
    cout<<root->val<<" ";
    if(root->right)
        midRecursionPrint(root->right);
    return ;
}
void afterRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    if(root->left)
        afterRecursionPrint(root->left);
    if(root->right)
        afterRecursionPrint(root->right);
    cout<<root->val<<" ";

    return ;
}


void preNonRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    TreeNode * cur=root;
    stack<TreeNode*>s;
    while(cur)
    {
        cout<<cur->val<<" ";
        if(cur->right)
        {
            s.push(cur->right);
        }
        if(cur->left)
            cur=cur->left;
        else if(!s.empty())
        {
            cur=s.top();
            s.pop();
        }
        else
            return ;
    }

    return ;
}

void midNonRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    TreeNode * cur=root;
    stack<TreeNode*>s;
    while(cur || (!s.empty()))
    {
        while(cur)
        {
            s.push(cur);
            cur=cur->left;
        }
        if(!s.empty())
        {
            cur=s.top();
            s.pop();
            cout<<cur->val<<" ";
            if(cur->right)
                cur=cur->right;
            else
                cur=NULL;
        }
        else
            return ;

    }
    return ;
}
void afterNonRecursionPrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    TreeNode * cur=root;
    stack<TreeNode*>s1;
    stack<TreeNode*>s2;

    while(cur || (!s1.empty()))
    {
        while(cur)
        {
            s1.push(cur);
            s2.push(cur);
            cur=cur->right;
        }
        cur = s1.top();
        s1.pop();
        cur =cur->left;
    }
    while(!s2.empty())
    {
        cur=s2.top();
        s2.pop();
        cout<<cur->val<<" ";
    }
    return ;
}

void widePrint(TreeNode*root)
{
    if(root==NULL)
        return ;
    queue<TreeNode*>q;
    q.push(root);
    TreeNode*cur;
    while(!q.empty())
    {
        cur=q.front();
        if(cur)
            cout<<cur->val<<" ";
        q.pop();
        if(cur->left)
            q.push(cur->left);
        if(cur->right)
            q.push(cur->right);
    }
    return ;

}
int main()
{
    TreeNode *root;
    //构建二叉树
    BuildTree(root);

    preRecursionPrint(root);
    cout<<endl;
    preNonRecursionPrint(root);
    cout<<endl;
    midRecursionPrint(root);
    cout<<endl;
    midNonRecursionPrint(root);
    cout<<endl;
    afterRecursionPrint(root);
    cout<<endl;
    afterNonRecursionPrint(root);
    cout<<endl;
    widePrint(root);
    cout<<endl;

    //销毁二叉树
    DestroyTree(root);
    return 0;
}

运行结果:

1 2 4 8 5 9 3 6 7
1 2 4 8 5 9 3 6 7
8 4 2 5 9 1 6 3 7
8 4 2 5 9 1 6 3 7
8 4 9 5 2 6 7 3 1
8 4 9 5 2 6 7 3 1
1 2 3 4 5 6 7 8 9

猜你喜欢

转载自blog.csdn.net/zujipi8736/article/details/86606515