Loading...

【数据结构】Java实现二叉树遍历(非递归)

以二叉排序树来建立的一棵二叉树,然后用先序,中序,后序非递归遍历

package DataStructure;

import java.util.Stack;  

public class BinaryTree {   

    private Node root;

    class Node{
        int data;
        Node left;
        Node right;
        public Node(int data){
            this.data = data;
            this.left = null;
            this.right= null;
           }
       }
       //构建一棵二叉树
       public BinaryTree(){
           root = null;
       }

       //二叉树添加节点
       public void buildTree(Node node,int data){
           if(root == null){
               root = new Node(data);
           }else{
               if(data>node.data){
                   if(node.right==null){
                       node.right=new Node(data);
                   }else{
                       buildTree(node.right,data);
                   }
               }else{
                   if(node.left==null){
                       node.left=new Node(data);
                   }else{
                       buildTree(node.left,data);
                   }
               }
           }
       }

    //前序遍历的非递归实现  
    public void PreOrder(Node n){  
        Stack<Node> stack=new Stack<Node>();  
        Node node=n;  
        while(node!=null||stack.size()>0){  
            while(node!=null){  
                visted(node);  
                stack.push(node);  
                node=node.left;  
            }   
                node=stack.pop();  
                node=node.right;    
        }  
    }      
    //中序遍历的非递归实现  
    public void InOrder(Node n){  
        Stack<Node> stack =new Stack<Node>();  
        Node node =n;  
        while(node!=null||stack.size()>0){  
            //存在左子树  
            while(node!=null){  
                stack.push(node);  
                node=node.left;  
            }  
            //栈非空  
            if(stack.size()>0){  
                node=stack.pop();  
                visted(node);  
                node=node.right;  
            }  
        }  
    }        
  //  后序遍历的非递归实现  
    public void PostOrder(Node p){  
        Stack<Node> stack=new Stack<Node>();  
        Node node =p;  
        while(p!=null){  
            for(;p.left!=null;p=p.left){  
                stack.push(p);  
            }  
            while(p!=null&&(p.right==null||p.right==node)){  
                visted(p);    
                node =p;  
                if(stack.empty())  
                    return;  
                p=stack.pop();  
            }  
            stack.push(p);  
            p=p.right;  
        }  
    }  
    public void visted(Node node){  
        System.out.print(" "+node.data);;  
    } 

       public static void main(String[] args){
           int a[]={62,88,58,47,35,73,51,99,37,93};
           BinaryTree bt = new BinaryTree();
           for(int i=0;i<a.length;i++){
               bt.buildTree(bt.root, a[i]);
           }
           System.out.println("先序遍历结果:");
           bt.PreOrder(bt.root);
           System.out.println();
           System.out.println("中序遍历结果:");
           bt.InOrder(bt.root);
           System.out.println();
           System.out.println("后序遍历结果:");
           bt.PostOrder(bt.root);
           System.out.println();
       }
    }    

这里写图片描述

posted @ 2015-12-12 00:32  严康  阅读(352)  评论(0编辑  收藏  举报