Binary Tree depth-first traversal of data structures

Source: Internet
Author: User

Binary Tree depth-first traversal of data structures

Binary tree traversal can be divided into two types:

I. Depth

First-order traversal

Summary

Post-order traversal

2. breadth (from left to right)

Sequence Traversal

The following are three depth traversal methods:

# Include
 
  
Using namespace std; typedef struct BitNode {char data; struct BitNode * lchild, * rchild;} BitNode, * BiTree; void CreateBiTree (BiTree & T); void PreOrderTraverse (BiTree T ); void InOrderTraverse (BiTree T); void PostOrderTraverse (BiTree T); void CreateBiTree (BiTree & T) {// complete string sequence representation when creating a binary tree, // example: AB # c # char ch; scanf ("% c", & ch); if (ch = '#') T = NULL; // empty tree else {T = new BitNode; if (T) {T-> data = ch; CreateBiTree (T-> lch Ild); CreateBiTree (T-> rchild) ;}}// first traverse the binary tree void PreOrderTraverse (BiTree T) {if (T! = NULL) {cout <T-> data <endl; PreOrderTraverse (T-> lchild); PreOrderTraverse (T-> rchild );}} // traverses Binary Tree void InOrderTraverse (BiTree T) {if (T! = NULL) {InOrderTraverse (T-> lchild); cout <T-> data <endl; InOrderTraverse (T-> rchild );}} // post-order traversal of the Binary Tree void PostOrderTraverse (BiTree T) {if (T! = NULL) {PostOrderTraverse (T-> lchild); PostOrderTraverse (T-> rchild); cout <T-> data <endl ;}} int main () {BiTree T; CreateBiTree (T); cout <"the binary tree has been generated and the first traversal starts:" <endl; PreOrderTraverse (T); cout <"Start the Middle-order traversal: "<endl; InOrderTraverse (T); cout <" start post-order traversal: "<endl; PostOrderTraverse (T); return 0 ;}
 




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.