Build and traverse a binary tree in the data structure experiment of SDUTOJ 2136, sdutoj2136

Source: Internet
Author: User

Build and traverse a binary tree in the data structure experiment of SDUTOJ 2136, sdutoj2136

#include<iostream>#include<string.h>using namespace std;typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild;}BiTNode, *BiTree;int Create(BiTree &T){char data;cin>>data;if(data==','){ T=NULL;}else{T=new BiTNode;T->data=data;Create(T->lchild);Create(T->rchild);}return 0;}void visit(BiTree T){if(T->data!=',')cout<<T->data;}void InOrder(BiTree T){if(T!=NULL){InOrder(T->lchild);visit(T);InOrder(T->rchild);}}void PostOrder(BiTree T){if(T!=NULL){PostOrder(T->lchild);PostOrder(T->rchild);visit(T);}}int count_tree(BiTree T,int &n){if(T){if(T->lchild==NULL&&T->rchild==NULL)n++;count_tree(T->lchild,n);count_tree(T->rchild,n);}return n;}int high_tree(BiTree T){int h,left,right;if(!T){ return 0;}left=high_tree(T->lchild);right=high_tree(T->rchild);h=(left>right?left:right)+1;return h;}int main(){int n,a,b=0;BiTree T;Create(T);InOrder(T);cout<<"\n";PostOrder(T);cout<<"\n";a=count_tree(T,b);cout<<a<<endl;n=high_tree(T);cout<<n<<endl;return 0;}


In our data structure experiment class, we used C ++ to build a binary tree traversal program. The teacher did not talk about how to solve the problem.

In fact, when we use data structures in C and C ++, the difference can be simply the difference between input and output. Therefore, it is completely possible to compile with vc6.0, we all use vc6.0 for this experiment. The Code is as follows:
// ================================================ === Define the header
# Include <iostream>
Using namespace std;
Struct BiTNode {
Char data;
Struct BiTNode * lchild, * rchild; // left and right children
};
BiTNode * T;
Void CreateBiTree (BiTNode * & T );
Void Inorder (BiTNode * & T );
Void PreOrderTraverse (BiTNode * & T );
Void Posorder (BiTNode * & T );
// ================================================ ===== Main Function
Int main (){
Cout <"creates A tree, where A-> Z represents the data of the tree, and" # "represents the empty tree:" <endl;
CreateBiTree (T );
Cout <"first-order recursive traversal:" <endl;
PreOrderTraverse (T );
Cout <endl;
Cout <"sequential recursive traversal:" <endl;
Inorder (T );
Cout <endl;
Cout <"post-order recursive traversal:" <endl;
Posorder (T );
Cout <endl;
Return 1 ;}
// ================================================ ======== Create a binary tree recursively
Void CreateBiTree (BiTNode * & T ){
// Enter the node value (one character) in the binary tree in the First Order. The space character represents the empty tree,
// Construct a binary tree table to represent the binary tree T.
Char ch;
If (ch = getchar () = '#') T = NULL; // getchar () is the function that reads standard library functions one by one.
Else {
T = new BiTNode; // generates a new subtree.
T-> data = ch; // read one by getchar ()
CreateBiTree (T-> lchild); // recursively create the left subtree
CreateBiTree (T-> rchild); // recursively create the right subtree
}
} // CreateTree
// ================================================ =========== First-order recursive traversal of Binary Trees
Void PreOrderTraverse (BiTNode * & T ){
// Recursively traverse Binary Trees in ascending order
If (T) {// execute when the node is not empty
Cout <T-> data;
PreOrderTraverse (T-> lchild );//
PreOrderTraverse (T-& ...... remaining full text>

Data Structure binary tree traversal

Know the First Order (left and right) and the middle order (left and right), you can find the back order (left and right); know the middle order and the back order, you can find the first order; know the first order and the back order, the resulting binary tree is not unique. All these books have been mentioned. Based on these pushes.

32. B
33.
34. D first, determine that the root node is C. The 2-fork root node does not have the right subtree. Then, only the dabe is left in the subsequent order, and the middle order is deba,
E, and confirm that e is the root, while the left side of the central order is only d, so the left side of e is d, so the right side of the middle order is only ba not determined.
Then the order is AB, so we can determine that a is the right child of B.
35. B
If the rest are pushed like this, OK
You only need to remember the order (left root and right) in the first order (left root and right root) and then the order (left and right root.

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.