Use Recursive Algorithms and non-recursive algorithms to traverse Binary Trees

Source: Internet
Author: User

Recursive Algorithm:

# Include <iostream. h>
Typedef char elemtype;
Struct bitree
{
Elemtype data;
Bitree * lchild, * rchild;

};
Bitree * Create () // create a binary linked list
{
Bitree * Q [100]; // defines the Q array as the queue for storing nodes in the binary linked list. 100 is the maximum capacity.
Bitree * s; // node of the binary linked list
Bitree * root; // binary linked list root pointer
Int front = 1, rear = 0; // define the header and tail pointer
Char ch; // The data field of the node
Root = NULL;
Cout <"Enter the node value (the nonexistent value is ','; '#' indicates the end )";
Cin> CH;
While (Ch! = '#') // Until input # ends with the algorithm
{
S = NULL;
If (Ch! = ',') // "," Indicates a virtual node
{
S = new bitree;
S-> DATA = CH;
S-> lchild = NULL;
S-> rchild = NULL;
}
Rear ++;
Q [rear] = s;
If (Rear = 1) root = s;
Else
{
If (S! = NULL) & (Q [Front]! = NULL ))
{
If (Rear % 2 = 0) Q [Front]-> lchild = s; // rear is an even number, and s is the left child
Else Q [Front]-> rchild = s; // The rear is an odd number, and the S is the right child.
}
If (Rear % 2 = 1) Front ++; // leaves
}
Cin> CH;
}
Return root;
}

Void preorder (bitree * root) // preorder traversal
{
Bitree * P;
P = root;
If (P! = NULL)
{
Cout <p-> data <Endl;
Preorder (p-> lchild );
Preorder (p-> rchild );
}
}
Void inorder (bitree * root) // supports sequential traversal.
{
Bitree * P;
P = root;
If (P! = NULL)
{
Inorder (p-> lchild );
Cout <p-> data <Endl;
Inorder (p-> rchild );
}
}
Void postorder (bitree * root) // post-order traversal
{
Bitree * P;
P = root;
If (P! = NULL)
{
Postorder (p-> lchild );
Postorder (p-> rchild );
Cout <p-> data <Endl;
}
}

Void main ()
{
Bitree * root;
Root = create ();
Cout <Endl <"returns the traversal result before the left and right subtree is exchanged:" <Endl;
Cout <"sequential traversal" <Endl;
Preorder (Root); cout <Endl;
Cout <"sequential traversal" <Endl;
Inorder (Root); cout <Endl;
Cout <"post-order traversal" <Endl;
Postorder (Root); cout <Endl;

}

Non-recursive algorithms: stacks must be set in non-recursive algorithms.

# Include <iostream. h>
Typedef char elemtype;
Struct bitree
{
Elemtype data;
Bitree * lchild, * rchild;

};
Bitree * Create () // create a binary linked list
{
Bitree * Q [100]; // defines the Q array as the queue for storing nodes in the binary linked list. 100 is the maximum capacity.
Bitree * s; // node of the binary linked list
Bitree * root; // binary linked list root pointer
Int front = 1, rear = 0; // define the header and tail pointer
Char ch; // The data field of the node
Root = NULL;
Cout <"Enter the node value (the nonexistent value is ','; '#' indicates the end )";
Cin> CH;
While (Ch! = '#') // Until input # ends with the algorithm
{
S = NULL;
If (Ch! = ',') // "," Indicates a virtual node
{
S = new bitree;
S-> DATA = CH;
S-> lchild = NULL;
S-> rchild = NULL;
}
Rear ++;
Q [rear] = s;
If (Rear = 1) root = s;
Else
{
If (S! = NULL) & (Q [Front]! = NULL ))
{
If (Rear % 2 = 0) Q [Front]-> lchild = s; // rear is an even number, and s is the left child
Else Q [Front]-> rchild = s; // The rear is an odd number, and the S is the right child.
}
If (Rear % 2 = 1) Front ++; // leaves
}
Cin> CH;
}
Return root;
}

Void preorder (bitree * root) // preorder traversal
{
Bitree * P, * s [100];
Int Top = 0;
P = root;
While (P! = NULL) | (top> 0 ))
{
While (P! = NULL)
{
Cout <p-> data <"";
S [++ top] = P;
P = p-> lchild;
}
P = s [top --];
P = p-> rchild;
}
}
Void inorder (bitree * root) // supports sequential traversal.
{
Bitree * P, * s [100];
Int Top = 0;
P = root;
While (P! = NULL) | (top> 0 ))
{
While (P! = NULL)
{
S [++ top] = P;
P = p-> lchild;
}
{
P = s [top --];
Cout <p-> data;
P = p-> rchild;
}
}
}
Void postorder (bitree * root) // post-order traversal
{
Bitree * P, * s [100];
Int S2 [100], Top = 0, B;
P = root;
Do
{
While (P! = NULL)
{
S [Top] = P; S2 [top ++] = 0;
P = p-> lchild;
}
If (top> 0)
{
B = S2 [-- top];
P = s [Top];
If (B = 0)
{
S [Top] = P;
S2 [top ++] = 1;
P = p-> rchild;
}
Else
{
Cout <p-> data <"";
P = NULL;
}
}
}
While (top> 0 );
}

Void main ()
{
Bitree * root;
Root = create ();
Cout <Endl <"returns the traversal result before the left and right subtree is exchanged:" <Endl;
Cout <"sequential traversal" <Endl;
Preorder (Root); cout <Endl;
Cout <"sequential traversal" <Endl;
Inorder (Root); cout <Endl;
Cout <"post-order traversal" <Endl;
Postorder (Root); cout <Endl;

}

 

 

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.