You are on page 1of 3

#include<iostream>

#include<string.h>
#include<string>
#include<stdlib.h>
#include<time.h>
#include<math.h>
using namespace std;
int mang[100000];
struct NODE{
int Info;
int count;
NODE* Next;
};
typedef struct NODE* BST;

NODE* taonode(int x){


NODE* p=new NODE;
p->Info=x;
p->Next=NULL;
p->count=1;
return p;
}
bool CHECK(BST &bst,NODE* p){
for(NODE*k=bst;k!=NULL;k=k->Next){
if(k->Info==p->Info){
p->count++;
return false;
}
}
return true;
}
void ADDNODETOTREE(BST &bst,NODE* p){
if(bst==NULL){
bst=p;
}
else{
if(p->Info>bst->Info){
p->Next=bst;
bst=p;
}
else{
NODE*k=bst;
bool TF=true;
while(k->Next!=NULL){
int x=k->Next->Info;
int y=p->Info;
if(x<y){
p->Next=k->Next;
k->Next=p;
TF=false;
break;
}
k=k->Next;
}
if(TF) k->Next=p;
}
}
}
void CAU1(BST &bst){
int n;
do{
cout<<"Nhap so nguyen: ";
cin>>n;
if(n<0) break;
NODE*p=taonode(n);
ADDNODETOTREE(bst,p);

}
while(true);
}
//--------------------------------
bool songuyento(int n){
if(n<2) return false;
for(int i=2;i<=sqrt(n);++i){
if(n%i==0) return false;
}
return true;
}
//--------------------------------
bool doixung(int n){
int h=n;
int dn=0;
while(n>0){
dn=dn*10+n%10;
n/=10;
}
return(dn==h);
}
//---------------------------
int CAU2(BST bst){
if(bst!=NULL){
int dem=CAU2(bst->Next);
int x=bst->Info;
if(songuyento(x)==true && doixung(x)==true) return dem+1;
return dem;
}else return 0;
}
void IN(BST bst){
for(NODE*k=bst;k!=NULL;k=k->Next) cout<<k->Info<<" ";
}
//--------------------------
int CAU3(BST bst){
int dem=0;
while(bst!=NULL){
++dem;
bst=bst->Next;
}
return dem;
}
//--------------------
void CAU4(BST bst){
while(bst!=NULL){
if(songuyento(bst->Info)==true && doixung(bst->Info)==true){
bst=bst->Next;
}
else{
break;
}
}
if(bst==NULL){
cout<<"khong co gia tri nao nua";
return;
}
BST tree=bst;
while(bst->Next!=NULL){
int x=bst->Next->Info;
if(songuyento(x)==true && doixung(x)){
bst->Next=bst->Next->Next;
continue;
}
bst=bst->Next;
}
IN(tree);
}
//-------------------------------------------
int CAU5(BST bst){
int maxn=-1;
while(bst!=NULL){
int x=bst->Info;
if(songuyento(x)==true && doixung(x)==true){
maxn=max(maxn,x);
}
bst=bst->Next;
}
return maxn;
}
//--------------------------------
int main(){
BST bst=new NODE;
bst=NULL;
cout<<"CAU1"<<endl;
CAU1(bst);
cout<<"CAU2"<<endl;
cout<<CAU2(bst)<<endl;
cout<<"CAU3"<<endl;
cout<<"So node chi co cay con ben trai ma khong co cay con ben
phai:"<<CAU3(bst)-1<<endl;
cout<<"CAU4"<<endl;
CAU4(bst);
cout<<endl;
cout<<"CAU5"<<endl;
cout<<"Gia tri so nguyen to Palindrome lon nhat:"<<CAU5(bst);
}

You might also like