java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
来源:互联网 发布:保定优化公司 编辑:程序博客网 时间:2024/06/06 21:39
TreeNode.java
public class TreeNode{public TreeNode left;public TreeNode right;public String value;public TreeNode(TreeNode left, TreeNode right, String value) {this.left = left;this.right = right;this.value = value;}}
BinaryTree.java
import java.util.LinkedList;import java.util.Queue;import java.util.Stack;public class BinaryTree{// 获取二叉树的高度public int getTreeHeight(TreeNode root) {if (root == null)return 0;if (root.left == null && root.right == null)return 1;return 1 + Math.max(getTreeHeight(root.left), getTreeHeight(root.right));}// 递归前序遍历public void preOrder1(TreeNode root) {if (root != null) {System.out.print(root.value);preOrder1(root.left);preOrder1(root.right);}}// 递归中序遍历public void inOrder1(TreeNode root) {if (root != null) {inOrder1(root.left);System.out.print(root.value);inOrder1(root.right);}}// 递归后序遍历public void postOrder1(TreeNode root) {if (root != null) {postOrder1(root.left);postOrder1(root.right);System.out.print(root.value);}}// 非递归前序遍历public void preOrder2(TreeNode root) {Stack<TreeNode> s = new Stack<TreeNode>();TreeNode p = root;while (p != null || !s.isEmpty()) {while (p != null) {s.push(p);System.out.print(p.value);p = p.left;}if (!s.isEmpty()) {p = s.pop();p = p.right;}}}// 非递归中序遍历public void inOrder2(TreeNode root) {Stack<TreeNode> s = new Stack<TreeNode>();TreeNode p = root;while (p != null || !s.isEmpty()) {while (p != null) {s.push(p);p = p.left;}if (!s.isEmpty()) {p = s.pop();System.out.print(p.value);p = p.right;}}}/** * 非递归后序遍历 * 只需保证每个节点都要在其左右孩子节点之后进行访问 * 也就是说当该节点没有孩子节点或者有孩子节点但孩子节点已经被访问时才能进行访问 */public void postOrder2(TreeNode root) {Stack<TreeNode> s = new Stack<TreeNode>();TreeNode p = root;TreeNode cur = null;TreeNode pre = null;s.push(p);while (!s.isEmpty()) {cur = s.peek();if ((cur.left == null && cur.right == null)|| (pre != null && (pre == cur.left || pre == cur.right))) {System.out.print(cur.value);s.pop();pre = cur;} else {if (cur.right != null)s.push(cur.right);if (cur.left != null)s.push(cur.left);}}}// 二叉树层序遍历public void levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<TreeNode>();TreeNode p = root;if (p != null)queue.add(p);while (!queue.isEmpty()) {p = queue.poll();System.out.print(p.value);if (p.left != null)queue.offer(p.left);if (p.right != null)queue.offer(p.right);}}// 测试public static void main(String[] args) {// TODO Auto-generated method stub// 构造二叉树TreeNode d = new TreeNode(null, null, "D");TreeNode e = new TreeNode(null, null, "E");TreeNode f = new TreeNode(null, null, "F");TreeNode b = new TreeNode(d, e, "B");TreeNode c = new TreeNode(f, null, "C");TreeNode root = new TreeNode(b, c, "A");BinaryTree bt = new BinaryTree();System.out.println("二叉树的高度:" + bt.getTreeHeight(root));System.out.println("================二叉树递归遍历===================");System.out.println("前序遍历:");bt.preOrder1(root);System.out.println("\n中序遍历:");bt.inOrder1(root);System.out.println("\n后序遍历:");bt.postOrder1(root);System.out.println("\n================二叉树非递归遍历=================");System.out.println("前序遍历:");bt.preOrder2(root);System.out.println("\n中序遍历:");bt.inOrder2(root);System.out.println("\n后序遍历:");bt.postOrder2(root);System.out.println("\n================二叉树层序遍历===================");bt.levelOrder(root);}}
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- 二叉树的遍历(前序、中序、后序、层序),递归和非递归实现
- 二叉树的遍历(前序,中序,后序,层序)--递归和非递归算法实现
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- C++实现二叉树的非递归遍历(层,前,中,后序)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
- 二叉树的前序,中序,后序遍历。用递归和非递归实现
- 二叉树的遍历:前序、中序、后序、层序的非递归实现
- 二叉树的,前/中/后序的遍历( 递归,非递归),层序遍历,以及各种应用功能
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- C# 二叉树的非递归中序 前序 后序遍历 层序遍历
- 常用的排序算法的时间复杂度和空间复杂度
- 如何更好的优化自己的网站
- jquery的图片效果和内容滑动及弹出插件
- 总算解决了QT的一个诡异问题
- sql使用cast进行数据类型转换
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- 哈希查找因何快?我们使用它需要付出什么代价
- VC 打开文件框选择文件
- 在C++中使用TinyXML2解析xml
- 一些CSS学习网站
- winform的DataGridView关键字高亮显示
- 常用排序算法的时间复杂度和空间复杂度
- 套接字I/O模型之WSAEventSelect
- 最近 由于项目需要被迫自己写一个 ajax gird 记录-1