Current location - Education and Training Encyclopedia - Educational Knowledge - Known binary trees are stored in the form of binary linked lists. Write an algorithm to calculate the nodes with binary tree degree of 0. The degree is 1. 2 degrees.
Known binary trees are stored in the form of binary linked lists. Write an algorithm to calculate the nodes with binary tree degree of 0. The degree is 1. 2 degrees.
# include & ltstdio.h & gt// header file

# include & ltstdlib.h & gt

Typedef structure bit node

{

Char data;

struct BiTNode *lchild,* rchild

}

BiTNode, * BiTree// defines the node type.

BiTree CreateBiTree()// Create a tree

{

char p; BiTree T;

scanf("%c ",& ampp);

if(p== ' ')

T = NULL

other

{

t =(bit node *)malloc(sizeof(bit node)); //Make room for nodes

t->; Data = p;;

t->; l child = create bitree();

t->; rchild = create bitree();

}

Return (t);

}

Int Nochild(BiTree T)// leaf node

{

if(T==NULL)

return(0);

if(T->; lchild = = NULL & amp& ampt->; rchild==NULL)

Returns (1);

return(no child(T-& gt; lchild)+no child(T-& gt; rchild));

}

Int Onechild(BiTree T)// The degree is 1.

{

int n = 0;

if(T==NULL)

return(0);

if((T-& gt; lchild = = NULL & amp& ampt->; Kid! = NULL)| |(T-& gt; Kid. = NULL & amp& ampt->; rchild==NULL))

n = 1;

return(one child(T-& gt; lchild)+one child(T-& gt; rchild)+n);

}

Int Twochild(BiTree T)// degree 2

{

int n = 0;

if(T==NULL)

return(0);

if(T->; Kid. = NULL & amp& ampt->; Kid! = empty)

n = 1;

return(two child(T-& gt; lchild)+two child(T-& gt; rchild)+n);

}

Void main()// main function

{

BiTree Ta

Printf ("Please create a tree: \ n");

ta = create bitree();

Printf ("The number of leaves is %d, the number with degree 1 is %d, and the number with degree 2 is% d", with no children (ta), one son (ta) and two sons (ta));

}