非递归中序遍历---算法导论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
- 非递归中序遍历---算法导论12.1-3
- 非递归中序遍历---算法导论12.1-3
- 算法导论12-1-3非递归中序遍历
- 算法导论 12-1-3 中序遍历的非递归算法
- 算法导论第十章习题10.4-3非递归方式实现二叉树的中序遍历
- 算法导论12.2-7.P165另一种非递归的中序遍历二叉树的办法
- 算法导论 习题12.1-3 二叉树的无栈非递归中序遍历算法
- 《算法导论》二叉树遍历的递归&非递归实现
- 算法导论第二版习题10.4-5.非递归有父节点中序遍历二叉树
- 二叉树前序遍历和中序遍历 非递归算法
- 算法导论—二叉树非递归遍历
- 前序遍历 中序遍历 后续遍历的递归与非递归算法
- 算法导论 10.4-3 O(n) 二叉树 非递归遍历
- 非递归中序遍历
- 非递归中序遍历
- 非递归中序遍历
- 中序遍历二叉树的非递归算法
- 二叉树先序、中序、后序三种遍历的非递归算法
- 数据导入与导出-----exp imp
- at91sam9260-shutdown controller 章节翻译,可用于待机功能
- Symantec云备份
- python爬虫scrapy
- 6.25 被虐后 聊思 const
- 非递归中序遍历---算法导论12.1-3
- openstack学习笔记----neutron扫盲贴
- 百分点推荐引擎——从需求到架构
- 圣杯布局
- IBM数据保护和存储管理
- Oracle 11gR2 RAC ohasd failed to start 解决方法
- oracle网络配置listener.ora、sqlnet.ora、tnsnames.ora
- Java Reflection(六):Getters and Setters
- opencv+VS2010环境配置