Binary tree traversal (Pre-and Post-sequence/non-recursion)

Source: Internet
Author: User

I. Pre-and Post-order Recursion
[Cpp]
/*
The Recursive Implementation of the pre-and post-order is the simplest to understand. The main point is the location of visit (node.
*/
/*
Pre-and post-order recursion implementation
*/
// Pre-order traversal
Void BT_PreOrder (BitTree node)
{
If (! Node) return;
 
Visit (node );
BT_PreOrder (node-> left );
BT_PreOrder (node-> right );
}
 
// Sequential Traversal
Void BT_InOrder (BitTree node)
{
If (! Node) return;
 
BT_PreOrder (node-> left );
Visit (node );
BT_PreOrder (node-> right );
}
 
// Sequential Traversal
Void BT_PostOrder (BitTree node)
{
If (! Node) return;
 
BT_PreOrder (node-> left );
BT_PreOrder (node-> right );
Visit (node );
}

/*
The Recursive Implementation of the pre-and post-order is the simplest to understand. The main point is the location of visit (node.
*/
/*
Pre-and post-order recursion implementation
*/
// Pre-order traversal
Void BT_PreOrder (BitTree node)
{
If (! Node) return;

Visit (node );
BT_PreOrder (node-> left );
BT_PreOrder (node-> right );
}

// Sequential Traversal
Void BT_InOrder (BitTree node)
{
If (! Node) return;

BT_PreOrder (node-> left );
Visit (node );
BT_PreOrder (node-> right );
}

// Sequential Traversal
Void BT_PostOrder (BitTree node)
{
If (! Node) return;

BT_PreOrder (node-> left );
BT_PreOrder (node-> right );
Visit (node );
}

 

II. Implementation of sequence Recursion
[Cpp]
*
Sequence Traversal
This method is implemented using queues, which is also the easiest way to understand. The idea is as follows:
According to the definition of sequence traversal, after accessing the current node, the left subnode of the current node has the penultimate priority, and the right subnode of the current node has the penultimate priority, you can achieve this by taking advantage of the first-in-first-out feature of the queue (the lowest priority can be determined.
*/
Void BT_LevelOrder (BitTree node)
{
If (! Node) return;
 
Queue <BitTree> q;
Q. push (node );
 
BitTree pvNode;
While (! Q. empty ())
{
PvNode = q. pop ();
Visit (pvNode );
 
If (! PvNode-> left) q. push (pvNode-> left );
If (! PvNode-> right) q. push (pvNode-> right );
}
}

/*
Sequence Traversal
This method is implemented using queues, which is also the easiest way to understand. The idea is as follows:
According to the definition of sequence traversal, after accessing the current node, the left subnode of the current node has the penultimate priority, and the right subnode of the current node has the penultimate priority, you can achieve this by taking advantage of the first-in-first-out feature of the queue (the lowest priority can be determined.
*/
Void BT_LevelOrder (BitTree node)
{
If (! Node) return;

Queue <BitTree> q;
Q. push (node );

BitTree pvNode;
While (! Q. empty ())
{
PvNode = q. pop ();
Visit (pvNode );

If (! PvNode-> left) q. push (pvNode-> left );
If (! PvNode-> right) q. push (pvNode-> right );
}
}

 

 


Iii. Non-recursive pre-order implementation
[Cpp]
/*
Implement 1 by traversing non-recursion in the forward order
This method is implemented using stacks, which is also the easiest way to understand. The idea is as follows:
According to the definition in the previous traversal, after accessing the current node, the left child node of the current node has the first priority, and the right child node of the current node has the second priority,
You can achieve this by taking advantage of the Post-stack first-in-first-out feature (the highest priority can be determined.
*/
Void BT_PreOrderNoRec (BitTree node)
{
If (! Node) return;
 
Stack <BitTree> s;
BitTree pvNode;
S. push (node );
While (! S. empty ())
{
PvNode = s. pop ();
Visit (pvNode );
 
If (! PvNode-> right) s. push (pvNode-> right );
If (! PvNode-> left) s. push (pvNode-> left );
}
}
 
/*
Implement non-recursive traversal in the forward order 2
I personally think it is not as easy to understand as the first method.
*/
Void BT_PreOrderNoRec2 (BitTree node)
{
If (! Node) return;
 
Stack <BitTree> s;
While (! Node &&! S. empty ())
{
/* If the current node is not empty, access it directly, store the node in the stack (only used to find the right sub-node in the future), and then change the current node to the left-byte node */
If (node)
{
Visit (node );
S. push (node );
Node = node-> left;
}
/* If the current node is empty, extract the previous node from the stack and find the right sub-node for access */
Else
{
Node = s. pop ();
Node = s. right;
}
}
}

/*
Implement 1 by traversing non-recursion in the forward order
This method is implemented using stacks, which is also the easiest way to understand. The idea is as follows:
According to the definition in the previous traversal, after accessing the current node, the left child node of the current node has the first priority, and the right child node of the current node has the second priority,
You can achieve this by taking advantage of the Post-stack first-in-first-out feature (the highest priority can be determined.
*/
Void BT_PreOrderNoRec (BitTree node)
{
If (! Node) return;

Stack <BitTree> s;
BitTree pvNode;
S. push (node );
While (! S. empty ())
{
PvNode = s. pop ();
Visit (pvNode );

If (! PvNode-> right) s. push (pvNode-> right );
If (! PvNode-> left) s. push (pvNode-> left );
}
}

/*
Implement non-recursive traversal in the forward order 2
I personally think it is not as easy to understand as the first method.
*/
Void BT_PreOrderNoRec2 (BitTree node)
{
If (! Node) return;

Stack <BitTree> s;
While (! Node &&! S. empty ())
{
/* If the current node is not empty, access it directly, store the node in the stack (only used to find the right sub-node in the future), and then change the current node to the left-byte node */
If (node)
{
Visit (node );
S. push (node );
Node = node-> left;
}
/* If the current node is empty, extract the previous node from the stack and find the right sub-node for access */
Else
{
Node = s. pop ();
Node = s. right;
}
}
}

 

4. Non-recursion

[Cpp]
/*
Non-Recursive Implementation of sequential Traversal
Stack-based implementation. This method can be understood using recursive methods.
*/
Void BT_InOrderNoRec (BitTree node)
{
If (! Node) return;
 
Stack <BitTree> s;
While (! S. empty ())
{
/* If the current node is not empty, it is not accessed. Instead, it is put into the stack and the current node becomes the left byte;
This method has always been adopted. According to the features of the stack, the left byte points will be in front of the current node in the future;
This is exactly the feature of sequential traversal.
*/
If (! Node)
{
Push (node );
Node = node-> left ();
}
/* If the current node is empty, access the node from the stack and then access the right child node.
This is not easy to understand. In fact, the start point here is that the Left byte point is empty, so the current node is accessed, and then the right subnode;
At the same time, the binary tree with the root node is actually the left byte point of the upper layer, and so on, which is exactly the feature of sequential traversal.
*/
Else
{
Node = s. pop ();
Visit (node );
Node = node-> right;
}
}
}

/*
Non-Recursive Implementation of sequential Traversal
Stack-based implementation. This method can be understood using recursive methods.
*/
Void BT_InOrderNoRec (BitTree node)
{
If (! Node) return;

Stack <BitTree> s;
While (! S. empty ())
{
/* If the current node is not empty, it is not accessed. Instead, it is put into the stack and the current node becomes the left byte;
This method has always been adopted. According to the features of the stack, the left byte points will be in front of the current node in the future;
This is exactly the feature of sequential traversal.
*/
If (! Node)
{
Push (node );
Node = node-> left ();
}
/* If the current node is empty, access the node from the stack and then access the right child node.
This is not easy to understand. In fact, the start point here is that the Left byte point is empty, so the current node is accessed, and then the right subnode;
At the same time, the binary tree with the root node is actually the left byte point of the upper layer, and so on, which is exactly the feature of sequential traversal.
*/
Else
{
Node = s. pop ();
Visit (node );
Node = node-> right;
}
}
}


V. Post-Order Non-recursion

[Cpp]
/*
Non-Recursive Implementation of post-order traversal
Stack-based implementation is not easy to understand, but at least we don't need to use a variety of flag spaces.
Ideas written in comments
*/
Void BT_PostOrderNoRec (BitTree node)
{
If (! Node) return;
 
Stack <BitTree> s;
BitTree tmp; // used to mark the accessed Node
While (! Node &&! S. empty ())
{
// If the current node is not empty, it is pushed into the stack and the current node changes to the left byte
If (node)
{
S. push (node );
Node = node-> left;
}
// If it is null, determine the next step based on the node at the top of the stack
Else
{
// Obtain the top node of the stack, not pop
Node = s. getPop ();
// If the top node of the stack has a right subnode (which is not easy to understand, but important) the right subnode is not just accessed by us,
// Then, we will go to the right subtree to access
If (node-> right & node-> right! = Tmp)
{
// Access the right subtree as a new start: the root node is pushed to the stack to access the left byte
S. push (node-> right );
Node = node-> right-> left;
}
// If the top node of the stack does not have the right subnode, or we have just accessed the right subnode, the requirements for post-sequential traversal are met. We can access the current node.
Else
{
// Access the current node, set the flag node (tmp) to the current node, and set the current node to null
Node = s. pop ();
Visit (node );
Tmp = node;
Node = null;
}
}
}
}

/*
Non-Recursive Implementation of post-order traversal
Stack-based implementation is not easy to understand, but at least we don't need to use a variety of flag spaces.
Ideas written in comments
*/
Void BT_PostOrderNoRec (BitTree node)
{
If (! Node) return;

Stack <BitTree> s;
BitTree tmp; // used to mark the accessed Node
While (! Node &&! S. empty ())
{
// If the current node is not empty, it is pushed into the stack and the current node changes to the left byte
If (node)
{
S. push (node );
Node = node-> left;
}
// If it is null, determine the next step based on the node at the top of the stack
Else
{
// Obtain the top node of the stack, not pop
Node = s. getPop ();
// If the top node of the stack has a right subnode (which is not easy to understand, but important) the right subnode is not just accessed by us,
// Then, we will go to the right subtree to access
If (node-> right & node-> right! = Tmp)
{
// Access the right subtree as a new start: the root node is pushed to the stack to access the left byte
S. push (node-> right );
Node = node-> right-> left;
}
// If the top node of the stack does not have the right subnode, or we have just accessed the right subnode, the requirements for post-sequential traversal are met. We can access the current node.
Else
{
// Access the current node, set the flag node (tmp) to the current node, and set the current node to null
Node = s. pop ();
Visit (node );
Tmp = node;
Node = null;
}
}
}
}

 

Related Article

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.