#include<stdio.h>
#include<conio.h>
typedef struct tree
{
int info;
tree *l;
tree *r;
};
tree *getnode(int x)
{
tree *p=new tree;
if(!p) return NULL;
p->info= x;
p->l=NULL;
p->r=NULL;
return p;
}
void addx(tree *&t, int x)
{
if(!t)
{
t=getnode(x);
return;
}
if(t->info ==x) return;
if(t->info>x)
addx(t->l,x);
else
addx(t->r,x);
}
void list(tree *&t)
{
int n,x;
do
{
printf("\nNhap n: ");
scanf("%d",&n);
if(n<0) printf("\nNhap sai Nhap lai");
}while(n<0);
for(int i=0; i<n; i++)
{
printf("\nNhap x: ");
scanf("%d",&x);
addx(t,x);
}
}
void NLR(tree *t)
{
if(!t) return;
printf("%5d",t->info);
NLR(t->l);
NLR(t->r);
}
void LNR(tree *t)
{
if(!t) return;
LNR(t->l);
printf("%5d",t->info);
LNR(t->r);
}
void LRN(tree *t)
{
if(!t) return;
LRN(t->l);
LRN(t->r);
printf("%5d",t->info);
}
tree *timx(tree *t, int x)
{
if(!t) return NULL;
if(t->info==x) return t;
if(t->info >x)
return(timx(t->l,x));
return(timx(t->r,x));
}
tree *timx2(tree *t, int x)
{
tree *p=t;
if(!t) return NULL;
while(p && p->info !=x)
{
if(p->info >x)
p=p->l;
else p=p->r;
}
return p;
}
void calltimx(tree *t)
{
int x;
printf("\nNhap x can tim: ");
scanf("%d",&x);
if(!t)
printf("\nCay rong");
else
{
tree *p=timx(t,x);
if(!p)
printf("\nKhong tim thay");
else printf("\nTim thay");
}
}
int ktsnt(int n)
{
if(n<2) return 0;
if(n==2) return 1;
for(int i =2; i*i<=n; i++)
if(n%i == 0) return 0;
return 1;
}
void ktam(tree *t)
{
if(!t) return;
if(t->info<0)
printf("%5d",t->info);
ktam(t->l);
ktam(t->r);
}
void ktduong(tree *t)
{
if(!t) return;
if(t->info>0)
printf("%5d",t->info);
ktduong(t->l);
ktduong(t->r);
}
void ktchan(tree *t)
{
if(!t) return;
if(t->info%2==0)
printf("%5d",t->info);
ktchan(t->l);
ktchan(t->r);
}
void ktle(tree *t)
{
if(!t) return;
if(t->info%2!=0)
printf("%5d",t->info);
ktle(t->l);
ktle(t->r);
}
void xuatsnt(tree *t)
{
if(!t) return;
if(ktsnt(t->info))
printf("%5d",t->info);
xuatsnt(t->l);
xuatsnt(t->r);
}
int tong(tree *t)
{
if(!t) return 0;
return t->info + tong(t->l) +tong(t->r);
}
int tongam(tree *t)
{
if(!t) return 0;
if(t->info <0)
return t->info + tongam(t->l) + tongam(t->r);
return tongam(t->l) + tongam(t->r);
}
int tongduong(tree *t)
{
if(!t) return 0;
if(t->info >0)
return t->info + tongduong(t->l) + tongduong(t->r);
return tongduong(t->l) + tongduong(t->r);
}
int tongchan(tree *t)
{
if(!t) return 0;
if(t->info %2 == 0)
return t->info + tongchan(t->l) + tongchan(t->r);
return tongchan(t->l) + tongchan(t->r);
}
int tongle(tree *t)
{
if(!t) return 0;
if(t->info %2 != 0)
return t->info + tongle(t->l) + tongle(t->r);
return tongle(t->l) + tongle(t->r);
}
int tongsnt(tree *t)
{
if(!t) return 0;
if(ktsnt(t->info))
return t->info + tongsnt(t->l) + tongsnt(t->r);
return tongsnt(t->l) + tongsnt(t->r);
}
int demnode(tree *t)
{
if(!t) return 0;
return 1+ demnode(t->l)+demnode(t->r);
}
int demnodela(tree *t)
{
if(!t) return 0;
if(t->l == NULL && t->r == NULL)
return 1+demnodela(t->l) + demnodela(t->r);
return demnodela(t->l) + demnodela(t->r);
}
int demnodeam(tree *t)
{
if(!t) return 0;
if(t->info <0)
return 1+ demnodeam(t->l)+ demnodeam(t->r);
return demnodeam(t->l)+ demnodeam(t->r);
}
int demnodeduong(tree *t)
{
if(!t) return 0;
if(t->info >0)
return 1+ demnodeduong(t->l)+ demnodeduong(t->r);
return demnodeduong(t->l)+ demnodeduong(t->r);
}
int demnodechan(tree *t)
{
if(!t) return 0;
if(t->info %2 == 0)
return 1 + demnodechan(t->l) + demnodechan(t->r);
return demnodechan(t->l) + demnodechan(t->r);
}
int demnodele(tree *t)
{
if(!t) return 0;
if(t->info %2 != 0)
return 1 + demnodele(t->l) + demnodele(t->r);
return demnodele(t->l) + demnodele(t->r);
}
int demnodesnt(tree *t)
{
if(!t) return 0;
if(ktsnt(t->info))
return 1 + demnodesnt(t->l) + demnodesnt(t->r);
return demnodesnt(t->l) + demnodesnt(t->r);
}
int max(tree *t)
{
tree *p=t;
while(p->r)
p=p->r;
return p->info;
}
int min(tree *t)
{
tree *p=t;
while(p->l)
p=p->l;
return p->info;
}
tree *nodemax(tree *t)
{
tree *p=t;
while(p->r)
p=p->r;
return p;
}
tree *nodemin(tree *t)
{
tree *p=t;
while(p->l)
p=p->l;
return p;
}
int demnodelonx(tree *t, int x)
{
if(!t) return 0;
if(t->info >x)
return 1+demnodelonx(t->l,x) + demnodelonx(t->r,x);
return demnodelonx(t->l,x) + demnodelonx(t->r,x);
}
int demnodenhox(tree *t, int x)
{
if(!t) return 0;
if(t->info <x)
return 1+demnodenhox(t->l,x) + demnodenhox(t->r,x);
return demnodenhox(t->l,x) + demnodenhox(t->r,x);
}
int Max(int a, int b)
{
return a>b?a:b;
}
int height(tree *t)
{
if(t)
return 1+ Max(height(t->l), height(t->r));
return 0;
}
int xuatxy(tree *t, int x, int y)
{
if(!t) return 0;
if(t->info>=x && t->info <=y)
return 1+xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
return xuatxy(t->l,x,y)+xuatxy(t->r,x,y);
}
void xuatxy1(tree *t)
{
int x,y;
printf("\nNhap x: ");
scanf("%d",&x);
printf("\nNhap y: ");
scanf("%d",&y);
printf("\nSo nut tu x den y trong cay la: %d",xuatxy(t,x,y));
}
void deltree(tree *&t)
{
if(!t) return;
deltree(t->l);
deltree(t->r);
delete t;
}
void main(void)
{
tree *t =NULL;
list(t);
printf("\nDuyet NLR: ");
NLR(t);
printf("\nDuyet LNR: ");
LNR(t);
printf("\nDuyet LRN: ");
LRN(t);
calltimx(t);
printf("\nCac so duong: ");
ktduong(t);
printf("\nCac so am: ");
ktam(t);
printf("\nCac so chan: ");
ktchan(t);
printf("\nCac so le: ");
ktle(t);
printf("\nCac so nguyen to: ");
xuatsnt(t);
printf("\nTong cac so duong trong cay: %d",tongduong(t));
printf("\nTong cac so le trong cay: %d",tongle(t));
printf("\nTong cac so chan trong cay: %d",tongchan(t));
printf("\nTong cac so am trong cay: %d",tongam(t));
printf("\nTong cac so nt trong cay: %d",tongsnt(t));
printf("\nTong so node tren cay: %d",demnode(t));
printf("\nTong so node la tren cay: %d",demnodela(t));
printf("\nTong cac so node duong trong cay: %d",demnodeduong(t));
printf("\nTong cac so node le trong cay: %d",demnodele(t));
printf("\nTong cac so node chan trong cay: %d",demnodechan(t));
printf("\nTong cac so node am trong cay:%d",demnodeam(t));
printf("\nTong cac so node nt trong cay: %d",demnodesnt(t));
printf("\nMax: %d",max(t));
printf("\nMin: %d",min(t));
int x;
printf("\nNhap x can tim: ");
scanf("%d",&x);
printf("\nTong cac node lon hon x: %d",demnodelonx(t,x));
printf("\nTong cac node nho hon x: %d",demnodenhox(t,x));
printf("\nChieu cao cua cay: %d",height(t));
xuatxy(t);
deltree(t);
if(t)
printf("\nCay da xoa !");
xuatxy1(t);
getch();
}
Không có nhận xét nào:
Đăng nhận xét