Several Traversal Algorithms of the c ++ Binary Tree

Source: Internet
Author: User

1. pre-order/middle-order/post-order traversal (Recursive Implementation)

Copy codeThe Code is as follows: // pre-order traversal
Void BT_PreOrder (BiTreePtr pNode ){
If (! PNode) return;
Visit (pNode );
BT_PreOrder (pNode-> left );
BT_PreOrder (pNode-> right );}
// Sequential Traversal
Void BT_PreOrder (BiTreePtr pNode ){
If (! PNode) return;
BT_PreOrder (pNode-> left );
Visit (pNode );
BT_PreOrder (pNode-> right );}
// Post-order traversal void BT_PreOrder (BiTreePtr pNode ){
If (! PNode) return;
BT_PreOrder (pNode-> left );
BT_PreOrder (pNode-> right );
Visit (pNode );}

2. pre-order traversal (non-Recursive Implementation)Copy codeThe Code is as follows: // stack implementation
Void BT_PreOrderNoRec1 (BiTreePtr pNode ){
Stack <BiTreePtr> s;
While (! PNode |! S. empty ())
{
If (! PNode)
{
Visit (pNode );
S. push (pNode );
PNode = pNode-> left;
}
Else
{
PNode = s. pop ();
PNode = pNode-> right;
}
}
}
// Stack implementation
Void BT_PreOrderNoRec2 (BiTreePtr pNode ){
If (! PNode)
{
Stack <BiTreePtr> s;
S. push (pNode );
While (! S. empty ())
{
BiTreePtr pvNode = s. pop ();
Visit (pvNode );
S. push (pvNode-> right );
S. push (pvNode-> left );
}
}}
//
No stack is required. Each node contains the parent node pointer and isVisited [false by default] status variable, and the binary tree contains a header node.
Void BT_PreOrderNoRec3 (BiTreePtr pNode ){
While (! PNode)
// Go back To the header node pointing to the root node and exit.
{
If (! PNode-> bVisited)
// Determine whether the object has been accessed
{
Visit (pNode );
PNode-> isVisited = true;
}
If (pNode-> left &&! PNode-> left-> isVisited)
PNode = pNode-> left;
Else if (pNode-> right &&! PNode-> right-> isVisited)
PNode = pNode-> right;
Else
// Backtracking
PNode = pNode-> parent;
}}

3. Sequential traversal (non-Recursive Implementation)

Copy codeThe Code is as follows: // stack implementation
Void BT_InOrderNoRec1 (BiTreePtr pNode ){
Stack <BiTreePtr> s;
While (! PNode |! S. empty ())
{
If (! PNode)
{
S. push (pNode );
PNode = pNode-> left;
}
Else
{
PNode = s. pop ();
Visit (pNode );
PNode = pNode-> right;
}
}}
// No stack is required. Each node includes the parent node pointer and isVisited [false by default] status variable, and the binary tree contains a header node.
Void BT_InOrderNoRec2 (BiTreePtr pNode ){
While (! PNode)
// Go back To the header node pointing to the root node and exit.
{
While (pNode-> left &&! PNode-> left-> isVisited)
PNode = pNode-> left;
If (! PNode-> isVisited)
{
Visit (pNode );
PNode-> isVisited = true;
}
If (pNode-> right &&! PNode-> right-> isVisited)
PNode = pNode-> right;
Else
PNode = pNode-> parent;
}}

4. Post-order traversal (non-Recursive Implementation)Copy codeCode: void BT_PostOrderNoRec (BiTreePtr pNode ){
If (! PNode) return;
Stack <BiTreePtr> s;
S. push (pNode );
While (! S. empty ())
{
BiTreePtr pvNode = s. pop ();
If (pvNode-> isPushed)
// Indicates that both left and right sub-trees have been written into the stack to access the node
Visit (pvNode );
Else
{
If (pvNode-> right)
{
PvNode-> right-> isPushed = false;
S. push (pvNode-> right );
}
If (pvNode-> left)
{
PvNode-> left-> isPushed = false;
S. push (pvNode-> left );
}
PvNode-> isPushed = true;
S. push (pvNode );
}
}}

5. Sequence traversal (using queues)

Copy codeCode: void BT_LevelOrder (BiTreePtr pNode ){
If (! PNode) return;
Queue <BiTreePtr> q;
Q. push (pNode );
BiTreePtr pvNode;
While (! Q. empty ())
{
PvNode = q. pop ();
Visit (pvNode );
If (pvNode-> left)
Q. push (pvNode-> left );
If (pvNode-> right)
Q. push (pvNode-> right );
}}

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.