Data structure and algorithm analysis of the binary tree three ways to traverse. --pre-sequence traversal, middle-order traversal and sequential traversal __java

Source: Internet
Author: User
before we introduce the traversal algorithm of binary tree, we need to introduce the concepts of binary tree and traversal method. Two fork Tree: is a special structure of the tree, in the binary tree, each node can have a maximum of two child nodes. The most important operation in the two-fork tree is traversal, which usually has several ways of traversing the binary tree:

Pre-sequence traversal: First access to the root node, then access to the left node, the last access to the right node. For example, the ordinal traversal order is: 10, 6, 4, 8, 14, 12, 16. (Root node in the first place)
In-sequence traversal: First access to the left node, and then access the root node, the last access to the right node. The sequence traversal order shown in the figure is: 4, 6, 8, 10, 12, 14, 16. (root node in the middle)
Subsequent traversal: first access to the left node, and then access to the right node, the last access to root node. The sequential traversal sequence shown in the figure is: 4, 8, 6, 12, 16, 14, 10. (root node at last)
not much to say, directly on an example of a deep understanding of the binary tree construction and traversal process. Title: Create a two-fork tree and output the number of layers per character. The binary tree is shown in the following illustration:

as shown in the picture, this is an ordinary two-fork tree. For this question, my idea is this:

1, the establishment of two-fork tree node, with the structure of data types to express;
2, according to the rules of the preceding sequence to build two fork tree-a recursive way to complete the two-tree construction process, where the node is passed the location of the pointer.
3, the same way, with the middle sequence traversal and sequential traversal output nodes sorted.

Detailed description See below analysis:
1, the first definition of a structure, the data contained in the two-fork tree each node data values, and points to the left and right node pointers. The definition of this data structure body is for the back establishment of the two-fork tree left ready:

typedef struct BITNODE
{
    elemtype data;
    struct Bitnode *lchild,*rchild;
} bitnode,*bitree;//the bitnode==*bitree here.

2, the two-fork tree node has been established, the next thing we need to do is to connect these nodes, generate a specific two-fork tree, the following according to the conditions given, in order to first traverse the way to enter the subject of the two-tree sequence , so as to generate a binary tree. Note here that the generation method used is to generate a two-fork tree recursively, that is, to connect the left and right pointers of each node to a specific node until the end.
Note: In order to ensure the uniqueness of the binary tree, the vacant cotyledons are replaced by a space symbol , because only a two-fork tree is established according to the first-order traversal method.

void Createbitree (Bitree *t)
{
elemtype C;
scanf ("%c", &c);
  if (' ==c ')
  *t=null;
  else
  {
  *t= (Bitree *) malloc (sizeof (Bitnode));
  (*t)->data=c;
  Createbitree (& ((*t)->lchild));
  Createbitree (& (*t)->rchild);
  }
   return;
}

Note: Here is the incoming Bitree * t parameter, which is a difficult point. First of all, this is a module to create a two-fork tree, simply speaking, each node is connected according to a certain rule. If the input mode takes First order traversal as an example, each node is connected by the way of first order traversal. First access to the root node, and then access to the left tree, and then access the right subtree. Every time this function is recursive, the scanf function is called, and the value of the current node is entered. (Here the scanf enter the node value of the entire binary tree), including the nonexistent cotyledons, replaced by a space . Because the data for the next node cannot be obtained, the address of the next node needs to be passed in ahead of the recursion process. It then operates by reading the address. * t= (bitree*) malloc (sizeof (Bitnode)); Type conversions are forced here because T contains the address of the structure body, so it is necessary to cast the right to the type of the structure's addresses as well.
3, after the completion of the binary tree, the need to start the traversal of binary tree work. Here is also a recursive operation, simply to deal with a node each time. first-order traversal as an example, first output the value of the node, and depth information, and then recursively to the next left node information, and then the right node information , in this way output the whole binary tree node information.

The same way to achieve the sequence traversal and sequential traversal.

Pre-sequence traversal binary tree
 int visitnum=0;
 void Preordertraverse (Bitnode *t,int level)
 {

     if (bool&) T)
     {
         if (visitnum==0)
           cout<< t->data<< "";
         else
           visit (t->data,level);
         Preordertraverse (t->lchild,level+1);
         Preordertraverse (t->rchild,level+1);
     }
     Visitnum=1;
     return;
 }

Note that the process of traversing, the binary tree has been established. Therefore only need to point to the structure of the pointer, the traversal operation is applied to the structure of data, including values and pointers, directly with the operation of the pointer can be.
4, finally write a functional module, to achieve the depth of the node, only need to output the node value and depth value can be. So add a depth value when you implement it. The
specific code is as follows:

/* Pre-order traversal: first access to the root node, in the access to the left subtree, after access to the right subtree sequence traversal: First access to the left subtree, root node, right subtree follow-up traversal: first access to the left subtree, then the right subtree, root node * * * #include <stdio.h> #include <
Iostream> using namespace std;

typedef char ELEMTYPE;
    typedef struct BITNODE {elemtype data;
struct Bitnode *lchild,*rchild;

The space content that the}bitnode,*bitree;//bitree points to is the Bitnode type of data.
Creating a binary tree void Createbitree (Bitree *t)//*t is a pointer to the structure body data, and the address of the structure body is stored in the pointer bitree.
    {Elemtype C;
    scanf ("%c", &c);
        if (' ==c) {*t=null;//The address of the next node is empty} else {*t= (Bitnode *) malloc (sizeof (Bitnode));
        (*t)->data=c; Createbitree (& (*t)->lchild); The value in the pointer points to the left child, the address of the left Child Createbitree (& (*t)->rchild);//} return
;
      //access the nodes of the binary tree and operate on the node void visit (char c,int level) {printf ("%c node is on layer%d \ n", c,level);
  return;
         }//Pre-sequence traversal binary tree void Preordertraverse (Bitnode *t,int level)//Here's the pointer, which holds the structure data {if ((bool&) T) {
        cout<<t->data<< ""; Preordertraverse (t->lchild,level+1);

     Preordertraverse (t->rchild,level+1);
 } return; } void Preordertraversevisit (Bitnode *t,int level)//Here the pointer, which holds the structure data {if ((bool&) T) {Visit (
         T->data,level);
         Preordertraversevisit (t->lchild,level+1);

     Preordertraversevisit (t->rchild,level+1);
 } return; }//In-sequence traversal binary tree void Midordertraverse (Bitnode *t,int level) {if ((bool&) T) {Midordertraverse (T-&G
         T;LCHILD,LEVEL+1);
         Visit (T->data,level);
         cout<<t->data<< "";
     Midordertraverse (t->rchild,level+1);
 } return; }//sequence traversal binary tree void Lastordertraverse (Bitnode *t,int level) {if ((bool&) t) {Lastordertraverse (t
         -&GT;LCHILD,LEVEL+1);
         Lastordertraverse (t->rchild,level+1);
         Visit (T->data,level);
     cout<<t->data<< "";
 } return; int main () {int level=1;

     Bitnode *t=null;
     Createbitree (&t);
     cout<< "Pre-sequence binary tree traversal result:" <<endl;
     Preordertraverse (T,level);
     cout<<endl;
     cout<< "Middle sequence binary tree traversal result:" <<endl;
     Midordertraverse (T,level);
     cout<<endl;
     cout<< "Subsequent binary tree traversal results:" <<endl;
     Lastordertraverse (T,level);
     cout<<endl;
     cout<< "Number of layers of each node:" <<endl;
     Preordertraversevisit (T,level);
     cout<<endl;

     Preordertraverse (T,level);
     System ("pause");

 return 0; }

Note: When you enter a binary tree node sequence, you must add a space to the leaf without the cotyledons.
such as: AB "" D "" "" CE "" ""
Enter the results as follows:

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.