On the method of recursive construction of binary tree in subsequent sequence

Source: Internet
Author: User
Tags define null

One of the easiest ways to do this is to reverse input and then use the "root right left" sequence to build two-prong trees.

*********************************/

/**//**********gamesduan*********/

/**//**********bitreeinlast*********/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#define NULL 0
int n=0;

typedef struct BITNODE ... {//Tree structure
char data;
struct Bitnode *lchild,*rchild;
}bitnode;

Bitnode *createbitreeinpre ()//First Order recursive achievements
{
Bitnode *m;
Char ch;
scanf ("%c", &ch);
if (ch== ') m=null;
Else
{
if (!) ( m= (Bitnode *) malloc (sizeof (Bitnode)))
Exit (0);
m->data=ch;
M->lchild=createbitreeinpre ();
M->rchild=createbitreeinpre ();
}
return m;
}

Bitnode *createbitreeinlast1 ()///The first method, input in the form of reverse input, input as "C Empty B empty"
{
Bitnode *t;
Char ch;
scanf ("%c", &ch);
if (ch== ')
T=null;
Else
{
t= (Bitnode *) malloc (sizeof (Bitnode));
t->data=ch;
T->rchild=createbitreeinlast2 (); Create right subtree First
T->lchild=createbitreeinlast2 (); Then create Zuozi
}
return T;
}

void preorder (Bitnode *t)//first-order recursive output
{
if (t!=null)
{
printf ("%c", t->data);
Preorder (T->lchild);
Preorder (T->rchild);
}
}

void Main ()
{
Bitnode *t=new Bitnode;
T=createbitreeinpre (); First Order
T=createbitreeinlast1 (); After 1
Preorder (t);
}

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.