二叉树的各种遍历 递归和非递归

  1 import java.util.Scanner;
  2 
  3 class Read{
  4     String s;
  5     Read(String a){
  6         s = a;
  7     }
  8     int pos = 0;
  9     char ReadChar(){
 10         pos++;
 11         return s.charAt(pos-1);
 12     }
 13 }
 14 class queue{
 15     Tree[] arr = new Tree[1000];
 16     int f = 0;
 17     int r = 0;
 18     int empty(){
 19         return f==r?1:0;
 20     }
 21     Tree front(){
 22         return arr[f];
 23     }
 24     void pop(){
 25         f++;
 26     }
 27     void push(Tree t){
 28         arr[r++] = t;
 29     }
 30 }
 31 class stack{
 32     Tree[] arr = new Tree[1000];
 33     int t = 0;
 34     int empty(){
 35         return t==0?1:0;
 36     }
 37     Tree top(){
 38         return arr[t-1];
 39     }
 40     void pop(){
 41         t--;
 42     }
 43     void push(Tree a){
 44         arr[t++]=a;
 45     }
 46 }
 47 class Tree{
 48     char data;
 49     Tree lchild,rchild;
 50     int nc = 0;
 51     static Tree build(Read s){
 52         char c = s.ReadChar();
 53         if(c=='#')
 54             return null;
 55         Tree t = new Tree();
 56         t.data = c;
 57         t.lchild = build(s);
 58         t.rchild = build(s);
 59         return t;
 60     }
 61     void qian(){
 62         System.out.print(data);
 63         if(lchild!=null)
 64             lchild.qian();
 65         if(rchild!=null)
 66             rchild.qian();
 67     }
 68     void zhong(){
 69         if(lchild!=null)
 70             lchild.zhong();
 71         System.out.print(data);
 72         if(rchild!=null)
 73             rchild.zhong();
 74     }
 75     void hou(){
 76         if(lchild!=null)
 77             lchild.hou();
 78         if(rchild!=null)
 79             rchild.hou();
 80         System.out.print(data);
 81     }
 82     void ceng(){
 83         queue que = new queue();
 84         que.push(this);
 85         while(que.empty()==0){
 86             Tree temp = que.front();
 87             que.pop();
 88             System.out.print(temp.data);
 89             if(temp.lchild!=null)
 90                 que.push(temp.lchild);
 91             if(temp.rchild!=null)
 92                 que.push(temp.rchild);
 93         }
 94     }
 95 
 96     void qqian(){
 97         stack st = new stack();
 98         Tree t = this;
 99         while(t!=null || st.empty()==0){
100             if(t!=null)
101             {
102                 System.out.print(t.data);
103                 st.push(t);
104                 t = t.lchild;
105             }
106             else
107             {
108                  t = st.top();
109                  st.pop();
110                  t = t.rchild;
111             }
112         }
113     }
114     void zzhong(){
115         stack st = new stack();
116         Tree t = this;
117         while(t!=null || st.empty()==0){
118             if(t!=null)
119             {
120                 st.push(t);
121                 t = t.lchild;
122             }
123             else
124             {
125                  t = st.top();
126                  st.pop();
127                  System.out.print(t.data);
128                  t = t.rchild;
129             }
130         }
131     }
132     void hhou(){
133         stack st = new stack();
134         Tree t = this;
135         do{
136             if(t!=null && t.nc==0){
137                 t.nc = 1;
138                 st.push(t);
139                 t = t.lchild;
140             }
141             else{
142                 t = st.top();
143                 st.pop();
144                 if(t.nc==1){
145                     t.nc = 2;
146                     st.push(t);
147                     t = t.rchild;
148                 }
149                 else{
150                     System.out.print(t.data);
151                 }
152             }
153         }while(st.empty()==0);
154 
155     }
156 }
157 class Dada{
158     public static void main(String[] args){
159         Scanner sc = new Scanner(System.in);
160         String s = sc.nextLine();
161         Read str = new Read(s);
162         Tree t = Tree.build(str);
163         t.qian();
164         System.out.println();
165         t.zhong();
166         System.out.println();
167         t.hou();
168         System.out.println();
169         t.ceng();
170         System.out.println();
171         t.qqian();
172         System.out.println();
173         t.zzhong();
174         System.out.println();
175         t.hhou();
176         System.out.println();
177     }
178 }
原文地址:https://www.cnblogs.com/Xycdada/p/6600516.html