Binary Tree construction and traversal of C program implementation code

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>

Typedef struct Node
{
Char value;
Struct node * leftchild;
Struct node * rightchild;
} Treenode;

// Construct a binary tree based on the middle and back Sequence

Treenode * createfrommidpst (char mid [], int midleft, int midright, char PST [], int pstleft, int pstright)
{
Treenode * node = (treenode *) malloc (sizeof (treenode ));
Int midleft1, midright1, pstleft1, pstright1, rootpos = 0;

If (midright-midleft! = Pstright-pstleft)
{
Printf ("error: the number of nodes in the input middle and back sequence is different! \ N ");
Return node;
}

For (; Mid [rootpos]! = PST [pstright]; rootpos ++ );

Node-> value = PST [pstright];
Node-> leftchild = NULL;
Node-> rightchild = NULL;

If (rootpos! = Midleft)

{
Midright1 = rootPos-1;
Pstright1 = pstleft + (midRight1-midLeft );
Node-> leftchild = createfrommidpst (MID, midleft, midright1, Pst, pstleft, pstright1 );
}
If (rootpos! = Midright)
{
Midleft1 = rootpos + 1;
Pstleft1 = pstleft + rootpos-midleft;
Node-> rightchild = createfrommidpst (MID, midleft1, midright, Pst, pstleft1, pstRight-1 );
}

Return node;
}

// Construct a binary tree based on the central and forward Sequences

Treenode * createfrommidpre (char mid [], int midleft, int midright, char pre [], int preleft, int preright)
{
Treenode * node = (treenode *) malloc (sizeof (treenode ));
Int midleft1, midright1, preleft1, preright1, rootpos = 0;

If (midright-midleft! = Preright-preleft)
{
Printf ("error: the number of nodes in the input middle sequence is different from that in the front sequence! \ N ");
Return node;
}

Node-> value = pre [preleft];
Node-> leftchild = NULL;
Node-> rightchild = NULL;

For (; Mid [rootpos]! = Pre [preleft]; rootpos ++ );

If (rootpos! = Midleft)
{
Midright1 = rootPos-1;
Preright1 = rootpos-midleft + preleft;
Node-> leftchild = createfrommidpre (MID, midleft, midright1, pre, preleft + 1, preright1 );
}
If (rootpos! = Midright)
{
Midleft1 = rootpos + 1;
Preleft1 = rootpos-midleft + preleft + 1;
Node-> rightchild = createfrommidpre (MID, midleft1, midright, pre, preleft1, preright );
}

Return node;
}

// Traverse binary trees in the forward order
Void pretraverse (treenode * root)
{
If (root = NULL)
Return;
Printf ("% C", root-> value );
Pretraverse (root-> leftchild );
Pretraverse (root-> rightchild );
Return;
}

// Traverse a binary tree in the middle order

Void midtraverse (treenode * root)
{
If (root = NULL)
Return;
Midtraverse (root-> leftchild );
Printf ("% C", root-> value );
Midtraverse (root-> rightchild );
Return;
}

// Traverse Binary Trees in descending order

Void psttraverse (treenode * root)
{
If (root = NULL)
Return;
Psttraverse (root-> leftchild );
Psttraverse (root-> rightchild );
Printf ("% C", root-> value );
Return;
}

Int main ()
{
Treenode * root1, * root2;
Char preseq [20], midseq [20], pstseq [20];
Int I = 0, numnode;

Printf ("Enter the total number of nodes: \ n ");
Scanf ("% d", & numnode );
Fflush (stdin );

Printf ("Please input the first sequence: \ n ");
Do
{
Scanf ("% C", preseq + I );
I ++;
}
While (I! = Numnode );
Fflush (stdin );

I = 0;
Printf ("Please input the central sequence: \ n ");
Do
{
Scanf ("% C", midseq + I );
I ++;
}
While (I! = Numnode );
Fflush (stdin );

I = 0;
Printf ("Please input the ordinal sequence: \ n ");
Do
{
Scanf ("% C", pstseq + I );
I ++;
}
While (I! = Numnode );
Fflush (stdin );

Root1 = createfrommidpre (midseq, 0, I-1, preseq, 0, I-1 );

Printf ("\ n ");
Printf ("preordered sequence: \ n ");
Pretraverse (root1 );
Printf ("\ n ");
Printf ("ordinal sequence: \ n ");
Midtraverse (root1 );
Printf ("\ n ");
Printf ("post sequence: \ n ");
Psttraverse (root1 );
Printf ("\ n ");

Root2 = createfrommidpst (midseq, 0, I-1, pstseq, 0, I-1 );

Printf ("\ n ");
Printf ("preordered sequence: \ n ");
Pretraverse (root2 );
Printf ("\ n ");
Printf ("ordinal sequence: \ n ");
Midtraverse (root2 );
Printf ("\ n ");
Printf ("post sequence: \ n ");
Psttraverse (root2 );
Printf ("\ n ");

Return 1;
}

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.