Recursive Algorithm Program for binary tree traversal

Source: Internet
Author: User
Binary tree traversal is a typical recursive algorithm. Tree traversal refers to accessing each node in the tree by a search path, so that each node is accessed once and only once. My program provides the first-order traversal of Binary trees, the middle-order traversal, the latter-order traversal, and the depth of the binary tree. The procedure is as follows:

# Include <stdio. h>
# Include <malloc. h>
# Include <stdlib. h>
# Define OK 1
# Define error 0
# Define overflow-2
# Define max (A, B) (A> B? A: B)

Typedef char telemtype;
Typedef int status;

// Binary Tree binary linked list Storage Structure
Typedef struct bitnode {
Telemtype data;
Struct bitnode * lchild, * rchild;
} Bitnode, * bitree;

// First traverse to generate a binary tree
Status creatbitree (bitree & T ){
Telemtype CH, temp;
Printf ("input an element :");
Scanf ("% C", & Ch );
Temp = getchar (); // press ENTER
If (CH = '') t = NULL; // enter a space to indicate that the node is an empty tree.
Else {
If (! (T = (bitree) malloc (sizeof (bitnode) Exit (overflow );
T-> DATA = CH; // generate the root node
Creatbitree (t-> lchild); // construct the left subtree
Creatbitree (t-> rchild); // construct the right subtree
}
Return OK;
}

// Print the element
Status printelem (telemtype e ){
Printf ("% C", e );
Return OK;
}

// Traverse Binary Trees in ascending order
Status preordertraverse (bitree T, status (* visit) (telemtype E )){
If (t) {// when the binary tree is not empty
If (visit (t-> data) // access the root node
If (preordertraverse (t-> lchild, visit) // first traverse the left subtree
If (preordertraverse (t-> rchild, visit) Return OK; // first traverse the right subtree
Return Error;
}
Else Return OK;
}

// Traverse a binary tree in the middle order
Status inordertraverse (bitree T, status (* visit) (telemtype E )){
If (t ){
If (inordertraverse (t-> lchild, visit ))
If (visit (t-> data ))
If (inordertraverse (t-> rchild, visit) Return OK;
Else return error;
}
Return OK;
}

// Traverse Binary Trees in descending order
Status postordertraverse (bitree T, status (* visit) (telemtype E )){
If (t ){
If (postordertraverse (t-> lchild, visit ))
If (postordertraverse (t-> rchild, visit ))
If (visit (t-> data) Return OK;
Else return error;
}
Return OK;
}

// Calculate the depth of a binary tree
Int bitreedepth (bitree t ){
If (! T) return 0; // when the binary tree is empty
Int DL = 0, DR = 0;
If (t-> lchild) dL = bitreedepth (t-> lchild); // evaluate the left subtree depth
If (t-> rchild) DR = bitreedepth (t-> rchild); // evaluate the depth of the right subtree
Return max (DL, Dr) + 1;
}

// Main Function
Void main ()
{
Bitree T;
Status (* visit) (telemtype );
Visit = printelem;
Creatbitree (t );
Printf ("/n first-order traversal :");
Preordertraverse (T, visit );
Printf ("/n sequential traversal :");
Inordertraverse (T, visit );
Printf ("/n post-order traversal :");
Postordertraverse (T, visit );
Printf ("/n Binary Tree depth: % d", bitreedepth (t ));
Printf ("/n program ended./N ");
}

The output result is as follows ):

Enter an element:
Input an element: B
Input an element: c
Enter an element:
Enter an element:
Input an element: d
Input an element: E
Enter an element:
Input an element: G
Enter an element:
Enter an element:
Input an element: F
Enter an element:
Enter an element:
Enter an element:

First traversal: a B c d e G F
Sequential traversal: c B e g d F
Post-order traversal: C G E F D B
Binary Tree depth is 5
Program ended.

 

Related Article

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.