.如何不用递归实现二叉树的前序/后序/中序遍历?



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);
        }
*/
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值