Java 二叉树的四种递归与非递归遍历
2017-09-06 10:29
337 查看
二叉树基础知识,四种遍历分别为前序、中序、后序,层次遍历。
前序的递归/非递归遍历代码为:
public static void main(String[] arg) { Node1 a = new Node1('A'); a.left=new Node1('B'); a.rigth=new Node1('C'); a.left.left=new Node1('D'); a.left.rigth=new Node1('E'); a.rigth.left=new Node1('F'); a.rigth.rigth=new Node1('G'); System.out.println("前序递归输出:"); perOrderRecur(a);//递归输出 System.out.println("\n"+"前序非递归输出:"); perOrderRecurs(a);//递归输出 } private static void perOrderRecurs(Node1 a) { if(a==null) { return ; } ArrayList<Node1> list=new ArrayList<>(); Stack<Node1> stack=new Stack<Node1>(); stack.push(a); while(!stack.isEmpty()) { Node1 nd=stack.pop(); list.add(nd); if(nd.rigth!=null) { stack.push(nd.rigth); } if(nd.left!=null) { stack.push(nd.left); } } for(int i=0;i<list.size();i++) { System.out.print(list.get(i).val+" "); } } private static void perOrderRecur(Node1 a) { if(a==null) { return ; } System.out.print(a.val+" "); perOrderRecur(a.left); perOrderRecur(a.rigth); }
中序的递归/非递归遍历代码为:
public static void main(String[] arg) { Node1 a = new Node1('A'); a.left=new Node1('B'); a.rigth=new Node1('C'); a.left.left=new Node1('D'); a.left.rigth=new Node1('E'); a.rigth.left=new Node1('F'); a.rigth.rigth=new Node1('G'); System.out.println("中序递归输出:"); inOrderRecur(a);//递归输出 System.out.println("\n"+"中序非递归输出:"); inOrderRecurs(a);//递归输出 }
private static void inOrderRecurs(Node1 a) { if(a==null) { return ; } ArrayList<Node1> list=new ArrayList<Node1>(); Stack<Node1> stack=new Stack<Node1>(); while(!stack.isEmpty()||a!=null) { if(a!=null) { stack.push(a); a=a.left; }else { Node1 nd=stack.pop(); list.add(nd); a=nd.rigth; } } for(int i=0;i<list.size();i++) { System.out.print(list.get(i).val+" "); } } private static void inOrderRecur(Node1 a) { if(a==null) { return ; } inOrderRecur(a.left); System.out.print(a.val+" "); inOrderRecur(a.rigth); }
后序的递归/非递归遍历代码为:
public static void main(String[] arg) { Node1 a = new Node1('A'); a.left=new Node1('B'); a.rigth=new Node1('C'); a.left.left=new Node1('D'); a.left.rigth=new Node1('E'); a.rigth.left=new Node1('F'); a.rigth.rigth=new Node1('G'); System.out.println("后序递归输出:"); posOrderRecur(a);//递归输出 System.out.println("\n"+"后序非递归输出:"); posOrderRecurs(a);//递归输出 } private static void posOrderRecurs(Node1 a) { if(a==null) { return ; } Stack<Node1> stack1=new Stack<Node1>(); stack1.push(a); Stack<Node1> stack2=new Stack<Node1>(); while(!stack1.isEmpty()) { Node1 nd=stack1.pop(); stack2.push(nd); if(nd.left!=null) { stack1.push(nd.left); } if(nd.rigth!=null) { stack1.push(nd.rigth); } } while(!stack2.isEmpty()) { System.out.print(stack2.pop().val+" "); } } private static void posOrderRecur(Node1 a) { if(a==null) { return ; } posOrderRecur(a.left); posOrderRecur(a.rigth); System.out.print(a.val+" "); }
层次的非递归遍历代码为:
public static void main(String[] arg) { Node a = new Node('A'); a.left=new Node('B'); a.rigth=new Node('C'); a.left.left=new Node('D'); a.left.rigth=new Node('E'); a.rigth.left=new Node('F'); a.rigth.rigth=new Node('G'); System.out.println("层次遍历非递归为:"); levelRecur(a); } private static void levelRecur(Node a) { if(a==null) { return; } Queue<Node> queue=new LinkedList<Node>(); queue.offer(a); while(!queue.isEmpty()) { Node nd=queue.poll(); System.out.print(nd.val+" "); if(nd.left!=null) { queue.offer(nd.left); } if(nd.rigth!=null) { queue.offer(nd.rigth); } } }
相关文章推荐
- 二叉树的递归与非递归遍历(Java描述)
- Java实现二叉树的递归、非递归遍历
- 二叉树的递归与非递归遍历(Java描述)
- Java实现二叉树的创建、递归/非递归遍历
- Java实现二叉树的递归与非递归遍历
- 二叉树的递归和非递归遍历(java)
- 二叉树3种递归和非递归遍历(Java)
- 二叉树的四种遍历 (六个方法递归 非递归都有 包含二叉树的创建java方法)
- Java实现二叉树的创建、递归/非递归遍历
- java实现二叉树的前中后遍历(递归和非递归)
- 二叉树的遍历 递归非递归 思路和 java实现
- 二叉树非递归遍历Java实现
- 【数据结构与算法】二叉树递归与非递归遍历(附完整源码)(转)
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- java 二叉树(二)递归方式创建二叉树
- Java实现非递归遍历二叉树
- 二叉树递归与非递归遍历(附完整源码)
- java二叉树的非递归遍历
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- 二叉树的三种遍历的递归和非递归的总结(详细,有推导)java