数据结构之二叉树(前序 中序 后续线索话非递归方式)

节点:
enum LinkType
{
                 THREAD,
                 LINK
};

template<class T>
struct ThredBinaryNode
{
                 ThredBinaryNode *_left;
                 ThredBinaryNode *_right;
                 LinkType _left_tag;
                 LinkType _right_tag;

                 T _data;
                ThredBinaryNode( T data ) :_data(data), _left( NULL), _right(NULL ), _left_tag(LINK), _right_tag(LINK)
                {};
};
线索化二叉树为:
template<class T>
class BinaryTreeThred
{
                 typedef ThredBinaryNode <T> Node;
public:
                BinaryTreeThred( const T * a, size_t size, const T & valiue)
                {
                                 size_t index = 0;
                                _root = _CreatTree( a, size , index, valiue);
                }
private:

              Node *_root;

};

构造函数:
                 Node*_CreatTree(const T* a, size_t size, size_t &index, const T &valiue)
                {
                                 Node *root = NULL ;
                                 if (index < size&& a[index ] != valiue)
                                {
                                                root = new Node (a[index]);
                                                root->_left = _CreatTree(a, size , ++index, valiue);
                                                root->_right = _CreatTree(a, size , ++index, valiue);
                                }
                                 return root;
                }

中序线索化递归:

                 void _InThred(Node *cur, Node* & prev)//递归线索化
                {
                                 if (cur != NULL)
                                {

                                                _InThred( cur->_left, prev );
                                                 if (cur ->_left == NULL)
                                                {
                                                                 cur->_left_tag = THREAD ;
                                                                 cur->_left = prev ;
                                                }
                                                 if (prev != NULL&& prev->_right==NULL )
                                                {
                                                                 prev->_right_tag = THREAD ;
                                                                 prev->_right = cur ;
                                                }
                                                 prev = cur ;
                                                _InThred( cur->_right, prev );
                                }

                };

中序线索非递归:
                 void InThred_Nor()//非递归
                {
                                 stack<Node *> s;
                                 Node *prev = NULL ;
                                 Node *cur = _root;
                                 while (cur||!s.empty())
                                {
                                                 while (cur)
                                                {
                                                                s.push(cur);
                                                                cur = cur->_left;
                                                };
                                                 Node *tmp = s.top();
                                                s.pop();
                                                 if (tmp->_left == NULL )
                                                {
                                                                tmp->_left = prev;
                                                                tmp->_left_tag = THREAD;
                                                }
                                                prev = tmp;
                                                 if (prev->_right == NULL &&!s.empty())
                                                {
                                                                tmp->_right = s.top();
                                                                tmp->_right_tag = THREAD;
                                                }
                                                 else
                                                {
                                                                cur = tmp->_right;
                                                }
                                }
                }

前序线化非递归:
void BinaryTreeThred <T>::PreThread() //前序线索化非递归
{
                 Node *pre = NULL ;
                 Node*cur = _root;
                 stack<Node *> s;
                s.push(cur);
                 while (cur||!s.empty())
                {
                                 Node *tmp = NULL ;
                                 if (!s.empty())
                                {
                                                tmp = s.top();
                                }
                                 else
                                {
                                                 return;
                                }

                                s.pop();
                                 if (tmp->_right)
                                {
                                                s.push(tmp->_right);
                                }

                                 if (tmp->_left)
                                {
                                                s.push(tmp->_left);
                                }
                                 else//tmp->left==null
                                {
                                                tmp->_left_tag = THREAD;
                                                tmp->_left=pre;
                                }              
                                 if (pre != NULL &&pre->_right == NULL)
                                {
                                                pre->_right_tag = THREAD;
                                                pre->_right = tmp;
                                }
                                pre = tmp;
                }
}

后序列线索话非递归:

void BinaryTreeThred <T>::PostThread() //后续
{
                 Node *cur = _root;
                 stack<Node *> s;
                 Node *prev = NULL ;
                 while (cur || !s.empty())
                {
                                 while (cur)
                                {
                                                s.push(cur);
                                                cur = cur->_left;//3
                                }
                                 Node *tmp = s.top();
                                 if (tmp->_right == NULL || tmp->_right == prev)
                                {
                                                 if (tmp->_left == NULL )
                                                {
                                                                tmp->_left_tag = THREAD;
                                                                tmp->_left = prev;
                                                }
                                                 if (prev != NULL &&prev->_right == NULL)
                                                {
                                                                prev->_right_tag = THREAD;
                                                                prev->_right = tmp;
                                                }
                                                s.pop();
                                                prev = tmp;
                                }
                                 else
                                {
                                                cur = tmp->_right;
                                }
                }

}
时间: 2024-10-25 07:16:20

数据结构之二叉树(前序 中序 后续线索话非递归方式)的相关文章

【算法与数据结构】二叉树的 中序 遍历

前一篇写了二叉树的先序遍历,本篇记录一下二叉树的中序遍历,主要是非递归形式的中序遍历. 由于距离上篇有好几天了,所以这里把二叉树的创建和存储结构也重复的写了一遍. 二叉树如下 二叉树的存储方式依然是二叉链表方式,其结构如下 typedef struct _tagBinTree { unsigned char value; struct _tagBinTree* left; struct _tagBinTree* right; }BinTree, *PBinTree; 先序递归形式的创建二叉树代码

hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

Binary Tree Traversals Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called

二叉树遍历,深度有限遍历,广度优先遍历,前序中序后续优先遍历,层次遍历

首先明白两个概念: 1. 深度遍历包括前中后序遍历三种: 2. 广度优先遍历就是层次遍历. PS: 前中后序遍历,如果使用递归遍历,都很简单易理解: 如果使用非递归方式,首先想到的就应该是使用栈结构来控制整个过程,因为递归也是利用栈来实现的: 前中后序遍历的非递归方式中,后序遍历的非递归方式相比较而言,略复杂. 直接上代码: #include "stdlib.h" #include <iostream> #include <stack> #include <

数据结构之二叉树 (构造 拷贝构造 以及前序中序后续三种遍历方法)

首先二叉树的节点定义如下: struct BinaryNode {                  BinaryNode *_left;                  BinaryNode *_right;                  T _data;                 BinaryNode( T data ) :_data(data), _left( NULL), _right(NULL )                 {}; }; 二叉树的结构以及接口如下 te

二叉树的前序/中序/后续遍历(递归+非递归)

这几日又把二叉树的递归写了一遍,原来是用C写的,自己写了一个栈,这一次直接用的C++,使用了自带的栈结构.代码如下: 1 /************************************************************************* 2 > Author: Yves 3 > E-mail: [email protected] 4 > File Name: BiTreeNew.cpp 5 > Description: ... 6 > Cre

面试之路(11)-java递归和非递归二叉树前序中序后序遍历

二叉树的遍历 对于二叉树来讲最主要.最基本的运算是遍历. 遍历二叉树 是指以一定的次序访问二叉树中的每个结点.所谓 访问结点 是指对结点进行各种操作的简称.例如,查询结点数据域的内容,或输出它的值,或找出结点位置,或是执行对结点的其他操作.遍历二叉树的过程实质是把二叉树的结点进行线性排列的过程.假设遍历二叉树时访问结点的操作就是输出结点数据域的值,那么遍历的结果得到一个线性序列. 从二叉树的递归定义可知,一棵非空的二叉树由根结点及左.右子树这三个基本部分组成.因此,在任一给定结点上,可以按某种次

HDU_1710_二叉树前序中序确定后序

研究生考试初试结束一个周了,开始准备复试了,又要开始刷题了. 给定一个二叉树前序和中序,确定后序. #include<iostream> #include<cstdio> using namespace std; int pre[1005],in[1005]; void getPostOrder(int ph,int ih,int len,int flag) { if(len<1) return; int i; for(i=0; pre[ph]!=in[ih+i]; i++)

二叉树-前序&amp;中序

题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回. # 返回构造的TreeNode根节点 def re_construct_binary_tree(pre, tin): if not pre or not tin: return None root = TreeNode(pre.pop()) index

二叉树前序中序遍历求后序遍历

已知先序和中序遍历序列,求后序遍历序列. 已知该二叉树的先序遍历序列为:A-B-D-E-G-C-F,中序遍历序列为:D-B-G-E-A-C-F. 接下来我们就可以求出该二叉树的后序遍历序列,具体步骤如下: 第一步:先求root根节点,根据先序遍历规则我们可知root为先序遍历序列的第一个节点,因此该二叉树的root节点为A. 第二步:求root的左子树和右子树,这点我们可以从中序遍历序列中找出,位于root节点A左侧的D-B-G-E为root的左子树,位于A右侧的C-F为右子树. 第三步:求ro