Title: Create a two-fork tree and output the number of layers where each character resides. If output is required
A on the first floor
B, C on the second floor
D, E on the third floor
The code is as follows:
#include <stdio.h>#include<stdlib.h>typedefstructBitnode {Chardata; structBitnode *lchild,*Rchild;} Bitnode,*Bitree;//Create a binary tree and contract the user to enter the data in the same way as the pre-order traversalvoidCreatebitree (Bitree *T) {CharC; scanf ("%c",&c); if(' '==c) { *t =NULL; } Else { *t = (Bitnode *)malloc(sizeof(Bitnode)); (*t)->data =C; Createbitree (& (*t)lchild); Createbitree (& (*t)rchild); }}voidVisitCharCintLevel ) {printf ("%c on level%d \ n", C,level);}//forward sequence traversal binary treevoidPreordertraverse (Bitree T,intLevel ) { if(t) {visit (t-data,level); Preordertraverse (T->lchild,level+1); Preordertraverse (T->rchild,level+1); }}intMain () {intLevel =1; Bitree T; T=NULL; Createbitree (&T); Preordertraverse (T,level); return 0;}
Operation Result:
Small turtle structure and algorithm-----binary tree construction and pre-sequence traversal