Traversal of a binary tree (recursive, non-recursive)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.