Java二叉树四种遍历算法
来源:互联网 发布:多益网络笔试题 编辑:程序博客网 时间:2024/06/04 19:12
构建二叉树
class Tree{ public Tree leftNode;//左节点 public Tree rightNode;//右节点 public int rootNode;//根节点 public Tree(int rootNode) { this.rootNode = rootNode; } //向二叉树中插入节点 public void insertNode(Tree tree,int rootNode){ if (rootNode > tree.rootNode) { if (tree.rightNode == null) { tree.rightNode = new Tree(rootNode); }else { insertNode(tree.rightNode,rootNode); } }else { if (tree.leftNode == null) { tree.leftNode = new Tree(rootNode); }else { insertNode(tree.leftNode,rootNode); } } }}
1、先根遍历public static void preRoot(Tree tree){ if (tree != null) { System.out.println(tree.rootNode); preRoot(tree.leftNode); preRoot(tree.rightNode); }}
2、中根遍历public static void midRoot(Tree tree){ if (tree != null) { midRoot(tree.leftNode); System.out.println(tree.rootNode); midRoot(tree.rightNode); }}
3、//后根遍历public static void posRoot(Tree tree){ if (tree != null) { posRoot(tree.leftNode); posRoot(tree.rightNode); System.out.println(tree.rootNode); }}
4、//层次遍历public static void level(Tree tree){ if (tree != null) { List<Tree> list = new ArrayList<Tree>(); list.add(tree); while (!list.isEmpty()) { Tree t = list.remove(0); System.out.println(t.rootNode); if (t.leftNode!=null) { list.add(t.leftNode); } if (t.rightNode != null) { list.add(t.rightNode); } } }}
1 0
- Java二叉树四种遍历算法
- Java实现二叉树遍历算法
- java二叉树的遍历算法
- Java实现二叉树三种遍历算法
- java二叉树的遍历算法
- 基础算法001---二叉树遍历 Java
- java二叉树的遍历算法
- java二叉树的遍历算法
- java二叉树的遍历算法
- 二叉树的遍历算法Java实现
- java实现二叉树常见遍历算法
- 二叉树层序遍历算法
- 二叉树遍历算法
- 二叉树遍历算法
- 二叉树遍历算法
- 二叉树遍历算法
- 二叉树遍历算法
- 二叉树遍历算法
- Spark入门实战系列--10.分布式内存文件系统Tachyon介绍及安装部署
- windows.location.href 中文汉字乱码
- JQuery库:(二)选择器
- 后台定时更新时钟,JVM退出时,线程自动回收---跟rocketmq学习
- 用正则替换标签内的所有数据
- Java二叉树四种遍历算法
- win10 uwp 分治法
- java.lang.NoSuchFieldError: INSTANCE
- 执行顺序
- FZU 2099 魔法阵(计算几何)
- 数据结构(11):线索二叉树(未完成)
- 小猪CMS微信管理平吧,用户登录出现小猪广告解决方法
- Window Direct3D游戏开发入门——Bomb游戏
- UiViewController 里frame和bounds取值有误