设为首页 加入收藏

TOP

二叉树的非递归遍历--京东2015笔试回忆(二)
2015-07-20 17:30:16 来源: 作者: 【 】 浏览:15
Tags:京东 2015 笔试 回忆
,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。

复制代码
void postOrder3(BinTree *root)     //非递归后序遍历
{
stack s;
BinTree *cur; //当前结点
BinTree *pre=NULL; //前一次访问的结点
s.push(root);
while(!s.empty())
{
cur=s.top();
if((cur->lchild==NULL&&cur->rchild==NULL)||
(pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))
{
cout< data<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过
s.pop();
pre=cur;
}
else
{
if(cur->rchild!=NULL)
s.push(cur->rchild);
if(cur->lchild!=NULL)
s.push(cur->lchild);
}
}
}
复制代码

四.整个程序完整的代码

复制代码
/*二叉树的遍历* 2011.8.25*/ 

#include
#include
#include
using namespace std;

typedef struct node
{
char data;
struct node *lchild,*rchild;
}BinTree;

typedef struct node1
{
BinTree *btnode;
bool isFirst;
}BTNode;


void creatBinTree(char *s,BinTree *&root) //创建二叉树,s为形如A(B,C(D,E))形式的字符串
{
int i;
bool isRight=false;
stack s1; //存放结点
stack s2; //存放分隔符
BinTree *p,*temp;
root->data=s[0];
root->lchild=NULL;
root->rchild=NULL;
s1.push(root);
i=1;
while(i {
if(s[i]=='(')
{
s2.push(s[i]);
isRight=false;
}
else if(s[i]==',')
{
isRight=true;
}
else if(s[i]==')')
{
s1.pop();
s2.pop();
}
else if(isalpha(s[i]))
{
p=(BinTree *)malloc(sizeof(BinTree));
p->data=s[i];
p->lchild=NULL;
p->rchild=NULL;
temp=s1.top();
if(isRight==true)
{
temp->rchild=p;
cout< data<<"的右孩子是"< }
else
{
temp->lchild=p;
cout< data<<"的左孩子是"< }
if(s[i+1]=='(')
s1.push(p);
}
i++;
}
}

void display(BinTree *root) //显示树形结构
{
if(root!=NULL)
{
cout< data;
if(root->lchild!=NULL)
{
cout<<'(';
display(root->lchild);
}
if(root->rchild!=NULL)
{
cout<<',';
display(root->rchild);
cout<<')';
}
}
}

void preOrder1(BinTree *root) //递归前序遍历
{
if(root!=NULL)
{
cout< data<<" ";
preOrder1(root->lchild);
preOrder1(root->rchild);
}
}

void inOrder1(BinTree *root) //递归中序遍历
{
if(root!=NULL)
{
inOrder1(root->lchild);
cout< data<<" ";
inOrder1(root->rchild);
}
}

void postOrder1(BinTree *root) //递归后序遍历
{
if(root!=NULL)
{
postOrder1(root->lchild);
postOrder1(root->rchild);
cout< data<<" ";
}
}

void preOrder2(BinTree *root) //非递归前序遍历
{
stack s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
cout< data<<" ";
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->rchild;
}
}
}

void inOrder2(BinTree *root) //非递归中序遍历
{
stack s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
cout< data<<" ";
s.pop();
p=p->rchild;
}
}
}

void postOrder2(BinTree *root) //非递归后序遍历
{
stack s;
BinTree *p=root;
BTNode *temp;
while(p!=NULL||!s.empty())
{
while(p!=NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点
{
BTNode *btn=(BTNode *)malloc(sizeof(BTNode));
btn->btnode=p;
btn->isFirst=true;
s.push(btn);
p=p->lchild;
}
if(!s.empty())
{
temp=s.top();
s.pop();
if(temp->isFirst==true) //表示是第一次出现在栈顶
{
temp->isFirst=false;
s.push(temp);
p=temp->btnode->rchild;
}
else //第二次出现在栈顶
{
cout< btnode->data<<" ";
p=NULL;
}
}
}
}

void postOrder3(BinTree *root) //非递归后序遍历
{
stack s;
BinTree *cur; //当前结点
BinTree *pre=NULL; //前一次访问的结点
s.push(root);
while(!s.empty())
{
cur=s.top();
if((cur->lchild==NULL&&cur->rchild==NULL)||
(pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))
{
cout< data<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过
s.pop();
pre=cur;
}
else
{
if(cur->rchild!=NULL)
s.push(cur->rchild);
if(cur->lchild!=NULL)
s.push(cur->lchild);
}
}
}


int main(int argc, cha
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Codeforces 328A-IQ Test(数列) 下一篇C++ STL 源码学习(之deque篇)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)