二叉樹的先序、中序、後序、層序遍歷的遞迴實現與非遞迴實現
阿新 • • 發佈:2019-01-05
1.總結深度優先與廣度優先的區別
1) 二叉樹的深度優先遍歷的非遞迴的通用做法是採用棧,廣度優先遍歷的非遞迴的通用做法是採用佇列。
2) 深度優先遍歷:對每一個可能的分支路徑深入到不能再深入為止,而且每個結點只能訪問一次。要特別注意的是,二叉樹的深度優先遍歷比較特殊,可以細分為先序遍歷、中序遍歷、後序遍歷。具體說明如下:
先序遍歷:對任一子樹,先訪問根,然後遍歷其左子樹,最後遍歷其右子樹。
中序遍歷:對任一子樹,先遍歷其左子樹,然後訪問根,最後遍歷其右子樹。
後序遍歷:對任一子樹,先遍歷其左子樹,然後遍歷其右子樹,最後訪問根。
廣度優先遍歷:又叫層次遍歷,從上往下對每一層依次訪問,在每一層中,從左往右(也可以從右往左)訪問結點,訪問完一層就進入下一層,直到沒有結點可以訪問為止。
3)深度優先搜素演算法:不全部保留結點,佔用空間少;有回溯操作(即有入棧、出棧操作),執行速度慢。
廣度優先搜尋演算法:保留全部結點,佔用空間大; 無回溯操作(即無入棧、出棧操作),執行速度快。
通常 深度優先搜尋法不全部保留結點,擴充套件完的結點從資料庫中彈出刪去,這樣,一般在資料庫中儲存的結點數就是深度值,因此它佔用空間較少。
所以,當搜尋樹的結點較多,用其它方法易產生記憶體溢位時,深度優先搜尋不失為一種有效的求解方法。
廣度優先搜尋演算法,一般需儲存產生的所有結點,佔用的儲存空間要比深度優先搜尋大得多,因此,程式設計中,必須考慮溢位和節省記憶體空間的問題。
但廣度優先搜尋法一般無回溯操作,即入棧和出棧的操作,所以執行速度比深度優先搜尋要快些
2.二叉樹的先序、中序、後序、層序遍歷的遞迴實現與非遞迴實現
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Stack; public class BinTree { private BinTree lChild; private BinTree rChild; private BinTree root; private Object data; private List<BinTree> datas; public BinTree(BinTree lChild, BinTree rChild, Object data) { super(); this.lChild = lChild; this.rChild = rChild; this.data = data; } public BinTree(Object data) { this(null,null,data); } public BinTree(){ super(); } //建立二叉樹 public void createTree(Object[] objs){ datas=new ArrayList<BinTree>(); for (Object object:objs) { datas.add(new BinTree(object)); } root=datas.get(0); for (int i = 0; i < objs.length/2; i++) { datas.get(i).lChild=datas.get(i*2+1); if(i*2+2<datas.size()){ datas.get(i).rChild=datas.get(i*2+2); } } } //先序遍歷非遞迴方法(遞迴實現) public void preorder1(BinTree root){ Stack<BinTree> stack =new Stack<BinTree>(); while(root!=null||!stack.isEmpty()){ while(root!=null){ visit(root.getData()); stack.push(root); root=root.lChild; } if(!stack.isEmpty()){ root=stack.pop(); root=root.rChild; } } } //先序遍歷遞迴方法 public void preorder(BinTree root){ if(root!=null){ visit(root.getData()); preorder(root.lChild); preorder(root.rChild); } } //中序遍歷非遞迴方法(堆疊實現) public void inorder1(BinTree root){ Stack<BinTree> stack=new Stack<BinTree>(); while(root!=null||!stack.isEmpty()){ while(root!=null){ stack.push(root); root=root.lChild; } if(!stack.isEmpty()){ root=stack.pop(); visit(root.getData()); root=root.rChild; } } } //中序遍歷遞迴方法 public void inorder(BinTree root){ if(root!=null){ inorder(root.lChild); visit(root.getData()); inorder(root.rChild); } } //後序遍歷非遞迴方法(堆疊實現) public void postorder1(BinTree root){ Stack<BinTree> stack=new Stack<BinTree>(); Stack<Integer> stack1 = new Stack<Integer>(); int i=1; while(root!=null||!stack.isEmpty()){ while(root!=null){ stack.push(root); stack1.push(0); root=root.lChild; } while(!stack.empty() && stack1.peek() == i) { stack1.pop(); visit(stack.pop().getData()); } if(!stack.empty()) { stack1.pop(); stack1.push(1); root = stack.peek(); root = root.rChild; } } } //後序遍歷遞迴方法(廣度優先遍歷) public void postorder(BinTree root){ if(root!=null){ postorder(root.lChild); postorder(root.rChild); visit(root.data); } } //層序遍歷遞迴方法 public void levelorder(BinTree root,int i) { if(root!=null&&i!=0){ if(i==1){ visit(root.getData()); } levelorder(root.lChild,i-1); levelorder(root.rChild,i-1); } } //遍歷樹的高度 public void height(BinTree root,Object[] objs){ if(root!=null){ //求當前節點的高度 double a=objs.length; int dep= (int) (Math.log(a)/Math.log(2))+2; for (int i = 0; i <=dep; i++) { levelorder(root,i); } } } //層序遍歷非遞迴實現(佇列)(廣度優先遍歷) public void levelorder1(BinTree root) { Queue<BinTree> queue = new LinkedList<BinTree>(); while(root!=null){ visit(root.getData()); if(root.lChild!=null) queue.add(root.lChild); if(root.rChild!=null) queue.add(root.rChild); root=queue.poll(); } } //深度優先遍歷 public void visit(Object obj){ System.out.print(obj+""); } public Object getData(){ return data; } public BinTree getRoot(){ return root; } public static void main(String[] args) { BinTree binTree=new BinTree(); Object[] objs={'a','b','c','d','e','f','g','h'}; binTree.createTree(objs); binTree.postorder(binTree.getRoot()); System.out.println("後序遍歷"); binTree.inorder(binTree.getRoot()); System.out.println("中序遍歷"); binTree.preorder(binTree.getRoot()); System.out.println("先序遍歷"); binTree.height(binTree.getRoot(),objs); System.out.println("層序遍歷(廣度優先遍歷)"); binTree.inorder1(binTree.getRoot()); System.out.println("中序遍歷非遞迴實現"); binTree.preorder1(binTree.getRoot()); System.out.println("先序遍歷非遞迴實現"); binTree.postorder1(binTree.getRoot()); System.out.println("後序遍歷非遞迴實現"); binTree.levelorder1(binTree.getRoot()); System.out.println("層序遍歷非遞迴實現(廣度優先遍歷)"); } }