1. Pre/middle/sequential traversal (recursive implementation)
Copy Code code as follows:
Pre-sequence traversal
void Bt_preorder (Bitreeptr pnode) {
if (!pnode) return;
Visit (Pnode);
Bt_preorder (Pnode->left);
Bt_preorder (Pnode->right); }
In-sequence traversal
void Bt_preorder (Bitreeptr pnode) {
if (!pnode) return;
Bt_preorder (Pnode->left);
Visit (Pnode);
Bt_preorder (pnode->right);}
Subsequent traversal of void Bt_preorder (Bitreeptr pnode) {
if (!pnode) return;
Bt_preorder (Pnode->left);
Bt_preorder (Pnode->right);
Visit (pnode);}
2. Pre-sequence traversal (non-recursive implementation)
Copy Code code as follows:
Using stacks to implement
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;
}
}
}
Using stacks to implement
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 implementation Each node contains the parent node pointer and the isvisited "default is False" state variable and the two-fork tree contains a header node
void Bt_preordernorec3 (Bitreeptr pnode) {
while (!pnode)
Exits when backtracking to the head node of the root node
{
if (!pnode->bvisited)
Determine if they have 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. In-sequence traversal (non-recursive implementation)
Copy Code code as follows:
Using stacks to implement
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;
}
}}
Do not use the stack to implement each node with the parent node pointer and isvisited "default to False" state variables and the two-fork tree with a head node
void Bt_inordernorec2 (Bitreeptr pnode) {
while (!pnode)
Exits when backtracking to the head node of the root node
{
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. Subsequent traversal (not recursive implementation)
Copy Code code as follows:
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 the left and right subtree are already on the stack, accessing 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 Code code as follows:
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);
}}