a binary tree linked list is used as the storage structure to complete the establishment of the two-fork tree, the operation of the total number of leaves and nodes by the sequence, sequence and order and the operations of the hierarchy traversal.
#include <iostream>
#include <cstdio>
#include <stdlib.h>
using namespace Std;
typedef int ELEMTYPE;
typedef struct BITNODE
{
Elemtype data;//Data domain
struct bitnode* lchild,*rchild; Left and right sub-tree domains;
}bitnode,*bitree;
int Create (Bitree *t)
{
Elemtype ch;
Elemtype temp;
scanf ("%d", &ch);
Temp=getchar ();
if (ch==-1)
{
*t=null;
}
Else
{
*t= (bitree) malloc (sizeof (Bitnode));
if (! ( *T))
{
Exit (-1);
}
Else
{
(*t)->data=ch;
printf ("Please enter the value of the left node of%d", ch);
Create (& (*t)->lchild);
printf ("Please enter the value of the right node of%d", ch);
Create (& (*t)->rchild);
}
}
return 1;
}
void Traverse (Bitree T)//Pre-order traversal binary tree
{
if (null==t)
{
Return
}
Else
{
printf ("%d", t->data);
Traverse (T->lchild);
Traverse (T->rchild);
}
}
Middle sequence Traversal binary tree
void Midtraverse (Bitree T)
{
if (t==null) {return;}
Midtraverse (T->lchild);
printf ("%d", t->data);
Midtraverse (T->rchild);
}
Sequential traversal of binary tree
void Lastraverse (Bitree T)
{
if (t==null) {return;}
Lastraverse (T->lchild);
Lastraverse (T->rchild);
printf ("%d", t->data);
}
Finding the depth of a binary tree
int Treedeep (Bitree T)
{
int deep=0;
if (T)
{
int Leftdeep=treedeep (t->lchild);
int Rightdeep=treedeep (t->rchild);
deep=leftdeep>=rightdeep?leftdeep+1:rightdeep+1;
}
return deep;
}
Finding the number of leaf nodes in binary tree
int Leafcount (Bitree t,int &num)
{
if (T)
{
if (t->lchild==null&&t->rchild==null)
{
num++;
}
Leafcount (T->lchild,num);
Leafcount (T->rchild,num);
}
return num;
}
int main ()
{
Bitree T;
Bitree *p= (bitree*) malloc (sizeof (bitree));
int deepth=0,num=0;
printf ("Please enter the value of the first node, 1 means no leaf node: \ n");
Create (&T);
printf ("Sequential traversal of binary tree: \ n");
Traverse (T);
printf ("\ n");
printf ("middle order traversal binary tree: \ n");
Midtraverse (T);
printf ("\ n");
printf ("Post-secondary traversal of binary tree: \ n");
Lastraverse (T);
printf ("\ n");
Deepth=treedeep (T);
printf ("Depth of tree:%d\n", deepth);
printf ("\ n");
Leafcount (T,num);
printf ("The number of leaf nodes in a binary tree is:%d\n", num);
printf ("\ n");
return 0;
}