【数据结构】树的非递归遍历、层次遍历、第K层遍历规则

爱你呀 =w=

#include<windows.h>
#include<assert.h>
#include<limits.h>
#include<iostream>
#include<vector>
#include<time.h>
#include<stack>
#include <queue>

using namespace std;
#define END -1
typedef int ElemType;

typedef struct BtNode
{
	BtNode *leftchild;
	BtNode *rightchild;
	ElemType data;
}BtNode,*BinaryTree;

BtNode* BuyNode()
{
	BtNode *s = (BtNode*)malloc(sizeof(BtNode));
	if(s == NULL)
	{
		exit(1);
	}
	memset(s,0,sizeof(BtNode));
	return s;
}
void FreeNode(BtNode *s)
{
	free(s);
}
///
//非递归遍历排序 <结合栈进行操作>
void NiceInOrder(BtNode *ptr)  //中序
{
	if(ptr == NULL)
	{
		return ;
	}
	stack <BtNode *> st;
	while(ptr != NULL || !st.empty())
	{
		while(ptr != NULL)
		{
			st.push(ptr);
		    ptr = ptr->leftchild;
	     }
		ptr = st.top;   
		st.pop;  //出栈
		cout << ptr->data << " ";
		ptr = ptr->rightchild;
	}
	cout << endl;
}

void NicePastOrder(BtNode *ptr)  //后序
{
	if(ptr == NULL)
	{
		return ;
	}
	stack <BtNode *> st;
	BtNode* tag = NULL;  //标记
	while(ptr != NULL || !st.empty())
	{
		while(ptr != NULL)
		{
			st.push(ptr);
			ptr = ptr->leftchild;
			ptr = st.top;
			st.pop();
			if(ptr->rightchild == NULL || ptr->rightchild == tag) //右子树为空
			{
				cout << ptr->data <<" ";
				tag = ptr;
				ptr = NULL;
			}
			else //父节点再次入栈,进右子树
			{
				st.push(ptr);
				ptr = ptr->rightchild;
			}
		}
		cout << endl;
	}
}

//建立结构体,用访问次数解决问题
struct StkNode
{
	BtNode* pnode;
	int popnum;
	StkNode(BtNode *p = NULL):pnode(p),popnum(0){}
};
//后序
void StkNicePastOrder(BtNode* ptr)
{
	if(NULL == ptr)
	{
		return ;
	}
	stack<StkNode>st;
	st.push(StkNode(ptr));
	while(!st.empty())
	{
		StkNode node = st.top();
		st.pop();
		if(++node.popnum == 3)
		{
			cout << node.pnode->data <<" ";
		}
		else
		{
			st.push(node);
			if(node.popnum == 1 && node.pnode->leftchild != NULL)
			{
				st.push(StkNode(node.pnode->leftchild));
			}
			else if(node.popnum == 2 && node.pnode->rightchild != NULL)
			{
				st.push(StkNode(node.pnode->rightchild));
			}
		}
		cout << endl;
	}
}
//中序
void StkNiceInOrder(BtNode *ptr)
{
	if(ptr == NULL)
	{
		return ;
	}
	stack<StkNode>st;
	st.push(StkNode(ptr));
	while(!st.empty())
	{
		StkNode node = st.top();
		st.pop();
		if(++node.popnum == 2)
		{
			cout << node.pnode->data <<" ";
			if(node.pnode->leftchild != NULL)
			{
				st.push(node.pnode->leftchild);
			}
		}
		else
		{
			st.push(node);
			if(node.pnode->rightchild != NULL)
			{
				st.push(node.pnode->rightchild);
			}
		}
	}
	cout << endl;
}
//先序
void StkNicePreOrder(BtNode *ptr)
{
	if(ptr == NULL)
	{
		return ;
	}
	stack<BtNode *>st;
	st.push(ptr);
	while(!st.empty())
	{
		ptr = st.top();
		st.pop();
		if(ptr->rightchild != NULL)
		{
			st.push(ptr->rightchild);
		}
		if(ptr->leftchild != NULL)
		{
			st.push(ptr->leftchild);
		}
	}
	cout << endl;
}

//借助队列实现
//层次遍历
void NiceLevelOrder(BtNode *ptr)
{
	if(NULL == ptr)
	{
		return ;
	}
	queue<BtNode *>qu;
	qu.push(ptr);
	while(!qu.empty())
	{
		ptr = qu.front();
		qu.pop();
		cout << ptr->data;
		if(ptr->leftchild != NULL)
		{
			qu.push(ptr->leftchild);
		}
		if(ptr->rightchild != NULL)
		{
			qu.push(ptr->rightchild);
		}
	}
}
//打印第k层
void PrintKLevel(BtNode *ptr,int k)
{
	if(NULL == ptr && k == 0)
	{
		cout << ptr->data << " ";
	}
	else if(ptr != NULL)
	{
		PrintKLevel(ptr->leftchild,k-1);
		PrintKLevel(ptr->rightchild,k-1);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值