码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的非递归遍历

时间:2020-11-01 22:08:22      阅读:16      评论:0      收藏:0      [点我收藏+]

标签:type   递归算法   算法   二叉树   非递归算法   memset   recursion   返回   child   

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

1、提供一个顺序存储的栈

#define  max 1024

struct sstack
{
	void * data[max];  //栈的数组

	int m_size; //栈大小
};

typedef void * seqstack;

//初始化栈
seqstack init_seqstack()
{
	struct sstack *  mystack = (struct sstack *)malloc(sizeof(struct sstack));

	if (mystack == NULL)
	{
		return NULL;
	}

	//初始化数组
	for(int i = 0; i<max; i++)
	{
		mystack->data[i] = 0;
	}
	//或者 
	//memset(mystack->data, 0, sizeof(void *)* max);

	//初始化栈大小
	mystack->m_size = 0;

	return mystack;
}
//入栈
void push_seqstack(seqstack stack , void * data)
{
	//入栈本质  --- 数组尾插
	if (stack == NULL)
	{
		return;
	}
	if ( data == NULL)
	{
		return;
	}

	struct sstack * mystack = (struct sstack * )stack; //强制转换成自己所需要的类型 
	if (mystack->m_size == max)
	{
		return;
	}

	mystack->data[mystack->m_size] = data;

	mystack->m_size++;
}
//出栈
void pop_seqstack(seqstack stack)
{
	//出栈本质  --- 数组尾删
	if (stack == NULL)
	{
		return;
	}

	struct sstack * mystack = (struct sstack * )stack;

	if (mystack->m_size == 0)
	{
		return;
	}

	mystack->data[mystack->m_size - 1] = NULL;

	mystack->m_size--;

}
//返回栈顶
void * top_seqstack(seqstack stack)
{
	if (stack == NULL)
	{
		return NULL;
	}

	struct sstack * mystack = (struct sstack * )stack;

	if (mystack->m_size == 0)
	{
		return NULL;
	}
	return mystack->data[mystack->m_size - 1];
}
//返回栈大小
int size_seqstack(seqstack stack)
{
	if (stack == NULL)
	{
		return -1;
	}

	struct sstack * mystack = (struct sstack * )stack;

	return mystack->m_size;

}
//判断栈是否为空
int isempty_seqstack(seqstack stack)
{
	if (stack == NULL)
	{
		return -1;//返回-1代表真  空栈
	}

	struct sstack * mystack = (struct sstack * )stack;

	if (mystack->m_size == 0)
	{
		return 1;
	}

	return 0; //返回0 代表 不是空栈

}
//销毁栈
void destroy_seqstack(seqstack stack)
{
	if (stack == NULL)
	{
		return;
	}

	free(stack);
	stack = NULL;
}
/* 栈的接口到此结束 */

2、构建一棵二叉树

typedef struct BinaryNode //二叉树结点 
{
	//数据域 
	char data;
	//指针域 
	struct BinaryNode * lChild;  //左孩子 
	struct BinaryNode * rChild;  //右孩子 
	//标志
	int flag; 
	
}BiNode;


//二叉树的非递归算法 
void nonRecursion(BiNode * root)
{
	 //初始化栈 
	 seqstack mystack = init_seqstack();
	 
	 //1. 将根节点 入栈 
	 push_seqstack(mystack, root);
	 //2. 进入循环:
	 while(size_seqstack(mystack)>0)
	 {
	 	//获取栈顶元素
		 BiNode * pTop = (BiNode * )top_seqstack(mystack);  //把void *强制转换成所需要的类型 
	 	
	 	//出栈 
	 	pop_seqstack(mystack);
	 	
	 	if(pTop->flag == 1)
	 	{
	 		printf("%c ", pTop->data);
	 		continue;
	 	}
	 	
	 	pTop->flag = 1;
	 	
	 	//将结点右子树 、左子树、 根  入栈,  出栈结果:先序
	 	if(pTop->rChild != NULL)
	 	{
	 		push_seqstack(mystack, pTop->rChild);
	 	}
	 	if(pTop->lChild != NULL)
	 	{
	 		push_seqstack(mystack, pTop->lChild);
	 	}
	 	push_seqstack(mystack, pTop);
	 	
	 }
	 
	 //销毁栈 
	 destroy_seqstack(mystack);
}
 
 
void test01()
{
	//对成员初始化 
	BiNode nodeA = {‘A‘, NULL, NULL, 0};
	BiNode nodeB = {‘B‘, NULL, NULL, 0};
	BiNode nodeC = {‘C‘, NULL, NULL, 0};
	BiNode nodeD = {‘D‘, NULL, NULL, 0};
	BiNode nodeE = {‘E‘, NULL, NULL, 0};
	BiNode nodeF = {‘F‘, NULL, NULL, 0};
	BiNode nodeG = {‘G‘, NULL, NULL, 0};
	BiNode nodeH = {‘H‘, NULL, NULL, 0};
	
	//建立关系 
	nodeA.lChild = &nodeB;
	nodeA.rChild = &nodeF;
	
	nodeB.rChild = &nodeC;
	
	nodeC.lChild = &nodeD;
	nodeC.rChild = &nodeE;
	
	nodeF.rChild = &nodeG;
	
	nodeG.lChild = &nodeH;
	
	//非递归遍历
	nonRecursion(&nodeA);//传入树根 
	 
}
int main(){
	test01();


	system("pause");
	return 0;
}

二叉树的非递归遍历

标签:type   递归算法   算法   二叉树   非递归算法   memset   recursion   返回   child   

原文地址:https://www.cnblogs.com/gmengshuai/p/13909786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!