二叉树最大深度,递归中序遍历,递归前序遍历,递归后序遍历,非递归中序遍历,非递归前序遍历,非递归后序遍历

二叉树最大深度,前序遍历、中序遍历、后序遍历算法,包含递归与非递归

  1 import java.util.*;
  2 
  3 import javax.swing.plaf.synth.SynthSpinnerUI;
  4 class TreeNode{
  5     int data;
  6     TreeNode lefchild=null;
  7     TreeNode rightchild=null;
  8     TreeNode(int data,TreeNode leftchild,TreeNode rightchild){
  9         this.data=data;
 10         this.lefchild=leftchild;
 11         this.rightchild=rightchild;
 12     }
 13 }
 14 public class BinaryTree {
 15 
 16     public static void main(String[] args) {
 17         // TODO Auto-generated method stub
 18         TreeNode J=new TreeNode(8,null,null);
 19         TreeNode H=new TreeNode(4,null,null);
 20         TreeNode G=new TreeNode(2,null,null);
 21         TreeNode F=new TreeNode(7,null,J);
 22         TreeNode E=new TreeNode(5,H,null);
 23         TreeNode D=new TreeNode(1,null,G);
 24         TreeNode C=new TreeNode(9,F,null);
 25         TreeNode B=new TreeNode(3,D,E);
 26         TreeNode A=new TreeNode(6,B,C);
 27         BinaryTree p=new BinaryTree();
 28         int maxdepth=p.getMaxDepth(A);
 29         System.out.println("最大深度:"+maxdepth);
 30         
 31         System.out.print("递归中序遍历:");
 32         p.inorder(A);
 33         System.out.println();
 34         System.out.print("非递归中序遍历:");
 35         p.inOrder(A);
 36         System.out.println();
 37         
 38         System.out.print("递归前序遍历:");
 39         p.preorder(A);
 40         System.out.println();
 41         System.out.print("非递归前序遍历:");
 42         p.preOrder(A);
 43         System.out.println();
 44         
 45         System.out.print("递归后序遍历:");
 46         p.postorder(A);
 47         System.out.println();
 48         System.out.print("非递归后序遍历:");
 49         p.postOrder(A);
 50         System.out.println();
 51     }
 52     /*获取最大深度*/
 53     public int getMaxDepth(TreeNode root){
 54         if(root==null){
 55             return 0;
 56         }else{
 57             int leftDepth=getMaxDepth(root.lefchild);
 58             int rightDepth=getMaxDepth(root.rightchild);
 59             return 1+Math.max(leftDepth, rightDepth);        }
 60     }
 61     /*递归中序遍历*/
 62     public void inorder(TreeNode root){
 63         if(root==null){
 64             return;
 65         }else{
 66             inorder(root.lefchild);
 67             System.out.print(root.data+" ");
 68             inorder(root.rightchild);
 69         }
 70     }
 71     /*递归前序遍历*/
 72     public void preorder(TreeNode root){
 73         if(root==null){
 74             return;
 75         }else{
 76             System.out.print(root.data+" ");
 77             preorder(root.lefchild);
 78             preorder(root.rightchild);
 79         }
 80     }
 81     /*递归后序遍历*/
 82     public void postorder(TreeNode root){
 83         if(root==null){
 84             return;
 85         }else{
 86             postorder(root.lefchild);
 87             postorder(root.rightchild);
 88             System.out.print(root.data+" ");
 89         }
 90     }
 91     /*非递归中序遍历*/
 92     public void inOrder(TreeNode root){
 93         Stack<TreeNode> stack=new Stack<TreeNode>();
 94         while(root!=null||!stack.isEmpty()){
 95             while(root!=null){
 96                 stack.push(root);
 97                 root=root.lefchild;
 98             }
 99             if(!stack.isEmpty()){
100                 root=stack.pop();
101                 System.out.print(root.data+" ");
102                 root=root.rightchild;
103             }
104         }
105     }
106     /*非递归前序遍历*/
107     public void preOrder(TreeNode root){
108         Stack<TreeNode> stack=new Stack<TreeNode>();
109         while(root!=null||!stack.isEmpty()){
110             while(root!=null){
111                 System.out.print(root.data+" ");
112                 stack.push(root);
113                 root=root.lefchild;
114             }
115             if(!stack.isEmpty()){
116                 root=stack.pop();
117                 root=root.rightchild;
118             }
119         }
120     }
121     /*非递归后序遍历*/
122     public void postOrder(TreeNode root){
123         Stack<TreeNode> stack=new Stack<TreeNode>();
124         TreeNode prev=null;
125         while(root!=null||!stack.isEmpty()){
126             while(root!=null){
127                 stack.push(root);
128                 root=root.lefchild;
129             }
130             if(!stack.isEmpty()){
131                 TreeNode temp=stack.peek().rightchild;
132                 if(temp==null||temp==prev){
133                     root=stack.pop();
134                     prev=root;
135                     System.out.print(root.data+" ");
136                     root=null;
137                 }else{
138                     root=temp;
139                 }
140             }
141         }
142     }
143 }

 

posted @ 2017-05-05 17:10  lulushow  阅读(210)  评论(0编辑  收藏  举报