[Java算法分析与设计]二叉树的遍历
来源:互联网 发布:淘宝智能版装修素材 编辑:程序博客网 时间:2024/06/08 02:15
设计Node节点类:
package com.chen.arithmetic_test.BiTree_test;/** * Created by ChenMP on 2017/7/13. */public class BiTreeNode { private Object data; private BiTreeNode leftChild; private BiTreeNode rightChild; public BiTreeNode() { } public BiTreeNode(Object data) { this.data = data; } public BiTreeNode(Object data, BiTreeNode leftChild, BiTreeNode rightChild) { this.data = data; this.leftChild = leftChild; this.rightChild = rightChild; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public BiTreeNode getLeftChild() { return leftChild; } public void setLeftChild(BiTreeNode leftChild) { this.leftChild = leftChild; } public BiTreeNode getRightChild() { return rightChild; } public void setRightChild(BiTreeNode rightChild) { this.rightChild = rightChild; }}
设计二叉树的遍历实现:
package com.chen.arithmetic_test.BiTree_test;import java.util.LinkedList;import java.util.Queue;/** * Created by ChenMP on 2017/7/13. */public class Traverse { //前序遍历 public static void preOrder(BiTreeNode root) { if (root != null) { System.out.print("_" + root.getData()); //打印根节点 if (root.getLeftChild() != null) preOrder(root.getLeftChild()); //左子节点递归 if (root.getRightChild() != null) preOrder(root.getRightChild()); //右子节点递归 } } //中序遍历 public static void inOrder(BiTreeNode root) { if (root != null) { if (root.getLeftChild() != null) inOrder(root.getLeftChild()); //左子节点递归 System.out.print("_" + root.getData()); //打印根节点 if (root.getRightChild() != null) inOrder(root.getRightChild()); //右子节点递归 } } //后序遍历 public static void postOrder(BiTreeNode root) { if (root != null) { if (root.getLeftChild() != null) postOrder(root.getLeftChild()); //左子节点递归 if (root.getRightChild() != null) inOrder(root.getRightChild()); //右子节点递归 System.out.print("_" + root.getData()); //打印根节点 } } //层序遍历 public static void levOrder(BiTreeNode root) { Queue<BiTreeNode> queue = new LinkedList<BiTreeNode>(); if (root == null) return ; BiTreeNode curr; queue.add(root); while(!queue.isEmpty()) { curr = queue.remove(); //获取顺序表第一位节点 System.out.print("_" + curr.getData()); if (curr.getLeftChild() != null) queue.add(curr.getLeftChild()); if (curr.getRightChild() != null) queue.add(curr.getRightChild()); } }}
设计测试类
package com.chen.arithmetic_test.BiTree_test;/** * Created by ChenMP on 2017/7/13. */public class Test { /**二叉树形 * A * B C * D E F * G * H I */ public static BiTreeNode makeTree() { BiTreeNode b,c,d,e,f,g,h,i; i = new BiTreeNode(new Character('I'), null, null); h = new BiTreeNode(new Character('H'), null, null); g = new BiTreeNode(new Character('G'), h, i); d = new BiTreeNode(new Character('D'), null, g); b = new BiTreeNode(new Character('B'), d, null); e = new BiTreeNode(new Character('E'), null, null); f = new BiTreeNode(new Character('F'), null, null); c = new BiTreeNode(new Character('C'), e, f); return new BiTreeNode(new Character('A'), b, c); } public static void main(String[] args) { BiTreeNode root = Test.makeTree(); System.out.println("前序遍历:"); Traverse.preOrder(root); System.out.println("\n中序遍历:"); Traverse.inOrder(root); System.out.println("\n后序遍历:"); Traverse.postOrder(root); System.out.println("\n层序遍历"); Traverse.levOrder(root); }}
阅读全文
0 0
- [Java算法分析与设计]二叉树的遍历
- [Java算法分析与设计]二叉树非递归实现遍历
- Java数据结构与算法分析《十二》遍历二叉树
- 数据结构与算法分析笔记与总结(java实现)--二叉树20:二叉搜索树的后序遍历序列
- 【算法设计-二叉树遍历】二叉树的递归与非递归遍历方法
- 【数据结构与算法】二叉树的遍历
- C递归算法与栈的分析,非完全二叉树遍历分析---ShinePans
- 数据结构与算法分析笔记与总结(java实现)--二叉树3:二叉树按层遍历打印练习
- java二叉树的遍历算法
- java二叉树的遍历算法
- java二叉树的遍历算法
- java二叉树的遍历算法
- java二叉树的遍历算法
- 二叉树的遍历算法Java实现
- java二叉树的创建与遍历
- Java二叉树的构建与遍历
- 数据结构与算法分析(Java语言描述)(17)—— 二叉搜索树的深度优先与广度优先遍历
- 数据结构与算法分析笔记与总结(java实现)--二叉树11:二叉树的深度
- Http中各状态码的意义
- According to TLD or attribute directive in tag file, attribute value does not accept any expression
- 去掉导航栏下面的黑线
- Spring MVC控制层封装的数据在前台如何访问?
- 关于JavaScript拼接字符串问题
- [Java算法分析与设计]二叉树的遍历
- JSTL关于<c:forEach>用法
- mysql分表后进行合并
- 动态规划学习笔记
- 两层c:forEach循环嵌套
- MyEclipce中的jdk问题
- JQuery实现动态数据二级联动
- tf.nn has no attribute rnn_cell in version 1.0.1
- 错误:Value \'0000-00-00 00:00:00\' can not be represented as java.sql.Timestamp;的解决