遍历

feiyusx 2003-08-03 09:51:51
有没有不通过递归,遍历不完全二叉树,并找出所有空的节点(注:二叉树已经通过数据库实现)
...全文
39 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyusx 2003-08-05
  • 打赏
  • 举报
回复
那么怎样用表格画出二叉树,包括空节点和非空节点呢?不明白
zorou_fatal 2003-08-03
  • 打赏
  • 举报
回复
dim cstack(20)
dim top
dim current
top=-1
top=top+1
cstack(top)=根结点
'前序
while not top=-1
current=cstack(top)
response.write current
top=top-1
if current的右结点不为空 then
top=top+1
cstack(top)=current的右结点
end if
if current的左结点不为空 then
top=top+1
cstack(top)=current的右结点
end if
wend
'中序
temp=树的结点数
dim PopTim(temp)
for i= 0 to temp-1
PopTim(i)=0'初始化计数组
next

top=top+1
cstack(top)=根结点
while not top=-1
PopTim(top)=PopTim(top)+1
current=cstack(top)
top=top-1

if(PopTim(top+1)=2) then
response.write current
if(current的右结点不为空) then
top=top+1
cstack(top)=current的右结点
end if
end if

top=top+1
cstack(top)=current

if(current的左结点不为空) then
top=top+1
cstack(top)=current的左结点
end if

wend
'后序
temp=树的结点数
dim PopTim(temp)
for i= 0 to temp-1
PopTim(i)=0'初始化计数组
next

top=top+1
cstack(top)=根结点
while not top=-1
PopTim(top)=PopTim(top)+1
current=cstack(top)
top=top-1
if(PopTim(top+1)=3) then
response.write current
end if
top=top+1
cstack(top)=current
if(PopTim(top-1)=1) then

if(current的左结点不为空) then
top=top+1
cstack(top)=current的左结点
else
top=top+1
cstack(top)=current的右结点
end if
end if

top=top+1
cstack(top)=current

if(current的左结点不为空) then
top=top+1
cstack(top)=current的左结点
end if

wend

数据结构思想来自清华大学数据结构(用面向对象方法与c++描述)殷人昆等
page179-page181
树的非递归遍历

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧