8606 binary tree construction and traversal, 8606 binary tree construction

Source: Internet
Author: User

8606 binary tree construction and traversal, 8606 binary tree construction
8606 build and traverse a binary tree

Time Limit: 1000 MS memory limit: 1000 K

Question type: Programming Language: Unlimited
Description

Construct a Binary Tree represented by a binary linked list: Enter the node value (one character) in the binary tree in the FIFO order, and the '#' character indicates the empty tree to construct the binary tree T represented by the binary linked list; output three traversal sequences. Only part of the code is provided in this question. Please complete the content.
# Include "stdio. h"
# Include "malloc. h"
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define INFEASIBLE-1
# Define OVERFLOW-2
Typedef int Status;

Typedef char ElemType;
Typedef struct BiTNode {
ElemType data;
Struct BiTNode * lchild, * rchild; // left and right child pointer
} BiTNode, * BiTree;

Status CreateBiTree (BiTree & T) {// algorithm 6.4
// Enter the value of the node in the binary tree in the First Order (one character). The '#' character indicates the empty tree,
// Construct the binary tree T represented by the binary linked list.
Char ch;
Scanf ("% c", & ch );
If (ch = '#') T = NULL;
Else {
If (! (T = (BiTNode *) malloc (sizeof (BiTNode) return ERROR;
________________________ // Generate the root node
_______________________ // Construct the left subtree
_________________________ // Construct the right subtree
}
Return OK;
} // CreateBiTree


Status PrintElement (ElemType e) {// value of output Element e
Printf ("% c", e );
Return OK;
} // PrintElement


Status PreOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in the forward order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.

} // PreOrderTraverse

Status InOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in the middle order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.


} // InOrderTraverse

Status PostOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in descending order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.

} // PostOrderTraverse



Int main () // main Function
{
// Add code
} // Main



Input Format

First line: Enter the first sequential traversal sequence of a binary tree

Output Format

First line: the first sequential traversal sequence of a binary tree
Row 2: ordinal traversal sequence of a binary tree
Row 3: Post-order traversal sequence of Binary Trees
Input example

AB ## C ##
Output example

ABC
BAC

BCA



Answer:


/* Construct a Binary Tree represented by a binary linked list: Enter the node value (one character) in the binary tree in the first order, and the '#' character indicates the empty tree,
Construct the binary tree T represented by the binary linked list, and then output three traversal sequences. Only part of the code is provided in this question. Please complete the content. */
# Include "stdio. h"
# Include "malloc. h"
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define INFEASIBLE-1
# Define OVERFLOW-2
Typedef int Status;

Typedef char ElemType;
Typedef struct BiTNode {
ElemType data;
Struct BiTNode * lchild, * rchild; // left and right child pointer
} BiTNode, * BiTree;

Status CreateBiTree (BiTree & T) {// algorithm 6.4
// Enter the value of the node in the binary tree in the First Order (one character). The '#' character indicates the empty tree,
// Construct the binary tree T represented by the binary linked list.
Char ch;
Scanf ("% c", & ch );
If (ch = '#') T = NULL;
Else {
If (! (T = (BiTNode *) malloc (sizeof (BiTNode) return ERROR;
T-> data = ch; // generate the root node
CreateBiTree (T-> lchild); // construct the left subtree
CreateBiTree (T-> rchild); // construct the right subtree
}
Return OK;
} // CreateBiTree


Status PrintElement (ElemType e) {// value of output Element e
Printf ("% c", e );
Return OK;
} // PrintElement


Status PreOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in the forward order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.
If (T)
{
If (Visit (T-> data ))
If (PreOrderTraverse (T-> lchild, Visit ))
If (PreOrderTraverse (T-> rchild, Visit ))
Return OK;
Return ERROR;

}
Else return OK;


} // PreOrderTraverse

Status InOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in the middle order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.
If (T)
{
If (InOrderTraverse (T-> lchild, Visit ))
If (Visit (T-> data ))
If (InOrderTraverse (T-> rchild, Visit ))
Return OK;
Return ERROR;
}
Else return OK;
} // InOrderTraverse

Status PostOrderTraverse (BiTree T, Status (* Visit) (ElemType )){
// The recursive algorithm that traverses binary tree T in descending order and calls the Visit function for each data element.
// Complete the code. Multiple statements are available.
If (T)
{
If (PostOrderTraverse (T-> lchild, Visit ))
If (PostOrderTraverse (T-> rchild, Visit ))
If (Visit (T-> data ))
Return OK;
Return ERROR;
}
Else return OK;
} // PostOrderTraverse



Int main () // main Function
{
BiTree T; // supplemental code
CreateBiTree (T );
PreOrderTraverse (T, PrintElement );
Printf ("\ n ");
InOrderTraverse (T, PrintElement );
Printf ("\ n ");
PostOrderTraverse (T, PrintElement );
Printf ("\ n ");
Return 0;

} // Main


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.