递归与非递归实现树的遍历(java)
2016-12-13 16:52
316 查看
package test; import java.util.Stack; public class Sort { public static void main(String[] args) { Node root9=new Node(9,null,null); Node root8=new Node(8,null,null); Node root7=new Node(7,null,null); Node root6=new Node(6,null,null); Node root5=new Node(5,null,null); Node root4=new Node(4,root8,root9); Node root3=new Node(3,root6,root7); Node root2=new Node(2,root4,root5); Node root1=new Node(1,root2,root3); System.out.println("pre..."); preOrderTraverse(root1); System.out.println(); preOrderTraverseNoRecursion(root1); System.out.println(); System.out.println("in..."); inOrderTraverse(root1); System.out.println(); inOrderTraverseNoRecursion(root1); System.out.println(); System.out.println("post..."); postOrderTraverse(root1); System.out.println(); postOrderTraverseNoRecursion(root1); System.out.println(); } static class Node{ public Node(Integer data,Node left,Node right) { this.data=data; this.left=left; this.right=right; } public Node() { super(); } Integer data; Node left; Node right; } static void preOrderTraverse(Node root){ if(root!=null){ System.out.print(root.data+" "); preOrderTraverse(root.left); preOrderTraverse(root.right); } } static void preOrderTraverseNoRecursion(Node root){ Stack<Node> s=new Stack<Node>(); Stack<Node> s1=new Stack<Node>(); Node p,p1; s.push(root); while(!s.isEmpty()){ while((p=s.lastElement())!=null){ System.out.print(p.data+" "); s.push(p.left); } s.pop(); if(!s.isEmpty()){ p=s.pop(); s.push(p.right); } } } static void inOrderTraverse(Node root){ if(root!=null){ inOrderTraverse(root.left); System.out.print(root.data+" "); inOrderTraverse(root.right); } } static void inOrderTraverseNoRecursion(Node root){ Stack<Node> s=new Stack<Node>(); Stack<Node> s1=new Stack<Node>(); Node p,p1; s.push(root); while(!s.isEmpty()){ while((p=s.lastElement())!=null){ s.push(p.left); } s.pop(); if(!s.isEmpty()){ p=s.pop(); System.out.print(p.data+" "); s.push(p.right); } } } static void postOrderTraverse(Node root){ if(root!=null){ postOrderTraverse(root.left); postOrderTraverse(root.right); System.out.print(root.data+" "); } } static void postOrderTraverseNoRecursion(Node root){ Stack<Node> s=new Stack<Node>(); Stack<Node> s1=new Stack<Node>(); Node p,p1; s.push(root); while(!s.isEmpty()){ while((p=s.lastElement())!=null){ s.push(p.left); } s.pop(); while(!s1.isEmpty()&&s1.lastElement()==s.lastElement()){ s.pop(); p1=s1.pop(); System.out.print(p1.data+" "); } if(!s.isEmpty()){ p=s.lastElement(); s1.push(p); s.push(p.right); } } } }
相关文章推荐
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- C语言实现二叉树的常用的算法(递归与非递归实现遍历)
- 树的三种遍历(递归与非递归实现)
- Java实现二叉树的遍历(递归和非递归)
- 二叉树的遍历 递归非递归 思路和 java实现
- 【应聘笔记系列】二叉树的递归与非递归遍历实现
- 面试题-程序题-Java递归实现遍历文件目录(所有文件夹和子文件)
- java语言实现二叉排序树(结点的增、删、查、改,递归、非递归遍历)
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树非递归遍历(前序,后序),java实现
- Java实现树的遍历(前序、中序、后续(递归|非递归)、层次)
- 前序 中序 后序 遍历 递归 非递归算法 java实现
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- JAVA实现二叉树及递归遍历二叉树
- 有关二叉树的相关实现:建树,遍历(递归与非递归实现)
- 我用Java实现的二叉树的遍历(递归和非递归)
- 图的深度优先遍历算法的递归与非递归JAVA实现
- 递归与非递归实现二叉树的遍历