Binary tree traversal

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
Typedef char telemtype;
Typedef struct bitnode
{
Telemtype data;
Struct bitnode * lchild, * rchild;
} Bitnode, * bitree;

Void createbitree (bitree * t)
{
// Input the value of the node in the binary tree in the first order, # Representing the empty tree
Char ch;
Scanf ("% C", & Ch );
If ('#' = CH)
{
* T = NULL;
}
Else
{
* T = (bitnode *) malloc (sizeof (bitnode ));
(* T)-> DATA = CH;
Createbitree (& (* t)-> lchild );
Createbitree (& (* t)-> rchild );
}
}
Void printelement (telemtype E)
{
Printf ("% C", e );
}
Void preordertraverse (bitree T, void (* visit) (telemtype E ))
{
// Recursive algorithm for traversing binary tree T in line Order, and visit method is called for each element
If (t)
{
Visit (t-> data );
Preordertraverse (t-> lchild, visit );
Preordertraverse (t-> rchild, visit );
}
}

Void inordertraverse (bitree T, void (* visit) (telemtype E ))
{
If (t)
{
Inordertraverse (t-> lchild, visit );
Visit (t-> data );
Inordertraverse (t-> rchild, visit );
}
}

Void postordertraverse (bitree T, void (* visit) (telemtype E ))
{
If (t)
{
Postordertraverse (t-> lchild, visit );
Postordertraverse (t-> rchild, visit );
Visit (t-> data );
}
}
Int main (void)
{
Bitree T = NULL;
Createbitree (& T );
Printf ("sequential traversal :");
Preordertraverse (T, printelement );
Printf ("sequential traversal :");
Inordertraverse (T, printelement );
Printf ("sequential traversal :");
Postordertraverse (T, printelement );

}

Binary tree traversal

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.