二叉树的遍历和生成——Pascal实现
2015-11-14 19:06
330 查看
吾今日写了一个关于二叉树的程序:
program Project1; type btree=^node; node=record data:char; l,r:btree; end; var head : btree; function inittree(var t:btree):btree; var ch:char; begin read(ch); if (ch='#') then exit(nil) else begin new(t); t^.data:=ch; t^.l:=nil; t^.r:=nil; inittree(t^.l); inittree(t^.r); end; end; procedure TBT_pre(var p:btree); begin if (p=nil) then exit; write(p^.data:2); TBT_pre(p^.l); TBT_pre(p^.r); end; procedure TBT_in(var p:btree); begin if (p=nil) then exit; TBT_in(p^.l); write(p^.data:2); TBT_in(p^.r); end; procedure TBT_post(var p:btree); begin if (p=nil) then exit; TBT_post(p^.l); TBT_post(p^.r); write(p^.data:2); end; begin new(head); writeln('please input string like ABD##E##C## to make preorder binary tree(#:means nil)'); inittree(head); readln; write('this binary tree preorder is: '); TBT_pre(head); writeln; write('this binary tree inorder is: '); TBT_in(head); writeln; write('this binary tree postorder is:'); TBT_post(head); readln; end.
相关文章推荐
- P问题、NP问题、NPC问题、NP难问题的概念
- raw_input 和input的区别
- 谈谈传递闭包以及自己杂想
- 1009. Product of Polynomials (25)
- vijos P1051 送给圣诞夜的极光
- kvo
- vijos P1051 送给圣诞夜的极光
- Android开发Vitamio开发框架Audio音频播放失败
- 自己动手做简易搜索引擎
- Excel文件读取
- hdoj 2151 Worm【动态规划】
- 数据结构(Java)——栈的实现总结
- 10.UIImageView图片视图
- 二维码的生成细节和原理
- 黑马视频-SQL之case
- 为什么需要定义虚的析构函数?
- javaScript-引用类型(一)
- SQLiteDataBase(通过测试类测试写好的SQLite的相关数据库操作)
- GLib中的数据类型及操作
- Git上手(终结)