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

来源:互联网 发布:日本佛教 知乎 编辑:程序博客网 时间:2024/05/16 01:25

问题描述:

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

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

中序遍历结果为: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       11       20       30       4-1      50       60       7-1      8-1      9非递归且不用栈中序遍历的结果:1       2       3       4       5       6       7       8       9Process returned 0 (0x0)   execution time : 0.585 sPress any key to continue.
0 0
原创粉丝点击