非递归先序遍历二叉树
P
P=root
访问节点P,并将P进栈
P=P-LChild
P为空?
N
栈为空?
出栈—P,
P=P-RChild
结束
Y
N
Y
非递归先序遍历二叉树
用扩展先序遍历序列建立二叉树
读入字符
读入字符?ch
ch=’.’?
建立根结点root,并将根结点进栈;pre=root
P=P-LChild
读入字符—ch
ch=’.’?
建立结点P,P进栈; pre-LChild=P;pre=p
Pre-LChild=NULL
出栈结点?pre
读入字符—ch
ch=’.’?
建立结点P,P进栈; pre-RChild=P;pre=P
Pre-RChild=NULL
栈空?
出栈结点?pre
结束
Y
N
N
Y
N
Y
根据扩展先序遍历序列建立二叉树
Root=NULL,结素
Void CreateBiTree(BiTree *root)
{
int CreateBiTree (BiTree Root)
{
char ch;
char *x;
InitStack(S);
BitNode *p,*pre;
ch=getchar();
if (ch==.)
{
Root=NULL;
return ;
}
Root=(BiTree)malloc(sizeof(BiTNode));
Root-data=ch;
pre=root;
ch=getchar();
while (1)
{
while(ch!=.)
{
p=(BiTree)malloc(sizeof(BiTNode));
p-data=ch;
PushStack(S,p);
p=pre-LChild;
pre=p;
ch=getchar();
};
pre-LChild=NULL;
PopStack(S,x);
pre=x;
while (ch==.)
{
pre-RChild=NULL;
if (!IsEmptyStack(S))
{
PopStack(S,x);
pre=x;
}
else return;
}
}
}
}
您可能关注的文档
最近下载
- 2025年教师资格考试高中学科知识与教学能力思想政治试卷与参考答案.docx VIP
- 基建项目财务咨询方案.docx VIP
- 五年(2021-2025)高考英语真题分类汇编专题17 语法填空(全国)(原卷版).pdf VIP
- 2026年党建重点工作(2篇).docx VIP
- 模拟经营类游戏的体验设计研究.pdf VIP
- 极限配合与技术测量基础第五版全套PPT电子课件.ppt
- 工程财务咨询方案.docx VIP
- 2026年山东劳动职业技术学院单招(计算机)测试备考题库附答案解析.docx VIP
- 6.2 我国四大地理区域 第1课时(教学课件)地理粤人版2024八年级下册.pptx VIP
- 时尚文化与社会意义研究报告-时尚文化与社会意义的探究.pptx
原创力文档

文档评论(0)