C/C++算法实例(六)--树的遍历
来源:互联网 发布:淘宝店铺商标注册 编辑:程序博客网 时间:2024/06/05 03:44
1.已知前序中序求后序
procedure Solve(pre,mid:string);
var i:integer;
begin
if (pre='''') or (mid='''') then exit;
i:=pos(pre[1],mid);
solve(copy(pre,2,i),copy(mid,1,i-1));
solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));
post:=post+pre[1]; {加上根,递归结束后post即为后序遍历}
end;
2.已知中序后序求前序
procedure Solve(mid,post:string);
var i:integer;
begin
if (mid='''') or (post='''') then exit;
i:=pos(post[length(post)],mid);
pre:=pre+post[length(post)]; {加上根,递归结束后pre即为前序遍历}
solve(copy(mid,1,I-1),copy(post,1,I-1));
solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));
end;
3.已知前序后序求中序的一种
function ok(s1,s2:string):boolean;
var i,l:integer; p:boolean;
begin
ok:=true;
l:=length(s1);
for i:=1 to l do begin
p:=false;
for j:=1 to l do
if s1[i]=s2[j] then p:=true;
if not p then begin ok:=false;exit;end;
end;
end;
procedure solve(pre,post:string);
var i:integer;
begin
if (pre='''') or (post='''') then exit;
i:=0;
repeat
inc(i);
until ok(copy(pre,2,i),copy(post,1,i));
solve(copy(pre,2,i),copy(post,1,i));
midstr:=midstr+pre[1];
solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));
end;
procedure Solve(pre,mid:string);
var i:integer;
begin
if (pre='''') or (mid='''') then exit;
i:=pos(pre[1],mid);
solve(copy(pre,2,i),copy(mid,1,i-1));
solve(copy(pre,i+1,length(pre)-i),copy(mid,i+1,length(mid)-i));
post:=post+pre[1]; {加上根,递归结束后post即为后序遍历}
end;
2.已知中序后序求前序
procedure Solve(mid,post:string);
var i:integer;
begin
if (mid='''') or (post='''') then exit;
i:=pos(post[length(post)],mid);
pre:=pre+post[length(post)]; {加上根,递归结束后pre即为前序遍历}
solve(copy(mid,1,I-1),copy(post,1,I-1));
solve(copy(mid,I+1,length(mid)-I),copy(post,I,length(post)-i));
end;
3.已知前序后序求中序的一种
function ok(s1,s2:string):boolean;
var i,l:integer; p:boolean;
begin
ok:=true;
l:=length(s1);
for i:=1 to l do begin
p:=false;
for j:=1 to l do
if s1[i]=s2[j] then p:=true;
if not p then begin ok:=false;exit;end;
end;
end;
procedure solve(pre,post:string);
var i:integer;
begin
if (pre='''') or (post='''') then exit;
i:=0;
repeat
inc(i);
until ok(copy(pre,2,i),copy(post,1,i));
solve(copy(pre,2,i),copy(post,1,i));
midstr:=midstr+pre[1];
solve(copy(pre,i+2,length(pre)-i-1),copy(post,i+1,length(post)-i-1));
end;
- C/C++算法实例(六)--树的遍历
- 数据结构 - 二叉树的广度优先遍历算法(C++)
- 二叉树的遍历(c语言非递归算法)
- C学习笔记(六)字符串遍历
- C#--遍历文件夹实例
- <<c>>树的遍历
- C C++,算法实例 (转载)
- C语言 二叉树的结构和遍历算法
- 【算法】二叉树的递归遍历C语言实现
- 二叉树的前序、中序、后序三种遍历的六种实现方式(递归、非递归)(C++)
- C语言经典实例六
- 【数据结构与算法】(六) c 语言实现简单的二叉树静态创建及先序、中序、后序遍历
- 二叉树按层遍历打印的算法(c/c++)
- 关于树的一些算法操作包含树的非递归遍历(c源代码)
- 一起talk C栗子吧(第四十回:C语言实例--遍历二叉树)
- C语言 二叉树的遍历 递归和(多种)非递归算法
- C语言 二叉树的遍历递归和(多种)非递归算法
- 数据结构与算法(C++)——二叉树的遍历
- C/C++算法实例(五)--高精度计算
- pydev 配置解释器出错,unsupported version
- apkbuilder命令详解
- c语言文件操作相关函数的用法
- cocoa touch layer下面的几个点连载之--CoreText
- C/C++算法实例(六)--树的遍历
- cocoa touch layer下面的几个点连载之--CoreImage
- Linux下两个网络函数inet_addr和inet_network (2008-09-01 01:42)
- C/C++算法实例(七)--其它
- 递归删除SVN目录
- svn
- 优化UITableView的性能
- 添加、查看、删除外键
- 在xp中运行 .air 文件--Adobe AIR Runtime for Window