【数据结构】树的非递归遍历、层次遍历、第K层遍历规则
#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);
}
}