Find the height of the binary tree, the number of nodes, traverse the binary tree

Source: Internet
Author: User
Tags strlen
#include "stdio.h"
#include "malloc.h"
#include "math.h"
#include "string.h"
#define MAX 100
void exit (int);
/*--------------------*/



int count; /* Record the number of two-degree nodes in the binary tree */
int flag; /* Indicates whether the binary tree is an AVL tree */



/*--------------------*/



typedef struct node{
Char D;
struct node *lchild,*rchild;
}tnode;

/*-----Build-----Through the sequencing sequence and sequencing sequence */



void Mktree (char in[],int is,int ie,char post[],int posts,int poste,tnode **r)
{
int i;
if (Is>ie | | posts>poste)
*r=null;
else{
*r=malloc (sizeof (tnode));
(*r)->d=post[poste];
for (i=is;i<=ie;i++)
if (post[poste]==in) {
Mktree (in,is,i-1,post,posts,posts+i-is-1,& (*r)->lchild);
Mktree (in,i+1,ie,post,posts+i-is,poste-1,& (*r)->rchild);
Break
}
if (I>ie) {
printf ("Error:input contains an error!/n");
Exit (9);
}
}
}



/*-----Pre-order Traversal binary Tree-----*/



void preorder (Tnode *r)
{
if (r) {
printf ("%c", r->d);
Preorder (R->lchild);
Preorder (R->rchild);
}
}

/*-----Find the number of two-degree nodes in the binary tree-----*/



void BNode (Tnode *r)
{
if (r) {
if (r->lchild&&r->rchild)
count++;
BNode (R->lchild);
BNode (R->rchild);
}
}



/*-----Find the number of leaves in the binary tree-----*/



int leaf (tnode *r)
{
if (r==null)
return 0;
Else
if (r->lchild==null && r->rchild==null)
return 1;
Else
Return Leaf (r->lchild) + leaf (r->rchild);
}



/*-----Find the height of the leaf in the binary tree-----*/



int Height (tnode *r)
{
int h1,h2;
if (r==null)
return 0;
else{
H1=height (R->lchild);
H2=height (R->rchild);
if (ABS (H1-H2) >1)
flag=1;
Return 1+ (H1&GT;H2?H1:H2);
}
}

void Main ()
{
Tnode *r;
int height;
Char Post[max],in[max];
printf ("Input inorder and Postorder:/n");
Gets (in);
Gets (post);
Mktree (In,0,strlen (in) -1,post,0,strlen (POST) -1,&r);
printf ("The preorder is as follows:/n");
Preorder (R);
printf ("/nthere is%dletters in the tree./n", Leaf (r));
count=0;
BNode (R);
printf ("/nthere is%d binarynode in the tree./n", count);
flag=0;
Height=height (R);
printf ("/nthe height of the tree is%d/n", height);
if (flag==1)
printf ("/nthis bintree is not a avl!/n");
Else
printf ("/nthis Bintree is an avl!/n");
}

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.