1. 程式人生 > >二叉樹的遍歷演算法Java實現

二叉樹的遍歷演算法Java實現

學習並總結了二叉樹的遞迴和非遞迴的遍歷方式~

下面是自己邊敲邊學的程式碼。

package tree;
import java.util.LinkedList;
public class Binaryorder {

    private TreeNode root; //根節點
    public Binaryorder()
    {

    }
    public Binaryorder(TreeNode root)
    {
        this.root=root;
    }
    public TreeNode getRoot()
    {
        return
root; } public void setRoot(TreeNode root) { this.root=root; }
/*
     * 遞迴 前序遍歷
     */
    public void preOrder(TreeNode node)
    {
        if(node!=null)
        {
            System.out.print(node.getdata()+" ");
            preOrder(node.getleft());
            preOrder(node.getright());
        }
    }
    /*
     * 遞迴 中序遍歷
     */
public void inOrder(TreeNode node) { if(node!=null) { inOrder(node.getleft()); System.out.println(node.getdata()+" "); inOrder(node.getright()); } } /* * 遞迴 後序遍歷 */ public void postOrder(TreeNode node) { if
(node!=null) { postOrder(node.getleft()); postOrder(node.getright()); System.out.println(node.getdata()+" "); } }
/*
     * 非遞迴 前序遍歷
     */
    public void preOrdernorec(TreeNode root)
    {
        LinkedList<TreeNode> list= new LinkedList<TreeNode>();
        list.push(root);
        TreeNode cur=null;
        while(!list.isEmpty())
        {
            cur=list.poll();  //!!!
            System.out.print(cur.getdata()+" ");
            if(cur.getright()!=null)
                list.push(cur.getright()); //!!!
            if(cur.getleft()!=null)
                list.push(cur.getleft()); //這裡使用push就是當做棧在使用,如果換成add就不是這樣的輸出
        }

    }
/*
     * 非遞迴 中序遍歷
     */
    public void inordernocur(TreeNode root)
    {
        LinkedList<TreeNode> list= new LinkedList<TreeNode>();
        TreeNode cur=root;
        while(cur!=null||! list.isEmpty())
        {
            while(cur!=null)
            {
                list.push(cur);
                cur=cur.getleft();
            }
            if(!list.isEmpty())
            {
                cur=list.pop();
                System.out.println(cur.getdata()+" ");
                cur=cur.getright();
            }
        }
    }
    /*
     * 非遞迴 後序遍歷  單棧法
     */
    public void postordernocur(TreeNode root)
    {
        Stack<TreeNode> stack= new Stack<TreeNode>();
        TreeNode cur=root;
        TreeNode node=root;
        while(cur!=null||!stack.isEmpty())
        {
            while(cur!=null)
            {
                stack.push(cur);
                cur=cur.getleft();  
            }
            if(stack.size()>0)
            {
                TreeNode temp=stack.peek().getright();

                if(temp==null||temp==node)//注意第二個條件
                {
                    cur=stack.pop();
                    System.out.print(cur.getdata()+" ");
                    node=cur;
                    cur=null;
                }
                else
                    cur=temp;

            }
        }
    }

/*
     * 雙棧法
     */
    public void postordernocur2(TreeNode root)
    {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        Stack<TreeNode> temp = new Stack<TreeNode>();
        TreeNode node=root;
        while(node!=null||stack.size()>0)
        {
            while(node!=null)
            {
                temp.push(node);
                stack.push(node);
                node=node.getright();
            }
            if(stack.size()>0)
            {
                node=stack.pop();
                node=node.getleft();
            }
        }
        while(temp.size()>0)
        {
            node=temp.pop();
            System.out.print(node.getdata()+" ");
        }
    }
//節點類
      class TreeNode
    {
        private String data=null;//資料部分
        private TreeNode left;
        private TreeNode right;
        public TreeNode(String data,TreeNode left,TreeNode right)
        {
            this.data=data;
            this.left=left;
            this.right=right;
        }
        public String getdata()
        {
            return data;
        }
        public void setdata(String data)
        {
            this.data=data;
        }
        public TreeNode getleft()
        {
            return left;
        }
        public void setleft(TreeNode left)
        {
            this.left=left;
        }
        public TreeNode getright()
        {
            return right;
        }
        public void setright(TreeNode right)
        {
            this.right=right;
        }
    }
//主函式
public static void main(String[] args) {

        Binaryorder b =new Binaryorder();
        //構造二叉樹
        TreeNode l2=new TreeNode("E",null,null);
        TreeNode r2=new TreeNode("D",null,null);
        TreeNode l1=new TreeNode("B",null,r2);
        TreeNode r1=new TreeNode("C",l2,null);
        TreeNode root=new TreeNode("A",l1,r1);

        b.postOrder(root);
        System.out.println("----");
        b.postordernocur(root);
  }
}