Several traversal algorithms for C + + two-fork Tree _c language

Source: Internet
Author: User

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);
}}

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.