Ý TƯỞNG:
CHƯƠNG TRÌNH MINH HOẠ
Code:
//Dem nut co bac day du tren cay
unsigned int Count(Tree T){
if(Empty(T))
return 0;
else
if(T->Left==NULL || T->Right==NULL)
return 0;
else
return 1 + Count(T->Left) + Count(T->Right);
}
CHƯƠNG TRÌNH MINH HOẠ
Code:
#include <conio.h>
#include <stdio.h>
#include <alloc.h>
typedef struct Node{
int Data;
Node*Left;
Node*Right;
};
typedef Node*Tree;
//Tao cay rong
Tree Make_Null(){
Tree T;
T=NULL;
return T;
}
//Tao node
Node*Make_Node(int X){
Node*P=(Node*)malloc(sizeof(Node));
P->Data=X;
P->Left=NULL;
P->Right=NULL;
return P;
}
//Kiem tra cay rong
char Empty(Tree T){
return T==NULL;
}
//Chen mot Node vao cay nhi phan
void Insert(Tree &T,Node*P){
if(Empty(T))
T=P;
else{
if(T->Data>P->Data){
if(T->Left)
Insert(T->Left,P);
else
T->Left=P;
}
else
if(T->Right)
Insert(T->Right,P);
else
T->Right=P;
}
}
//Nhap cay
void Input(Tree &T){
int X;
printf("Nhap cay nhi phan");
printf("\nNhap -1 de ket thuc\n");
do{
scanf("%d",&X);
if(X!=-1)
Insert(T,Make_Node(X));
}while(X!=-1);
}
//Dem nut co bac day du tren cay
unsigned int Count(Tree T){
if(Empty(T))
return 0;
else
if(T->Left==NULL || T->Right==NULL)
return 0;
else
return 1 + Count(T->Left) + Count(T->Right);
}
//Ham chinh
void main(){
//clrscr();
int X;
Tree T=Make_Null();
Input(T);
printf("\n\nTong so mut co bac day du:%d",Count(T));
getch();
}