Small turtle structure and algorithm-----binary tree construction and pre-sequence traversal

Source: Internet
Author: User

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

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.