Two methods for creating a two-fork tree and three traversal methods

Source: Internet
Author: User

two ways to create a two-fork tree and three ways to traverse it

Here are two ways to create a method that is worth the data structure above:

The code of the method is as follows:

The structure of the binary tree is defined as follows:

typedefstruct BinaryTreeNode{    char value;    struct  BinaryTreeNode *left;     struct BinaryTreeNode *right;};
-C language version
void CreateBinaryTree(BinaryTreeNode **T){ char data; scanf("%d",&data); if(data==‘#‘) *T=NULL; else { *T=(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode)); (*T)->value=data; CreateBinaryTree(&((*T)->left)); CreateBinaryTree(&((*T)->right)); }}
-C + + version
void  createbinarytree (Binarytreenode * &t) {char  data; cin  >>data; if  (Data== ' # ' ) T=null ; else  {t=new  Binarytreenode; t->value=data; Createbinarytree (T->left); Createbinarytree (T->right); }}
  The return value of the function is null, Note the C language version of the pointer, in C + + in lieu of, the program is more concise, clear C language version, when creating the left and right sub-tree node is, easy to write & (T->left) and & (T->right) note here T and *t difference *t point to B The pointer t of the Inarytreenode node is a pointer to the pointer, *t->left refers to the binarytreenode of the left node C + + version without the C language pointer so responsible, with reference, the operation of T will affect T  

The method two code is as follows:

-C + + version
Binarytreenode * CreateBinaryTree2 () {Binarytreenode *t;CharDataCin>>data;//scanf ();    if(data==' # ') t=NULL;Else{t=NewBinarytreenode;//(Binarytreenode *) malloc (sizeof (Binarytreenode) )t->value=data;        T->left=createbinarytree2 ();    T->right=createbinarytree2 (); }returnT;}
    这种创建方法和方法一的不同之处在于 函数返回值为BinaryTreeNode *    逻辑更加清楚,利用函数的返回值,构建树的左右子树

Three recursive traversal methods, the code is as follows

//first  order void Preordertree (Binarytreenode *t) {if (    T==null) return;    cout<<t->value<< "";    Preordertree (T->left); Preordertree (t->right);}    middle order void Inordertree (Binarytreenode *t) {if (t==null) return;    Inordertree (T->left);    cout<<t->value<< ""; Inordertree (t->right);}    Post-post void Postordertree (Binarytreenode *t) {if (t==null) return;    Postordertree (T->left);    Postordertree (T->right); cout<<t->value<< "";}  

Total code

#include <iostream>#include <stdlib.h>#include <malloc.h>using namespace STD;typedef structbinarytreenode{CharValuestructBinarytreenode *left;structBinarytreenode *right;};/*void createbinarytree (Binarytreenode **t) {char data;    cin>>data;    if (data== ' # ') *t=null;         else {*t= (Binarytreenode *) malloc (sizeof (Binarytreenode));         (*t)->value=data;         Createbinarytree (& (*t)->left);    Createbinarytree (& (*t)->right); }}*/voidCreatebinarytree (Binarytreenode * &t) {CharDataCin>>data;if(data==' # ') t=NULL;Else{t=NewBinarytreenode;         t->value=data;         Createbinarytree (T->left);    Createbinarytree (T->right); }}binarytreenode * CreateBinaryTree2 () {Binarytreenode *t;CharDataCin>>data;if(data==' # ') t=NULL;Else{t=NewBinarytreenode;        t->value=data;        T->left=createbinarytree2 ();    T->right=createbinarytree2 (); }returnT;}voidPreordertree (Binarytreenode *t) {if(t==NULL)return;cout<<T->value<<" ";    Preordertree (T->left); Preordertree (t->right);}voidInordertree (Binarytreenode *t) {if(t==NULL)return; Inordertree (T->left);cout<<T->value<<" "; Inordertree (t->right);}voidPostordertree (Binarytreenode *t) {if(t==NULL)return;    Postordertree (T->left); Postordertree (T->right);cout<<T->value<<" ";}voidMain () {cout<<"---First method of creating a two-fork tree the input sequence is a B C # # # # # # # #----------" <<endl;    cout<< "Please enter the first order to create a two-fork tree sequence, with the ' # ' sign that the node is null, the sequence is as follows:" <<endl;    Binarytreenode *t;    Createbinarytree (T);    cout<< "Method 1 is listed as the first order:";    Preordertree (T);    cout<<endl; cout<< "The central sequence of Method 1 is listed as: ";    Inordertree (T);    cout<<endl;    cout<< "Method 1 of the order sequence is:";    Inordertree (T);    cout<<endl;    cout<< "-----First method of creating two-fork tree the input sequence is a B C # # # # # # # #--------" <<endl;    cout<< "Please enter the first order to create a two-fork tree sequence, with the ' # ' sign that the node is null, the sequence is as follows:" <<endl;    Binarytreenode *t1; T1=createbinarytree2 ();cout<< "Method 2 is listed as the first order:";    Preordertree (T1);        cout<<endl;    cout<< "Method 2 is listed in the preface:";    Inordertree (T1);    cout<<endl;    cout<< "Method 2 of the order sequence is:";    Inordertree (T1); Cout<<endl;}


Two methods for creating a two-fork tree and three traversal methods

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.