C language to create a binary tree traversal, depth, leaf node

Introduction: Re-read it again binary tree, found a lot of problems these days to check a lot of information, such as why the time to create the incoming pointer to a pointer (the first since a similar swap (int a, int b) you pass a and b is a value not to change the function pointer passed out ab value,

This create the same type creates a BitNode * pointer p in the main he is NULL, the function passed done after the end of a series of operations function to recover, p outside the function is still NULL, so you have to pass the pointer modifying the pointer value of p)

#include <stdio.h>
#include <stdlib.h>
typedef int elemType;
typedef struct BitNode
{
elemType Data;
struct BitNode * Lchild;
struct BitNode * Rchild;
} BitNode, * BiTree;
void creatBiTree (BiTree T *);
void TraverseBitree (BiTree T);
void InOrderBitree (BiTree T);
void PostOrderBitree (BiTree T);
int leafCount (BiTree T, int * NUM);
int Treedeep (BiTree T);
void PrintTree (BiTree T, int H);
int main ()
{
BiTree T = NULL;
int NUM = 0, Deep;
the printf ( "enter a value of the first node, -1 indicates no leaf node: \ n-");
creatBiTree (& T);
the printf ( "first preorder is: ");
TraverseBitree (T);
the printf (" \ n-");

printf ( "preorder is:");
InOrderBitree (T);
printf ( "\ n-");

printf ( "postorder is:");
PostOrderBitree (T);
printf ( "\ n-");

the printf ( "leaf node has:");
leafCount (T, & NUM);
the printf ( "\ n-");
the printf ( "the number of leaf nodes is:% D \ n-', NUM);

Deep = Treedeep (T);
printf ( "binary depth is: D% \ n-", deep);
deep =. 1;
PrintTree (T, deep);

}
// create a binary tree by preorder
void creatBiTree (BiTree * T)
{
elemType CH;
Scanf ( "% D", & CH);
IF (CH == -1)
{
* T = NULL;
}
the else
{
* T = ; (BiTree) the malloc (the sizeof (BitNode))
(! (* T)) IF Exit (-1);
(* T) -> Data = CH;
the printf ( "% input left child node d:", ch) ;
creatBiTree (& (* T) -> Lchild);
the printf ( "% d input of the right child:", CH);
creatBiTree (& (* T) -> Rchild);
}

}
//先序遍历
void TraverseBitree(BiTree T)
{
if(T!=NULL)
{
printf("%d ",T->data);
TraverseBitree(T->Lchild);
TraverseBitree(T->Rchild);
}

}
// inorder traversal
void InOrderBitree (BiTree T)
{
IF (T = NULL!)
{
InOrderBitree (T-> Lchild);
the printf ( "% D", T-> Data);
InOrderBitree (T-> Rchild);
}
}
// subsequent traversal
void PostOrderBitree (BiTree T)
{
IF (T = NULL!)
{
PostOrderBitree (T-> Lchild);
PostOrderBitree (T-> Rchild);
the printf ( "% D", T-> Data);
}
}
// leaf node number
int leafCount (BiTree T, int * NUM)
{

IF (T! = NULL)
{
// IF (T-> Rchild-> T-Data == -1 &&> Lchild-> Data == -1) // this does not work

if (T-> Rchild == NULL && T-> Lchild == NULL)
{
(* NUM) ++;
the printf ( "% D", T-> Data); // leaf node output
}
leafCount (T-> Lchild, NUM);
leafCount (T- > Rchild, NUM);
}
// return NUM;
}
// binary tree depth
int Treedeep (BiTree T)
{
int deep = 0;
IF (T)
{
int leftdeep Treedeep = (T-> Lchild);
int = Treedeep rightdeep (T-> Rchild);
Deep = leftdeep> = rightdeep leftdeep +. 1: rightdeep +. 1;?
}
return Deep;
}
// print binary tree
void PrintTree (BiTree T,you h)
{
you i;
if(T==NULL) return ;
PrintTree(T->Rchild,h+1);
for(i=0;i<h;i++)
{
printf(" ");
}
printf("%d\n",T->data);
PrintTree(T->Lchild,h+1);
}

 

Guess you like

Origin www.cnblogs.com/cocobear9/p/12549673.html