#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