Inorder traversal non-recursive binary tree (use stack)

In order to achieve non-recursive binary tree traversal

  • Before the blog wrote achieve recursion, say white is their own calls itself constantly, keeping the root of the left-right order. Just write the logical structure, the program will own recursive complex process, that is, roughly:
void inTraverseByRecur(Tree& T)
{
	if(T)
	{	
		inTraverseByRecur(T->lchild);
		cout<<T->data;
		inTraverseByRecur(T->rchild);
	}
}
  • Rather than the recursive stack structure I used as an aid, because preorder its roots left and right, which itself traversal feature can be used continuously from the stack into the stack simulations. When we root of the tree T is, in order to achieve inorder traversal, requires the following steps:
    . ① If the presence of the root node T, T pushed onto the stack will, when T-> lchild is true (i.e. T has a left child), to update its left child as root: namely T = T-> lchild, repeat step ①, until it has been left to go until the left can not find child (this time in order to find the first junction sequence point), go to step ②.
    ②. Since no current left child node, the root according to preorder left and right, the node from the stack. If the node has a right child, the child will be seen as the root of the right, go to step ①, went to the order until the end of the sequence, the stack is empty.

  • The following is a complete code, a stack structure comprising a binary tree structure of nodes and their operating functions, recursive and non-recursive function I have realized on the inside.

  • Here are some details to be noted that the difference between transmission and transmission reference pointer, stack type data field elements of the node structure storage.

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

#define STACK_INIT_SIZE 100   //栈的初始化容量 
#define STACK_INC_SIZE 10	  //栈的分配增量 

typedef char ElementType;  //二叉树的数据域元素类型 

/*二叉树结点结构*/ 
typedef struct BiTree{
	struct BiTree* lchild;
	struct BiTree* rchild;
	ElementType data;
}TreeNode,*Tree;

typedef Tree elemType;	   //栈存储的数据元素类型为二叉树结点结构 

/*栈的数据结构*/ 
typedef struct{
	elemType* base;  //栈底指针 
	elemType* top;	 //栈顶指针 
	int stackSize;   //当前已分配的栈总存储空间 
}SqStack; 

void initStack(SqStack& S);  		   	  //构造空栈并初始化 
void pushStack(SqStack& S,elemType data); //数据元素进栈
void popStack(SqStack& S);  		      //数据元素出栈 
int  emptyStack(SqStack& S);			  //判断栈是否为空 
void createBiTree(Tree& T);				  //创建并添加二叉树信息,录入方式为根左右 
void inTraverseByRecur(Tree& T);		  //递归方式实现中序遍历二叉树
void inTraverseByNotRecur(Tree& T,SqStack& S);  //非递归方式实现中序遍历二叉树()
elemType getTop(SqStack& S);              //返回栈顶元素 

void initStack(SqStack& S)
{ 
	S.base=(elemType*)malloc(STACK_INIT_SIZE * sizeof(elemType));
	if(!S.base)  exit(1);
	S.top=S.base;
	S.stackSize=STACK_INIT_SIZE;  
} 

void pushStack(SqStack& S,elemType data)
{
	if(S.top-S.base>=S.stackSize) //栈满,扩充容量 
	{
		S.base=(elemType*)realloc(S.base,(S.stackSize+STACK_INC_SIZE) * sizeof(elemType));
		if(!S.base)  exit(1);
		S.top=S.base+S.stackSize; //栈顶改变
		S.stackSize+=STACK_INC_SIZE;
	}
	*S.top=data;
	S.top++;
}

void popStack(SqStack& S)
{
	if(S.top!=S.base)
	{
		S.top--;
	} 
}

int emptyStack(SqStack& S)
{
	//栈空则返回0 
	if(S.base==S.top)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

elemType getTop(SqStack& S)
{
	return *(S.top-1);
}	

void createBiTree(Tree& T)
{
	ElementType ch;
	cin>>ch;
	if(ch=='#') T=NULL;
	else
	{
		//if(!(T=(TreeNode*)malloc(sizeof(TreeNode))))
		if(!(T=(Tree)malloc(sizeof(TreeNode))))
		{
			cout<<"malloc fail"<<endl;
			exit(0);
		}
		T->data=ch;
		createBiTree(T->lchild);
		createBiTree(T->rchild);
	}
}

//实现递归方式的中序遍历
void inTraverseByRecur(Tree& T)
{
	//-+a##*b##-c##d##/e##f##
	if(T)
	{
		inTraverseByRecur(T->lchild);
		cout<<T->data;
		inTraverseByRecur(T->rchild);
	}
} 

//实现非递归方式的中序遍历
void inTraverseByNotRecur(Tree& T,SqStack& S)
{
	Tree p;
	p=T;
	//若栈为空,emptyStack(S)返回为0 
	while(p||emptyStack(S))  //只有当结点为空,栈也为空时退出循环 
	{
		while(p)   //结点不为空时,一直找左孩子 
		{
			pushStack(S,p);
			p=p->lchild;
		}		   
		//找不到左孩子时,若栈也不为空,则栈顶元素出栈,将栈顶元素的右孩子再作为根结点,继续找左孩子
		//这样持续下去最终会按照非递归的方式输出左根右 
		if(emptyStack(S))		
		{
			p=getTop(S);	 
			cout<<p->data;
			popStack(S);
			p=p->rchild;
		}
	}
}

int main()
{
	SqStack S;     			//定义栈S 
	Tree T1;	   			//定义二叉树的根结点 
	initStack(S);  			//初始化栈S 
	createBiTree(T1);		//创建并添加二叉树信息
	inTraverseByRecur(T1);  //递归方式中序遍历二叉树 
	cout<<endl; 
	inTraverseByNotRecur(T1,S);//非递归方式中序遍历二叉树 
	return 0;
}

Published 77 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42932834/article/details/94207849