1 Public classBintree {2 Private Chardate; 3 PrivateBintree Lchild; 4 PrivateBintree rchild; 5 6 PublicBintree (Charc) {7Date =C; 8 } 9 Ten //first-order traversal recursion One Public Static voidPreorder (Bintree t) { A if(T = =NULL) { - return; - } theSystem. out. Print (t.date); - preorder (t.lchild); - preorder (t.rchild); - } + - //Middle Sequence traversal recursion + Public Static voidinorder (Bintree t) { A if(T = =NULL) { at return; - } - inorder (T.lchild); -System. out. Print (t.date); - inorder (T.rchild); - } in - //Post- Iteration traversal recursion to Public Static voidpostorder (Bintree t) { + if(T = =NULL) { - return; the } * Postorder (T.lchild); $ Postorder (T.rchild); Panax NotoginsengSystem. out. Print (t.date); - } the + //First Order traversal non-recursive A Public Static voidPreOrder2 (Bintree t) { thestack<bintree> s =NewStack<bintree>(); + while(t! =NULL|| !S.empty ()) { - while(t! =NULL) { $System. out. Print (t.date); $ S.push (t); -t =T.lchild; - } the if(!S.empty ()) { -t =S.pop (); Wuyit =T.rchild; the } - } Wu } - About //Middle Sequence traversal non-recursive $ Public Static voidInOrder2 (Bintree t) { -stack<bintree> s =NewStack<bintree>(); - while(t! =NULL|| !S.empty ()) { - while(t! =NULL) { A S.push (t); +t =T.lchild; the } - if(!S.empty ()) { $t =S.pop (); theSystem. out. Print (t.date); thet =T.rchild; the } the } - } in the //Post -Iteration traversal non-recursive the Public Static voidPostOrder2 (Bintree t) { Aboutstack<bintree> s =NewStack<bintree>(); thestack<integer> s2 =NewStack<integer>(); theInteger i =NewInteger (1); the while(t! =NULL|| !S.empty ()) { + while(t! =NULL) { - S.push (t); theS2.push (NewInteger (0)); Bayit =T.lchild; the } the while(!s.empty () &&S2.peek (). Equals (i)) { - S2.pop (); -System. out. Print (S.pop (). date); the } the the if(!S.empty ()) { the S2.pop (); -S2.push (NewInteger (1)); thet =S.peek (); thet =T.rchild; the } 94 } the } the
Traversal of a binary tree (recursive, non-recursive)