非递归中序遍历---算法导论12.1-3

问题描述:

给出一个非递归的中序树遍历算法。(提示:有两种方法,在较容易的方法中,可以采用栈作为辅助数据结构;在较为复杂的方法中,不采用栈结构,但假设可以测试两个指针是否相等。)

构造的树如下:
这里写图片描述

中序遍历结果为:1 2 3 4 5 6 7 8 9

算法实现:

//非递归用栈实现中序遍历
void BinaryTree::non_recurse_using_stack_in_order_visit(Node* root)
{
    Node* stack[9];
    int top =-1;
    cout<<"top值"<<"\t"<<"元素"<<endl;//for test
    while(root != NULL || top >-1)
    {
        while(root != NULL)
        {
            stack[++top] = root;
            root = root->left;
        }
        if(top >-1)
        {
            root = stack[top--];
            cout<<top<<"\t";//for test
            printf("%d\n", root->data);
            root = root->right;
        }
    }
}

该算法实现步骤如下:
这里写图片描述
这里写图片描述
这里写图片描述

以下全部实现代码来自http://www.cnblogs.com/shuaiwhu/archive/2011/04/20/2065055.html

#include <iostream>
#include <stdio.h>
using namespace std;
class Node
{
public:
    int data;
    Node* left;
    Node* right;
    Node* parent;
    bool visited;
    //Node(){}
    //Node(int d);
    Node(int d, Node* l, Node* r);//构造函数的声明
};
class BinaryTree
{
public:
    Node* root;
    BinaryTree(Node* r);
    //递归实现中序遍历
    void recurse_in_order_visit(Node* root);
    //非递归用栈实现中序遍历
    void non_recurse_using_stack_in_order_visit(Node* root);
    //非递归且不用栈实现中序遍历
    void non_recurse_non_stack_in_order_visit(Node* root);
    //enum TRAVESAL_STATE
    //{
    //    LEFT_NOT_TRAVERS,//左子树未遍历
    //    LEFT_TRAVERSED,//左子树已遍历(包括左子树为空)
    //    RIGHT_TRAVERSED//右子树已遍历(右子树为空)
    //};
};
int main()
{
    Node* node1 =new Node(1, NULL, NULL);//格式有Node()构造函数定义
    Node* node2 =new Node(2, node1, NULL);
    Node* node3 =new Node(4, NULL, NULL);
    Node* node4 =new Node(3, node2, node3);
    Node* node5 =new Node(7, NULL, NULL);
    Node* node6 =new Node(6, NULL, node5);
    Node* node7 =new Node(9, NULL, NULL);
    Node* node8 =new Node(8, node6, node7);
    Node* root =new Node(5, node4, node8);
    BinaryTree* binary_tree =new BinaryTree(root);
    cout<<"递归中序遍历的结果:"<<endl;
    binary_tree->recurse_in_order_visit(binary_tree->root);
    cout<<endl;
    cout<<"非递归用栈中序遍历的结果:"<<endl;
    binary_tree->non_recurse_using_stack_in_order_visit(binary_tree->root);
    cout<<endl;
    //若使用非递归且不用栈来进行中序遍历,则需要parent指针作为辅助
    node1->parent = node2;
    node2->parent = node4;
    node3->parent = node4;
    node5->parent = node6;
    node6->parent = node8;
    node7->parent = node8;
    node4->parent = root;
    node8->parent = root;
    cout<<"非递归且不用栈中序遍历的结果:"<<endl;
    binary_tree->non_recurse_non_stack_in_order_visit(binary_tree->root);
    cout<<endl;
    return 0;
}
/*
Node::Node(int d)
{
    data = d;
    parent = left = right = NULL;
    visited =false;
}
*/
Node::Node(int d, Node* l, Node* r)
{//构造函数的实现
    data = d;
    left = l;
    right = r;
    parent = NULL;
    visited =false;
}

BinaryTree::BinaryTree(Node* r)
{
    root = r;
}

//递归实现中序遍历
void BinaryTree::recurse_in_order_visit(Node* root)
{
    if(root != NULL)
    {
        recurse_in_order_visit(root->left);
        printf("%d\t", root->data);
        recurse_in_order_visit(root->right);
    }
}

//非递归用栈实现中序遍历
void BinaryTree::non_recurse_using_stack_in_order_visit(Node* root)
{
    Node* stack[9];
    int top =-1;
    cout<<"top值"<<"\t"<<"元素"<<endl;//for test
    while(root != NULL || top >-1)
    {
        while(root != NULL)
        {
            stack[++top] = root;
            root = root->left;
        }
        if(top >-1)
        {
            root = stack[top--];
            cout<<top<<"\t";//for test
            printf("%d\n", root->data);
            root = root->right;
        }
    }
}
//非递归且不用栈实现中序遍历
void BinaryTree::non_recurse_non_stack_in_order_visit(Node* root)
{
    while ( root != NULL )
    {
        while ( root->left != NULL &&!root->left->visited )
        {//一路向左
            root = root->left;
        }
        if ( !root->visited )
        {//中
            cout<<root->data<<"\t";
            root->visited=true;
        }
        if ( root->right != NULL &&!root->right->visited )
        {//右子树
            root = root->right;
        }
        else
        {//回溯至parent节点
            root = root->parent;
        }
    }
}

执行结果:

递归中序遍历的结果:
1       2       3       4       5       6       7       8       9
非递归用栈中序遍历的结果:
top值   元素
2       1
1       2
0       3
0       4
-1      5
0       6
0       7
-1      8
-1      9

非递归且不用栈中序遍历的结果:
1       2       3       4       5       6       7       8       9

Process returned 0 (0x0)   execution time : 0.585 s
Press any key to continue.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值