遞歸實現二叉樹的遍歷
class BinaryTree{
? ? ? ? ?public int value;
? ? ? ? ?public BinaryTree left;
? ? ? ? ?public BinaryTree right;
? ? ? ? ?public BinaryTree(int data){
? ? ? ? ? ? ? ? ? this.value = data;
? ? ? ? ?}
? ? ? ? ?//先序遍歷二叉樹
? ? ? ? ?public void preOrderRecur(BinaryTree head){
? ? ? ? ? ? ? ? ?if(head==null){
? ? ? ? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ?System.out.println(head.value+" ");
? ? ? ? ? ? ? ? ?preOrderRecur(head.left);
? ? ? ? ? ? ? ? preOrderRecur(head.right);
? ? ? ? ?}
? ? ? ? //中序遍歷二叉樹
? ? ? ? public void inOrderRecur(BinaryTree head){
? ? ? ? ? ? ? ? ? ?if(head==null){
? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? inOrderRecur(head.left);
? ? ? ? ? ? ? ? ? System.out.println(head.value+" ");? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? inOrderRecur(head.right);
? ? ? ? ?}
? ? ? ? ?//后序遍歷二叉樹
? ? ? ? ?public void postOrderRecur(BinaryTree head){
? ? ? ? ? ? ? ? ?if(head==null){
? ? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? postOrderRecur(head.left);
? ? ? ? ? ? ? ? postOrderRecur(head.right);
? ? ? ? ? ? ? ? System.out.println(head.value+" ");
? ? ? ?}
}
用遞歸方法實現二叉樹的遍歷,那么也可以非遞歸來實現。遞歸方法是利用棧來保存信息的,棧是具有記憶功能的數據結構。我個人不怎么喜歡用遞歸,建議盡量不要用遞歸來解決問題。因為遞歸涉及到重復計算,還有當數據規模很大時就會導致內存溢出。下面介紹用非遞歸的方法來實現二叉樹遍歷。
1.非遞歸先序遍歷二叉樹
a.申請一個棧stack,將頭結點head壓入stack中;
b.從stack中彈出棧頂節點元素cur,然后打印節點元素的值,再將節點cur的右子樹先壓入stack中,最后將cur的左子樹壓入stack中,前提是左子樹和右子樹不為空
c.不斷重復步驟b,一直到stack為空,全部過程結束
舉例說明(這里要說明的是棧是后進先出,所以先是右子樹進棧,然后再左子樹進棧,這樣就可以使左子樹的元素先打印出來)
第一步,1進棧,彈出來并打印,3進棧,2進棧(棧頂到棧底依次是2,3)
第二步,2彈出并打印,5進棧,4進棧(棧頂到棧底依次是4,5,3)
第三步,4彈出來并打印(棧頂到棧底依次是5,3)
第四步,5彈出來并打印(棧中只有3)
第五步,3彈出來并打印,7進棧,6進棧(棧頂到棧底依次是6,7)
第六步,6彈出并打印
第七步,7彈出并打印
非遞歸的先序遍歷代碼如下:
public void preOrderUnRecur(BinaryTree head){
? ? ? ? System.out.println("非遞歸先序遍歷:");
? ? ? ? if(head!=null){
? ? ? ? ? ? ? Stack<BinaryTree> stack = new Stack<BinaryTree>();
? ? ? ? ? ? ? ?stack.add(head);
? ? ? ? ? ? ? ?while(!stack.isEmpty()){
? ? ? ? ? ? ? ? ? ? ?head = stack.pop();//頭結點元素出棧
? ? ? ? ? ? ? ? ? ? ?System.out.println(head.value+" ");
? ? ? ? ? ? ? ? ? ? ?if(head.right!=null){
? ? ? ? ? ? ? ? ? ? ? ? ? ? stack.push(head.right);
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ?if(head.left!=null){
? ? ? ? ? ? ? ? ? ? ? ? ? ? stack.push(head.left);
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ?System.out.println();
}
就寫到這里了,實驗室要關門了,不往下寫了。非遞歸的關鍵點就是要利用棧的性質,還有就是輸出的結果跟元素進棧的順序是相反的,這里還是棧的性質,非遞歸的中序遍歷和后序遍歷跟這差不多。希望各位前輩同仁指正!謝謝...