Summary of binary tree traversal algorithm

Source: Internet
Author: User

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? Summary of two-fork tree traversal algorithm

? ??

?? This article is based on the data structure and algorithm (C language version) (third edition) of the collation.

? ? A. Traversal of a binary tree? ? ? 1. Pre-order Traversal binary tree:? ? ? ? (1) If the binary tree is empty, then null operation, return NULL.


? ? ? ? (2) Visit the root node.
? ? ? ? (3) The anterior sequence traverses the left subtree.


? ? ? ? (4) The preceding sequence traverses the right subtree.


? ? ?? A. Recursive algorithm for two fork tree pre-sequence traversal:
   void Preordertraverse (Bitree bt)   {     if (BT)     {        printf ("%c", bt->data);              Visit root node        preordertraverse (bt->lchild);       The anterior sequence traverses the left subtree        preordertraverse (bt->rchild);       Pre-order traversal right sub-tree     }   }

? ?B. Use stacks to store a non-recursive algorithm for two-tree pre-order traversal of each node right subtree:? ? ?(1) When the tree is empty, the pointer p points to the root node and p is the current node pointer.


? ? ? (2) First visit the current node p. and press p into the stack s.


? ? ? (3) Make P point to the left child.
? ? ? (4) Repeat the steps (2), (3). Until P is empty.
? ? ? (5) Eject the top element of the stack from the stack s. Point P to the right child of this element.
? ? ? (6) Run the Step repeatedly (2) ~ (5) until p is empty and the stack s is empty.
? ? ? (7) End of traversal.


? ? ?? non-recursive algorithm using the pre-order traversal of the stack:

      void Preordernorec (Bitree BT)      {        stack S;        Bitree p=bt->root;        while ((null!=p) | |! Stackempty (S))        {          if (null!=p)          {            printf ("%c", p->data);            Push (s,p);            p=p->lchild;          }          else          {            p=top (S);            Pop (S);            p=p->rchild;          }}}      
? ?? C. Two-tree pre-order traversal non-recursive algorithm using two-fork-list storage:
    void preorder (Pbintreenode pbnode)    {      pbintreenode stack[100];      Pbintreenode p;      int top;      Top=0;      P=pbnode;      Do      {while        (p!=null)        {          printf ("%d\n", p->data);      Interview node P          top=top+1;          stack[top]=p;          p=p->llink;                  Continue searching for the left subtree of the node P        }        if (top!=0)        {          p=stack[top];          top=top-1;          p=p->rlink;                  Continue searching for the right subtree of node P        }      }while ((top!=0) | | (P!=null));    }

? ? 2. Middle sequence Traversal binary tree:? ? ??(1) If the binary tree is empty. Then NULL to return NULL.


? ? ? (2) The middle sequence traverses the left subtree.
? ? ? (3) Visit the root node.
? ? ? (4) The middle sequence traverses the right sub-tree.
? ? ? A. Recursive algorithm for sequence traversal in a two-tree:

    void Inordertraverse (Bitree bt)    {      if (BT)      {         inordertraverse (bt->lchild);        The middle sequence traverses the left subtree         printf ("%c", bt->data);              Visit root node         inordertraverse (bt->rchild);        Middle sequence traversal right subtree      }    }
? ? ?? B. Non-recursive algorithm for sequence traversal in a two-fork tree using stack storage:? ? ? ?(1) When the tree is empty, the pointer p points to the root node and p is the current node pointer.
? ? ? ? (2) Press p into the stack s. And make P point to its left child.
? ? ? ? (3) Run the Step repeatedly (2). Until P is empty.
? ? ? ? (4) Eject the top element from stack s and point p to this element.


? ? ? ? (5) Visit the current node p. and point P to its right child.
? ? ? ? (6) Run the Step repeatedly (2) ~ (5) until p is empty and the stack s is empty.
? ? ? ? (7) End of traversal.
? ? ? ?? non-recursive algorithm using the middle sequence traversal of the stack:

     void Ineordernorec (Bitree BT)     {       stack S;       Bitree p=bt->root;       while ((null!=p) | |! Stackempty (S))       {         if (null!=p)         {           Push (s,p);           p=p->lchild;         }         else         {           p=top (S);           Pop (S);           printf ("%c", p->data);           p=p->rchild;         }}}     
? ? ?C. A two-fork-tree sequence traversal non-recursive algorithm using a two-fork linked list:
    void Inorder (Pbintreenode pbnode)    {         pbintreenode stack[100];         Pbintreenode p;         int top;         Top=0;         P=pbnode;         Do         {           while (p!=null)           {             top=top+1;             stack[top]=p;                Node P-in-stack             p=p->llink;                  Continue searching for the left subtree of the node P           }           if (top!=0)           {             p=stack[top];                Node p out of stack             top=top-1;             printf ("%d\n", p->data);      Interview node P             p=p->rlink;                  Continue searching for the right subtree of node P           }         }while ((top!=0) | | (P!=null));    }
? ? ??? ?? 3. Post-secondary traversal of the binary tree:? ? ? (1) If the binary tree is empty. Then NULL to return NULL.


? ? ? (2) to traverse the left subtree.
? ? ? (3) The right sub-tree is traversed after the post.
? ? ? (4) Visit the root node. ? ? ?? A. Recursive algorithm for post-sequential traversal of a. Two fork tree:

     void Postordertraverse (Bitree bt)     {       if (BT)       {          postordertraverse (bt->lchild);        Post-sequential traversal left subtree          postordertraverse (bt->rchild);        Post-sequential traversal          of the right subtree printf ("%c", bt->data);                Visit root node       }     }
? ? ?? B. Non-recursive algorithm for post-routing traversal of two-forked tree using stack storage:? ? ?Algorithm idea: First scan the root node of all the left node into the stack, and then out of the stack a node, scan the node of the right node into the stack, and then scan the right node of all the left node into the stack, when a node of the left and right sub-tree are interviewed and then visit the node. Because in the recursive algorithm. The right subtree of Saozi is returned, so in order to differentiate between the two cases. You also need to set an identity stack tag, which is returned from Zuozi when the top element of the tag is 0. 1 indicates the return from the right subtree.
? ? ? ? (1) When the tree is empty, the pointer p points to the root node and p is the current node pointer.
? ? ? ? (2) Press p into the stack s, 0 presses into the stack tag, and p points to the left child.
? ? ? ? (3) Run the Step repeatedly (2) until P is empty.
? ? ? ? (4) Assume that the stack top element in the tag stack is 1, skip to step (6).


? ? ? ? (5) Assume that the stack top element in the tag stack is 0, skip to step (7).


? ? ? ? (6) Pop up the stack s top element and visit this node. Skip to Step (8).
? ? ? ? (7) Point p to the right child of the stack s top element.
? ? ? ? (8) Run the Step repeatedly (2) ~ (7) until p is empty and the stack s is empty.


? ? ? ? (9) End of traversal.


? ? ? ? Use the post-post traversal of the stack for non-recursive algorithms:

       void Postordernorec (Bitree BT)       {         stack S;         stack tag;         Bitree p=bt->root;         while ((null!=p) | |! Stackempty (S))         {           while (null!=p)           {             Push (s,p);             Push (tag,0);             p=p->lchild;           }           if (! Stackempty (s))           {             if (POP (tag) ==1)             {               p=top (s);               Pop (S);               printf ("%c", p->data);               POP (tag);    Stack tag to be synchronized with stack s             }             else             {               p=top (s);               if (! Stackempty (S))               {                 p=p->rchild;                 POP (tag);                 Push (tag,1);}}}}       
??? C. Two-tree post-traversal non-recursive algorithm using two-fork-list storage:
     void Posorder (Pbintreenode pbnode)     {          pbintreenode stack[100];       node pointer stack          int count[100];                Record the number of nodes in the stack          pbintreenode p;          int top;          Top=0;          P=pbnode;          Do          {            while (p!=null)            {              top=top+1;              stack[top]=p;                The node P is first count[top]=0 in the stack              ;              p=p->llink;                  Continue searching for the left subtree of node P            }            p=stack[top];                  Node p out of stack            top=top-1;            if (count[top+1]==0)            {              top=top+1;              stack[top]=p;                The node P is first count[top]=1 in the stack              ;              p=p->rlink;                  Continue searching for node P's right subtree            }            else            {              printf ("%d\n", p->data);      Interview node P              p=null;            }          } while ((top>0));     }

? ? ? B Clue binary tree:? ? ? ? the node structure diagram of the threaded binary tree: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /southeast "/>????? Description of the node type of the threaded binary tree:

      typedef struct NODE      {        DataType data;        struct node *lchild, *rchild;       Left and right child pointer        int Ltag, Rtag;                     Left and right clue      }tbintnode;         Node type      typedef tbintnode *tbintree;
? ? ? ?in a threaded binary tree. The sufficient and necessary condition for a node to be a leaf node is that both the left and right signs are 1."???" in order to lead a binary tree and its corresponding clue chain list for example:? ? ? ? ? ? ? ? ? ? ? ?? ? ? (1) The algorithm of the sequential Clue binary tree:
       void Inorderthreading (Tbintree p)      {        if (p)        {          inorderthreading (p->lchild);   Left dial hand tree-threaded          if (p->lchild)            p->ltag=0;          else            p->ltag=1;          if (p->rchild)            p->rtag=0;          else            p->rtag=1;          if (* (PRE))      //If *p's precursor *pre exists          {            if (pre->rtag==1)              pre->rchild=p;            if (p->ltag==1)              p->lchild=pre;          }          pre=p;                         The second pre is the inorderthreading precursor of the next interview node          (p->rchild);   Right subtree thread        }      }
? ? ? ?
? ?? (2) under the middle sequence clue binary tree, the node P's successor node has the following two kinds of conditions:? ? ? the right sub-tree of the ① node P is empty. Then the right child pointer field of P is the right clue, which points directly to the successor node of node p.
? ? ? The right sub-tree of the ② node p is not empty. Then the algorithm is based on the sequence traversal. The successor to P must be the 1th traversed node in its right subtree.
? ? ??The algorithm to find the successor node of the middle sequence Clue binary tree:
    Tbintnode *inordersuc (Bithrtree p)    {       Tbintnode *q;       if (p->rtag==1)   //① case         return p->rchild;       else            //② case       {         q=p->rchild;         while (q->ltag==0)           q=q->lchild;         return q;       }    }
? ? ? The algorithm for finding the precursor node of the binary tree in the middle order clue:
    Tbintnode *inorderpre (Bithrtree p)    {       Tbintnode *q;       if (p->ltag==1)         return p->lchild;       else       {         q=p->lchild;         Start looking for while (q->rtag==0) from *p's left child           q=q->rchild;         return q;       }    }
? ? ? (3) An algorithm for traversing a binary tree with sequential cues
    void Traversinorderthrtree (Bithrtree p)    {      if (p)      {while        (p->ltag==0)          p=p->lchild;        while (p)        {          printf ("%c", p->data);          P=INORDERSUC (P);}}    

Summary of binary tree traversal algorithm

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.