Forward traversal Binary Tree forward traversal Binary Tree backward traversal Binary Tree C ++ STL Template

Source: Internet
Author: User
// Preordered travel tree or its subtree template <class T> void binarytree <t>: Preorder (binarytreenode <t> * root) {stack <binarytreenode <t> *> astack; binarytreenode <t> * pointer = root; while (! Astack. empty () | pointer) {If (pointer) {// access the current node visit (pointer); // access the current node address to the stack astack. push (pointer); // The current link structure points to the left child pointer = pointer-> leftchild ();} else {// The left subtree has been accessed, turn to access the right subtree pointer = astack. top (); astack. pop (); // stack top element rollback // The current link structure points to the right child pointer = pointer-> rightchild ();}} // end while} // The middle-order travel binary tree or its sub-tree template <class T> void binarytree <t>: inorder (binarytreenode <t> * root) {stack <binarytreenode <t> *> astack; binarytreenode <t> * Poin TER = root; while (! Astack. empty () | pointer) {If (pointer) {// the current node address is added to the stack astack. push (pointer); // The current link structure points to the left child pointer = pointer-> leftchild ();} // end ifelse {// The left subtree has been accessed, turn to access the right subtree pointer = astack. top (); astack. pop (); // stack top element rollback visit (pointer); // access the current node // The current link structure points to the right child pointer = pointer-> rightchild ();} // end else} // end while} // The back-order travel binary tree or its subtree template <class T> void binarytree <t >:: postorder (binarytreenode <t> * root) {// use the STL stack part Using STD: Stack; stackeleme NT <t> element; // stack declaration stack <stackelement <t> astack; binarytreenode <t> * pointer; If (root = NULL) return; // empty tree returns // else pointer = root; while (true) {// enter the left subtree while (pointer! = NULL) {element. pointer = pointer; element. tag = left; astack. push (element); // travel down the left subtree to pointer = pointer-> leftchild ();} // push out the top element of the stack = astack. top (); astack. pop (); pointer = element. pointer; // return from the right subtree while (element. tag = right) {visit (pointer); // access the current node if (astack. empty () return; // else {element = astack. top (); astack. pop (); // poll the stack pointer = element. pointer; //} // end else} // end while // element returned from the left subtree. tag = right; astack. push (element); // redirect to access the right subtree pointer = pointer-> rightchild ();} // end while}

reference (Beijing University instructor Zhang Ming ppt)

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.