Construct a simple binary tree (c)

Source: Internet
Author: User

# Include "stdafx. H"

# Include "stdlib. H"
# Include "stdio. H"

Struct tree {
Char Info;
Struct tree * left;
Struct tree * right ;}
;

Struct tree * root;

Void print_tree (struct tree * r, int L );

Struct tree * stree (struct tree * root, struct tree * r, char info)
{
If (! R)
{
R = (struct tree *) malloc (sizeof (struct tree ));
If (! R)
{
Printf ("out of mem ");
Exit (0 );
}

R-> left = NULL;
R-> right = NULL;
R-> info = Info;

If (! Root) return R;
If (info <root-> info)
Root-> left = R;
Else
Root-> right = R;
Return R;
}
If (info <r-> info)
Stree (R, R-> left, Info );
Else
Stree (R, R-> right, Info );

Return root;
}

 

Int main (void)
{
Char s [80];
Root = NULL;
Do
{
Printf ("enter a letter: \ n ");
Gets (s );
Root = stree (root, root, * s );
}
While (* s );

Print_tree (root, 0 );

Return 0;
}

Void print_tree (struct tree * r, int L)
{
Int I;
If (! R) return;
Print_tree (R-> right, L + 1 );
For (I = 0; I <1; ++ I) printf ("");
Printf ("% C \ n", R-> info );
Print_tree (R-> left, l + 1 );
}

Compiled by vc6.0...

Related Article

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.