Binary Tree Reconstruction and binary tree breadth-first Traversal
# Include
# Include
Using namespace std; // Tree node class TNode {public: TNode * left, * right; char value; TNode () {left = right = NULL;} TNode (char v) {left = right = NULL; value = v ;}}; // re-build a binary tree based on the first-order traversal of the Binary Tree and the middle-order traversal of the Binary Tree // PreOrder for the Binary Tree first-order traversal of InOrder for the binary tree in the middle-order traversal of TNode * RebuildTree (string PreOrder, string InOrder) {// In the first traversal, the first element is the root TNode * root = new TNode (PreOrder [0]); // if there is only one node left, the node if (PreOrder. length () <= 1) return root; // In the middle-order traversal, find the left part of the root as the right part of the left subtree as the right subtree string LeftChild = InOrder. substr (0, InOrder. find (PreOrder [0]); string RightChild = InOrder. substr (InOrder. find (PreOrder [0]) + 1); // recursively create left and right subtree if (LeftChild. length () root-> left = RebuildTree (PreOrder. substr (1, LeftChild. length (), LeftChild); if (RightChild. length () root-> right = RebuildTree (PreOrder. substr (1 + LeftChild. length (), RightChild); return root;} // The Binary Tree first traverses void PrevOrder (TNode * root) {if (root = NULL) return; // access the root cout <root-> value; // access the left subtree if (root-> left) PrevOrder (root-> left ); // access the right subtree if (root-> right) PrevOrder (root-> right);} // traverses void InOrder (TNode * root) in a binary tree in ascending order) {if (root = NULL) return; // access the left subtree if (root-> left) InOrder (root-> left ); // access the root cout <root-> value; // access the right subtree if (root-> right) InOrder (root-> right );} // The binary tree returns void PostOrder (TNode * root) {if (root = NULL) return; // access the left subtree if (root-> left) postOrder (root-> left); // access the right subtree if (root-> right) PostOrder (root-> right ); // access the root cout <root-> value;} // binary tree traversal void BFS (TNode * root) {// use the queue to store the queue
Nodes; // The root node enters the queue. push (root); while (! Nodes. empty () {TNode * top = nodes. front (); // The cout element of the output team header <top-> value; // enter the left subtree if (top-> left) nodes. push (top-> left); // enter the right subtree if (top-> right) nodes. push (top-> right); // The nodes header is displayed. pop () ;}cout <endl ;}int main () {string Prev, In; int t; cin >>> t; while (t --) {cin> Prev> In; TNode * root = RebuildTree (Prev, In); cout <PreOrder:; PrevOrder (root); cout <endl <InOrder :; inOrder (root); cout <endl <PostOrder:; PostOrder (root); cout <endl <BFS:; BFS (root); cout <endl ;} return 0 ;}
Requirement Analysis:
Input the first and middle traversal sequences of a binary tree, and output the first, middle, last, and breadth traversal sequences.
Outline Design
First, define the tree node class and use the linked list storage method:
Class TNode
{
Public:
TNode * left, * right;
Char value;
TNode ()
{
Left = right = NULL;
}
TNode (char v)
{
Left = right = NULL;
Value = v;
}
};
For binary tree T, you can recursively define its first-order traversal, middle-order traversal, and post-order traversal as follows:
PreOrder (T) = T root node + PreOrder (T's left subtree) + PreOrder (T's right subtree)
InOrder (T) = InOrder (left subtree of T) + root node of T + InOrder (right subtree of T)
PostOrder (T) = PostOrder (left subtree of T) + PostOrder (right subtree of T) + root node of T
The plus sign indicates the string join operation. For example, for the binary tree shown in, the first traversal is dbacepidermal, the middle traversal is ABCDEFG, And the last traversal is ACBFGED.
D
/
B E
//
A C G F
Rebuilding a binary tree:
Because the first and middle traversal of a binary tree are given, the first node in the first traversal is the root node, and then the root is found in the middle traversal. the left part of the root is the left subtree, the right part is the right subtree. Follow this procedure to recursively create the left and right subtree.
The breadth first traverses the binary tree:
Use the queue to save the current access node. First, enter the root node into the queue. When the queue is not empty, access the first node of the queue and add the left and right children of the node to the queue. Then, the Header element of the queue is displayed, until the queue is empty
Program input:
The first behavior is an integer t (0
Sample Input
2 dbacepidermal ABCDEFG BCAD CBAD
Sample Output
PreOrder: dbacgf
InOrder: ABCDEFG
PostOrder: ACBFGED
BFS: DBEACGF
PreOrder: BCAD
InOrder: CBAD
PostOrder: CDAB
BFS: BCAD
Test Results