JAVA 先序、中序、後序、層序,遞迴非遞迴遍歷二叉樹
定義一個二叉樹 package com.niuke.binaryTree;
public class binaryTree { int data; binaryTree left; binaryTree right; public binaryTree(int data){ this.data=data; left=null; right=null; } //插入節點 public void insert(binaryTree root,int data){ if(data>=root.data){ if(root.rightnull){ root.right=new binaryTree(data); }else { insert(root.right,data); } } else { if(root.left
}
}
具體排序演算法 package com.niuke.binaryTree;
import java.util.*;
public class binaryTreeOrder {
public static void binaryTreePre_1(binaryTree root){ //先序遞迴遍歷 if(root!=null){ System.out.print(root.data+" "); binaryTreePre_1(root.left); binaryTreePre_1(root.right); } } public static void binaryTreeMid_1(binaryTree root){ //中序遞迴遍歷 if(root!=null){ binaryTreePre_1(root.left); System.out.print(root.data+" "); binaryTreePre_1(root.right); } } public static void binaryTreePost_1(binaryTree root){ //後序遞迴遍歷 if(root!=null){ binaryTreePost_1(root.left); binaryTreePost_1(root.right); System.out.print(root.data+" "); } } public static void binaryTreePre_2(binaryTree root){ //非遞迴先序遍歷二叉樹 //此種方法僅適用於此處,不具備擴充套件性,先將根節點入棧,再取出,再先入右再入左 // (棧先入後出)所以每次先取出左子樹,只有當左子樹不為空時出取出右子樹 if(root==null){ return; } Stack<binaryTree> s=new Stack<binaryTree>(); s.push(root); while(!s.isEmpty()){ binaryTree r2=s.pop(); System.out.print(r2.data+" "); if(r2.right!=null){ s.push(r2.right); } if(r2.left!=null){ s.push(r2.left); } } } public static void binaryTreePre_3(binaryTree root){ /* * 這種方法具備擴充套件性,可以在後續的中序遍歷和後序遍歷當中用到,模擬遍歷過程 * 先處理左子樹,當其左子樹為空時,將其取出,處理右子樹。 * */ if(root==null){ return; } Stack<binaryTree> s=new Stack<binaryTree>(); while(root!=null || !s.isEmpty()){ while(root!=null){ System.out.print(root.data+" "); s.push(root); root=root.left;//處理左子樹 } binaryTree r=s.pop();//左子樹已為空,處理右子樹 root=r.right; } } public static void binaryTreeMid_2(binaryTree root){ //中序非遞迴遍歷 if(root==null){ return; } Stack<binaryTree> s=new Stack<binaryTree>(); while (root!=null || !s.isEmpty()){ while (root!=null){ s.push(root); root=root.left; } root=s.pop(); System.out.print(root.data+" "); root=root.right; } } public static void binaryTreePost_2(binaryTree root){ //後序非遞迴遍歷 if(root==null){ return; } Stack<binaryTree> s=new Stack<binaryTree>(); Map<binaryTree,Boolean> map=new HashMap<binaryTree,Boolean>(); s.push(root); while(!s.isEmpty()){ binaryTree temp=s.peek(); if(temp.left!=null && !map.containsKey(temp.left)){ temp=temp.left; while(temp!=null){ if(map.containsKey(temp)) { break; } else { s.push(temp); } temp=temp.left; } continue; } if(temp.right!=null && !map.containsKey(temp.right)){ s.push(temp.right); continue; } binaryTree t=s.pop(); map.put(t,true); System.out.print(t.data+" "); } } public static void levelTravelTree(binaryTree root){ //層序遍歷二叉樹 //序遍歷二叉樹,用佇列實現,先將根節點入佇列,只要佇列不為空 // 然後出佇列,並訪問,接著講訪問節點的左右子樹依次入佇列 if(root==null){ return; } Queue<binaryTree> queue=new LinkedList<binaryTree>(); queue.add(root); while(!queue.isEmpty()){ binaryTree b=queue.poll(); System.out.print(b.data+" "); if(b.left!=null){ queue.add(b.left); } if(b.right!=null){ queue.add(b.right); } } }
}
測試 package com.niuke.binaryTree;
public class binaryTreeTest { public static void main(String[] args) { int[] arr=new int[]{7,1,15,5,6,10,25}; binaryTree t=new binaryTree(arr[0]); for(int i=1;i<arr.length;i++){ t.insert(t,arr[i]); } //先序遞迴遍歷 binaryTreeOrder.binaryTreePre_1(t); System.out.println(); //中序遞迴遍歷 binaryTreeOrder.binaryTreeMid_1(t); System.out.println(); //後序遞迴遍歷 binaryTreeOrder.binaryTreePost_1(t); System.out.println(); //先序非遞迴遍歷1 binaryTreeOrder.binaryTreePre_2(t); System.out.println(); //先序非遞迴遍歷2 binaryTreeOrder.binaryTreePre_3(t); System.out.println(); //中序非遞迴遍歷1 binaryTreeOrder.binaryTreeMid_2(t); System.out.println(); //後序非遞迴遍歷1 binaryTreeOrder.binaryTreePost_2(t); System.out.println(); //層序遍歷二叉樹 binaryTreeOrder.levelTravelTree(t); }
}