C-language recursive implementation of the first order, the middle order and the sequential traversal of the two-fork tree

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>//***** two fork tree two-fork list storage represents *****//typedef struct BINODE {char data;  struct Binode *lchild, *rchild;  }binode, *bitree;                                         Enter the value of the node in the binary tree in order of first order (one character), and the empty characters represents the two-fork tree t*****//void Createbitree (Bitree &t) represented by the empty tree construction two-linked list {      Char ch;      scanf ("%c", &ch);      if (ch = = ") {T = NULL; } else {if (! (          T = (Binode *) malloc (sizeof (Binode))) {return;                    } t->data = ch;     Generate root node Createbitree (t->lchild);     Constructs left subtree Createbitree (t->rchild);  Construct right subtree} return; }//***** first-order traversal of the binary tree *****//void Preordertraverse (Bitree T) {if (!                             T) {return;                  If T is an empty tree, return directly to the} printf ("%c", t->data);         Access root node Preordertraverse (t->lchild);       The first order traversal left subtree Preordertraverse (t->rchild);  The first sequence traverses the right subtree return; }//***** in sequence traversal binary tree *****//void Inordertraverse (Bitree T) {if (!                             T) {return;              If T is an empty tree, it is returned directly} inordertraverse (T->lchild);                  The middle sequence traverses the left subtree printf ("%c", t->data);              Access root node Inordertraverse (t->rchild);  The middle sequence traverses the right subtree return; }//***** post-traverse the binary tree *****//void Postordertraverse (Bitree T) {if (!                             T) {return;            If T is an empty tree, it is returned directly} postordertraverse (T->lchild);            Post-sequential traversal left subtree postordertraverse (t->rchild);                  Post-sequential traversal of the right subtree printf ("%c", t->data);  Access root node return;          } int main (void) {Bitree T;      printf ("Enter the values (characters) of the nodes in the binary tree in order of precedence, empty characters indicate empty trees: \ n");          Createbitree (T);      printf ("First-order traversal results are:");      Preordertraverse (T);       printf ("\ n");      printf ("Middle sequence traversal result is:");      Inordertraverse (T);       printf ("\ n");      printf ("Post-order traversal results are:"); PostordertraVerse (T);          printf ("\ n");      return 0;     }

Taking the following two-fork tree as an example, the values (characters) of the nodes in the binary tree are given in order to construct a two-fork tree according to the algorithm presented in this paper.


The order in which the characters are entered is:-+a space space *b space space-C space space D space Space/E space space F space space, you can verify the traversal algorithm provided in this article.

C-language recursive implementation of the first order, the middle order and the sequential traversal of the two-fork tree

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.