.如何不用递归实现二叉树的前序/后序/中序遍历?
2013-06-06 17:03
387 查看
import java.lang.Thread.State; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.Stack; public class BinaryTree { private Node root; private Node list; private Node current; public void print(){ if(this.root!=null){ this.root.print(); } } /** 非递归中序遍历 * 思想是:用一个栈,先保存跟节点,然后循环保存的它的左子树, * 接着做出栈操作,pop出的内容的先判断他是否有右子树,如果有右子树 * 做如栈操作,然后接着判断如栈的元素是否有左子树,如果有的话,循环加入栈中 * * 5 * / \ * 3 7 * /\ /\ * 2 4 6 * / * 1 * * stack * * 例子 左子树入栈 然后出站 * |1| 【1】pop * |2| 【2】pop * |3| 【3】pop 注意呀,这个时候就得判断【3】的右子树了 * |5| 因为3有右子树,所以的把它的右子树入栈 栈的结构图如下 * stack * 【4】 * 【5】 * 接着同理了 */ public void midPint(){ Stack<Node> stack=new Stack<>(); Node temp=root; if(temp!=null){ stack.push(temp); } while(temp.left!=null){ stack.push(temp.left); temp=temp.left; } while(stack.size()>0){ temp=stack.pop(); System.out.println(temp.data); if(temp.right!=null){ temp=temp.right; stack.push(temp); while(temp!=null){ if(temp.left!=null){ stack.push(temp.left); } temp=temp.left; } } } } public List<Node> breadFirst(){ List<Node> list=new ArrayList<Node>(); Queue<Node> queue=new ArrayDeque<Node>(); if(root!=null){ queue.add(root); } while(!queue.isEmpty()){ list.add(queue.peek()); Node node=queue.poll(); if(node.left!=null){ queue.offer(node.left); } if(node.right!=null){ queue.offer(node.right); } } return list; } public List<Node> breadFirst2(){ List<Node> list=new ArrayList<>(); Queue<Node> queue=new ArrayDeque<>(); if(root!=null){ queue.offer(root); } while(!queue.isEmpty()){ list.add(queue.peek()); Node curren=queue.poll(); if(curren.left!=null){ queue.offer(curren.left); } if(curren.right!=null){ queue.offer(curren.right); } } return list; } /** 5 镜像后 5 * / \ / \ * 3 7 7 3 * /\ /\ /\ / * 2 4 6 4 2 6 * / / \ * 1 1 * @param root */ public static void preOrder(Node root){ if(root==null){ return; } Node temp=root.left; root.left=root.right; root.right=temp; preOrder(root.left); preOrder(root.right); } public List<Node> midPrint(Node node){ List<Node> list=new ArrayList<Node>(); if(node.left!=null){ list.addAll(midPrint(node.left)); } list.add(node); if(node.right!=null){ list.addAll(midPrint(node.right)); } return list; } public void converList(){ if(this.root!=null){ this.root.convertList(); } } public void addList(Node node){ if(list==null){ System.out.println("cao"+node.data); list=node; current=node; }else{ current.right=node; node.left=current; current=node; } } public void add(int data){ if(root==null){ root=new Node(data, null, null); }else{ Node parent = null; Node current=root; while(current!=null){ parent=current; if(current.data>data){ current=current.left; }else{ current=current.right; } } if(parent.data>data){ Node node=new Node(data, null, null); parent.left=node; }else{ Node node=new Node(data, null, null); parent.right=node; } } } class Node{ int data; Node left; Node right; public Node(int data,Node left,Node right){ this.data=data; this.left=left; this.right=right; } /** * 二差数转化成有序的链表 */ public void convertList(){ if(this.left!=null){ this.left.convertList(); } addList(this); // System.out.println(this.data); if(this.right!=null){ this.right.convertList(); } } public void print(){ if(this.left!=null){ this.left.print(); } System.out.println(this.data); if(this.right!=null){ this.right.print(); } } @Override public String toString() { // TODO Auto-generated method stub return ""+this.data; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BinaryTree bt = new BinaryTree(); bt.add(5); bt.add(7); bt.add(3); bt.add(2); bt.add(1); bt.add(6); bt.add(4); System.out.println("************************"); bt.midPint(); // bt.preOrder(bt.root); System.out.println("************************"); // System.out.println(bt.breadFipreOrderrst()); bt.converList(); System.out.println("head"+bt.list.data); Node current1=bt.list; while(current1!=null){ System.out.println(current1.data); current1=current1.right; } /* List<Node> list=bt.breadFirst2(); for(Node node:list){ System.out.println(node.data); } */ } }
相关文章推荐
- C++实现二叉树 前序遍历, 后序遍历, 中序遍历, 层序遍历(不用递归)
- 二叉树的中序遍历、递归实现、非递归实现、层次遍历、二叉树的应用,来来来,都有都有
- 先序遍历、中序遍历二叉树非递归实现
- 试编写一个函数,返回一颗给定二叉树在中序遍历下的最后一个节点(分别用递归和非递归实现)
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树的中序遍历(非递归实现)
- 二叉树经典面试题解析一:用非递归实现二叉树的前序遍历,中序遍历,后序遍历
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树的中序遍历,递归实现与循环实现
- leetcode | 二叉树的前序遍历、中序遍历、后续遍历的非递归实现
- Binary Tree Inorder Traversal-非递归实现中序遍历二叉树
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树基础之前序遍历、中序遍历、后序遍历的递归和非递归实现
- 14.二叉树 中序遍历 先序遍历 的非递归实现 以及 二叉树 的复制 及判断二叉树的等价性
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 不使用递归和栈实现二叉树的中序遍历
- 二叉树的递归非递归实现的中序遍历
- 面试题 二叉树的前序遍历,中序遍历,后序遍历(递归实现)