Binary Tree Learning 1:2 fork tree creation and traversal

Source: Internet
Author: User

There are three ways to traverse a binary tree:

1) First Order traversal: If the binary tree is empty, then empty operation; not NULL, first access to the root node, the first sequence to traverse the left subtree, the first sequence to traverse the right sub-tree.

2) post-order traversal: If the binary tree is empty, then empty operation; not empty, then the middle sequence traverses the left subtree, accesses the root node, and traverses the right subtree in the middle sequence.

3) post-order traversal: If the binary tree is empty, then empty operation, not empty, then the post-order traversal of the left sub-tree, the order to traverse the right subtree, access to the root node.

Cases:

1) The result of the first order traversal is: ABDECF

2) The result of the middle sequence traversal is: DBEAFC

3) post-order traversal results are: DEBFCA

Binary tree output of the idea is to convert the tree into a linear structure output, the general use of recursion, non-recursive method is to use the stack implementation, C + + recursive implementation is as follows:

1#include <iostream>2 using namespacestd;3 4typedefstructbitreenode{5     Chardata;6bitreenode*Plchild;7bitreenode*Prchild;8}bitreenode, *Bitree;9 Ten //create a two-fork tree One voidCreatebitree (Bitree &PTree) { A     Charch; -CIN >>ch; -     if(ch = ='*') the     { -PTree =NULL; -     } -     Else +     { -PTree =NewBitreenode; +Ptree->data =ch; ACreatebitree (ptree->plchild); atCreatebitree (ptree->prchild); -     } - } -  - //first-order traversal of binary tree - voidPreordertrav (Bitree &PTree) { in     if(PTree! =NULL) -     { tocout << Ptree->data <<'\ t'; +Preordertrav (ptree->plchild); -Preordertrav (ptree->prchild); the     } * } $ Panax Notoginseng //Middle Sequence Traversal - voidInordertrav (Bitree &PTree) { the     if(PTree! =NULL) +     { AInordertrav (ptree->plchild); thecout << Ptree->data <<'\ t'; +Inordertrav (ptree->prchild); -     } $ } $  - //Post-post traversal - voidPosordertrav (Bitree &PTree) { the     if(PTree! =NULL) -     {WuyiPosordertrav (ptree->plchild); thePosordertrav (ptree->prchild); -cout << Ptree->data <<'\ t'; Wu     } - } About  $ voidMain () - { -Bitree PTree =NULL; - Createbitree (pTree); Acout <<"first-order traversal:"<<Endl; + Preordertrav (pTree); thecout << Endl <<"Middle sequence Traversal:"<<Endl; - Inordertrav (pTree); $cout << Endl <<"Post-post traversal:"<<Endl; the Posordertrav (pTree); thecout <<Endl; the}

Binary Tree Learning 1:2 fork tree creation and 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.