# Binary tree Establishment, traversal (pre-order, sequence, order), the number of leaf nodes, to find the number of nodes

Source: Internet
Author: User
Tags data structures

Binary tree is one of the most frequently tested data structures in a written interview, including the procedure to build a binary tree, three sequential traversal of the binary tree, the number of leaf nodes returned, and the total number of binary tree nodes. Building a binary tree node data structure

typedef struct NODE
{
int data;
struct Node *left,*right;
}NODE, structure body includes data, left subtree, and subtree;

One, the establishment of a two-fork Tree program code is as follows

[CPP] View Plain copy print? Node *creatbtree ()  //build two fork tree    {       Node *t;       int x;       scanf ("%d", &x);        if (x==0)        {            t=NULL;       }       else        {           t= (node*) malloc ( sizeof (Node));           t->data=x;            printf ("\ n Please enter the left Dial hand node for%d nodes:",t->data );            t->left=creatbtree ();            printf ("\ n Please enter the right child node of the%d node:",t->data );        &Nbsp;  t->right=creatbtree ();       }        return t;  }

```Node *creatbtree ()//Build two fork tree
{
Node *t;
int x;
scanf ("%d", &x);
if (x==0)
{
t=null;
}
else
{
t= (node*) malloc (sizeof (Node));
t->data=x;
printf ("\ n Please enter the left Dial hand node for%d nodes:", t->data);
T->left=creatbtree ();
printf ("\ n Please enter the right sub-node of the%d node:", T->data);
T->right=creatbtree ();
}
return t;
}
```

Second, pre-sequence traversal binary tree

[CPP] view plain copy print?       void Previsit (Node *t) {if (t==null) return;           else {printf ("%3d", t->data);           Previsit (T->left);       Previsit (T->right); }   }

```void Previsit (Node *t)
{
if (t==null) return;
else
{
printf ("%3d", t->data);
Previsit (t->left);
Previsit (t->right);
}
}```

Third, middle sequence traversal binary tree

[CPP] view plain copy print?       void Middvisit (Node *t) {if (t==null) return;           else {middvisit (t->left);           printf ("%3d", t->data);       Middvisit (T->right); }   }

```void Middvisit (Node *t)
{
if (t==null) return;
else
{
middvisit (t->left);
printf ("%3d", t->data);
Middvisit (t->right);
}
}```

Iv. post-secondary traversal of binary tree

[CPP] view plain copy print?       void Lastvisit (Node *t) {if (t==null) return;           else {lastvisit (t->left);           Lastvisit (T->right);       printf ("%3d", t->data); }   }

```void Lastvisit (Node *t)
{
if (t==null) return;
else
{
lastvisit (t->left);
Lastvisit (t->right);
printf ("%3d", T->data);
}
}
```
V. Number of LEAF nodes returned

[CPP] view plain copy print? Int leafnum (node *t)    {        if  (! T)        {           return  0;       }       else if  ((! T->left) && (! T->right)        {            return 1;       }       else        {           return  ((Leafnum (t-> left) +leafnum (t->right));       }  }

```int Leafnum (Node *t)
{
if (! T)
{
return 0;
}
else if ((! T->left) && (! T->right))
{
return 1;
}
else
{
return ((Leafnum (T->left) +leafnum (t->right)));
}
}```

Vi. total number of returned nodes

[CPP] view plain copy print?       int Nodenum (Node *t) {if (T) {return 1+nodenum (t->left) +nodenum (t->right);       } if (t==null) {return 0; }      }

```int Nodenum (Node *t)
{
if (T)
{
return 1+nodenum (t->left) +nodenum (t->right);
}
if (t==null)
{
return 0;
}

}```

Vii. test procedures; [CPP] View Plain copy print? Int menu ();   void main ()    {       node *t=null ;       int choice;       do{            choice=menu ();            if (choice==1)            {                printf ("Binary tree Establishment, with input" 0 "means end:.) \ n ");               printf (" Please enter root node: \ n ");                t=creatbtree ();                printf ("Binary tree successfully established");           }            else  if (choice==2)            {                printf ("Sequential traversal of Binary tree  :\n");                previsit (T);            }           else if (choice==3)            {                printf ("Middle sequence traversal binary tree: \ n");                middvisit (T);           }            else if (choice==4)             {                printf ("Post-Traverse binary Tree  :\n  ");                Lastvisit (T);           }            else if (choice==5)            {                int ct=10;                ct=leafnum (T);                printf ("  Two fork Tree leaf knot point is  : \n");                printf ("%d\n", CT);                            }           else if (choice ==7)            {                int count=nodenum (T);                printf ("The two fork tree has a total of%d nodes. \ n ", count);           }            else if (choice==8)             Exit (0);       }while (choice<=8);  }
```int menu ();
void Main ()
{
Node *t=null;
int choice;
do{
if (choice==1)
{
printf ("Binary tree" is established to enter "0" to indicate the end:.) \ n ");
printf ("Please enter root node: \ n");
T=creatbtree ();
printf ("Binary tree successfully established");
}
else if (choice==2)
{
printf ("Sequential traversal of binary tree: \ n");
Previsit (T);
}
else if (choice==3)
{
printf ("middle order traversal binary tree: \ n");
Middvisit (T);
}
else if (choice==4)
{
printf ("Traverse binary tree: \ n");
Lastvisit (T);
}
else if (choice==5)
{
int ct=10;
Ct=leafnum (T);
printf ("Binary tree leaf knot points are: \ n");
printf ("%d\n", CT);

}
else if (choice==7)
{
int count=nodenum (T);
printf ("The two fork tree has a total of%d nodes.") \ n ", count);
}
else if (choice==8)
exit (0);
} while (choice<=8);
}```

[CPP] View Plain copy print? Int menu ()    {           int choice;            printf ("\ n");            printf ("  Two fork Tree  \n");            printf (" ***************************\n");            printf (" * *\n");           printf (" *  Main Menu  *\n ");           printf ("  * 1  Build two fork Tree  *\n ");           printf ("  * 2  sequential traversal of binary tree  *\n ");           printf ("  * 3  Middle sequence traversal binary tree  *\n ");           printf ("  * 4 Calendar two fork Tree  *\n ");           printf ("  * 5  Binary tree leaf knot point  *\n ");           printf ("  * 7  All nodes of the binary tree  *\n ");           printf ("  * 8  Exit program Run  *\n ");           printf ("  ****************** \ n ");           printf ("   Please enter your selection (1,2,3,4,5,6,7 , 8):  \n ");           scanf ("%d ", &choice);            return choice;  }
```int menu ()
{
int choice;
printf ("\ n");
printf ("Binary tree \ n");
printf ("***************************\n");
printf ("* *\n");
printf ("* 1 build two fork tree *\n");
printf ("* 2 sequential traversal of binary tree *\n");
printf ("* 3 sequential traversal of binary tree *\n");
printf ("* 4 sequential traversal of binary tree *\n");
printf ("* 52 tree leaf knot point *\n");
printf ("* All nodes of the 72 fork Tree *\n");
printf ("* 8 exit program run *\n");
printf ("****************************\n");
scanf ("%d", &choice);
return choice;
}```

Related Keywords:

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.