Establishment of Binary tree

Source: Internet
Author: User

#include "stdio.h"
#include "string.h"
#include "BiTNode.h"




Establishing two-pronged tree in the first order
Bitree procreate (Bitree T)
{
Char ch;
scanf ("%c", &ch);
if (ch = = ' # ')
{
T = NULL;
return NULL;
}


Else
{
T = (Bitnode *) malloc (sizeof (Bitnode));
if (! T
{
printf ("error!");
return NULL;
}


T->data = ch;
T->lchild = Procreate (T->lchild);
T->rchild = Procreate (T->rchild);
}
return T;
}
First-order traversal of binary tree
void preorder (Bitree T)
{
if (T)
{
printf ("%c", t->data);
Preorder (T->lchild);
Preorder (T->rchild);
}
}


Middle sequence Traversal binary tree
void Inorder (Bitree T)
{
if (T)
{
Inorder (T->lchild);
printf ("%c", t->data);
Inorder (T->rchild);
}
}
Sequential traversal of binary tree
void Postorder (Bitree T)
{
if (T)
{
Postorder (T->lchild);
Postorder (T->rchild);
printf ("%c", t->data);
}
}
Find the number of leaf nodes
int Sumleaf (Bitree T)
{
int sum = 0, M, N;
if (T)
{
if ((! T->lchild) && (! T->rchild))
sum + +;
m = Sumleaf (T->lchild);
sum + = m;
n = sumleaf (t->rchild);
sum + = n;
}
return sum;
}
Finding the depth of a binary tree
int Depth (Bitree T)
{
int dep = 0, DEPL, Depr;
if (! T) dep = 0;
Else
{
Depl=depth (T->lchild);
Depr=depth (T->rchild);
dep=1+ (DEPL>DEPR?DEPL:DEPR);
}
return DEP;
}


int main ()
{
Bitree T;
int sum, DEP;
printf ("Order to create a two-fork tree, please enter the sequence, empty use # instead of \ n");
t = Procreate (t);
printf ("Result of the first Order traversal:");
Preorder (T);
printf ("\ n the result of the sequence Traversal:");
Inorder (T);
printf ("\ nthe result of post-sequential traversal:");
Postorder (T);
printf ("\ n number of Leaves:");
Sum=sumleaf (T);
printf ("%d", sum);
Dep=depth (T);
printf ("\ n Depth:%d \ n", DEP);
return 0;

}


BiTNode.h's Content

#ifndef bitnode_h_included
#define Bitnode_h_included
#define Elemtype Char


Defining Data structures
typedef struct BITNODE
{
Elemtype data;
struct Bitnode *lchild, *rchild;
}bitnode,*bitree;






#endif//bitnode_h_included

Establishment of Binary tree

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.