1#include <stdio.h>2#include <stdlib.h>3 //* * * * * * Two fork tree two-linked list storage indicates * * * *// 4typedefstructBinode5 { 6 Chardata; 7 structBinode *lchild, *Rchild; 8}binode, *Bitree; 9 Ten //* * * * * The value of the node in the binary tree is entered in order (one character), and the empty characters represents the two-fork tree represented by the empty tree two-linked list t*****// One voidCreatebitree (Bitree &T) A { - Charch; -scanf"%c", &ch); the if(ch = =' ') - { -T =NULL; - } + Else - { + if(! (T = (Binode *) malloc (sizeof(Binode)))) A { at return; - } -T->data = ch;//Generating root nodes -Createbitree (T->lchild);//constructing the left sub-tree -Createbitree (T->rchild);//Constructing the right sub-tree - } in - return; to } + - //* * * * * sequential traversal of binary tree * * * *// the voidPreordertraverse (bitree T) * { $ if(!T)Panax Notoginseng { - return;//If T is an empty tree, it is returned directly the } +printf"%c", T->data);//Access root node APreordertraverse (T->lchild);//First order traversal left subtree thePreordertraverse (T->rchild);//first-order traversal of the right sub-tree + - return; $ } $ - //* * * * sequential traversal of binary tree * * * *// - voidInordertraverse (bitree T) the { - if(!T)Wuyi { the return;//If T is an empty tree, it is returned directly - } WuInordertraverse (T->lchild);//Middle sequence traversal left subtree -printf"%c", T->data);//Access root node AboutInordertraverse (T->rchild);//the middle sequence traverses the right sub-tree $ - return; - } - A //* * * * * After the second traverse tree * * *// + voidPostordertraverse (bitree T) the { - if(!T) $ { the return;//If T is an empty tree, it is returned directly the } thePostordertraverse (T->lchild);//To traverse the left subtree thePostordertraverse (T->rchild);//To traverse the right sub-tree -printf"%c", T->data);//Access root node in the return; the } About the intMainvoid) the { the Bitree T; +printf"Enter the value (character) of the node in the binary tree in order of precedence, and the empty characters represents the empty tree: \ n"); - Createbitree (T); the Bayiprintf"The result of the first order traversal is:"); the Preordertraverse (T); theprintf"\ n"); - -printf"The result of the middle sequence traversal is:"); the Inordertraverse (T); theprintf"\ n"); the theprintf"The post-order traversal results are:"); - Postordertraverse (T); theprintf"\ n"); the the return 0; 94}
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