đây là code của em trong khi thực hành cây nhị phân tìm kiếm, em gặp phải vấn đề code của hàm xoá số nguyên tố trong cây(removenguyento), mong anh chị sửa lỗi ngữ nghĩa dùm!
còn đây là code tách ra:
PHP Code:
// cây nhị phân tìm kiếm.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
typedef struct tagnode
{
int data;
struct tagnode *R;
struct tagnode *L;
}node;
typedef node* tree;
void creattree(tree &t)
{
t = NULL;
}
node* creatnode(int x)
{
node *p;
p= new node;
p->data = x;
p->L = p->R = NULL;
return p;
}
void addnode(tree &t,node *p)
{
if(t == NULL)
t = p;
else
{
if(t->data > p->data)
addnode(t->L,p);
else
if(t->data < p->data)
addnode(t->R,p);
else
exit(1);
}
}
void changeminR(tree &p,tree &t)
{
if(t->L != NULL)
changeminR(p,t->L);
else
{
p->data = t->data;
p=t;
t=t->R;
}
}
void removenode(tree &t,int x)
{
if(t != NULL)
{
if(t->data < x)
removenode(t->R,x);
else
{
if(t->data > x)
removenode(t->L,x);
else//tim thay x trong cay
{
node *p;
p=t;//t,p cung tro toi 1 phan tu
if(t->L == NULL)//co 1 nut con
t=t->R;
else
{
if(t->R == NULL)//co 1 nut con
t=t->L;
else//co 2 nut con
changeminR(p,t->R);
}
delete(p);
}
}
}
else
printf("khong tim thay phan tu de xoa!");
}
node* searchnode(tree t,int x)
{
if(t != NULL)
if(t->data == x)
return t;
else
if(t->data < x)
return searchnode(t->R,x);
else
return searchnode(t->L,x);
return NULL;
}
node* nodemax(tree t)
{
if(t != NULL)
if(t->R == NULL)
return t;
else
return nodemax(t->R);
return NULL;
}
node* nodemin(tree t)
{
if(t != NULL)
if(t->L == NULL)
return t;
else
return nodemin(t->L);
return NULL;
}
void inputtree(tree &t)
{
int x;
node *p;
creattree(t);
printf("moi ban nhap cac phan tu cho cay ket thuc bang so 0:\n");
do
{
scanf("%3d",&x);
if(x == 0)
break;
p = creatnode(x);
addnode(t,p);
}while(x != 0);
}
int demnode(tree t)
{
int dem =0;
if(t!=NULL)
dem = 1 + demnode(t->L) + demnode(t->R);
return dem;
}
int sumnode(tree t)
{
int sum = 0;
if(t!=NULL)
sum = t->data + sumnode(t->L) + sumnode(t->R);
return sum;
}
int isnodechan(int x)
{
if(x%2 == 0)
return 1;
else
return 0;
}
int demnodechan(tree t)
{
int dem=0;
if(t!=NULL)
if(isnodechan(t->data) == 1)
dem = 1 + demnodechan(t->L) + demnodechan(t->R);
else
dem = demnodechan(t->L) + demnodechan(t->R);
return dem;
}
int sumnodechan(tree t)
{
int sum=0;
if(t!=NULL)
if(isnodechan(t->data) == 1)
sum = t->data + sumnodechan(t->L) + sumnodechan(t->R);
else
sum = sumnodechan(t->L) + sumnodechan(t->R);
return sum;
}
int isnutla(tree t)//kiem tr co phai nut la hay khong
{
if((t != NULL) && (t->L == NULL) && (t->R == NULL))
return 1;
else
return 0;
}
int demnodela(tree t)//dem nut la
{
int dem = 0;
if(t != NULL)
{
if(isnutla(t) == 1)
dem++;
else
dem = demnodela(t->L) + demnodela(t->R);
}
return dem;
}
int sumnodela(tree t)//tong cac nut la
{
int sum=0;
if(t != NULL)
if(isnutla(t) == 1)
sum = sum + t->data;
else
sum = sumnodela(t->L) + sumnodela(t->R);
return sum;
}
int isnode1con(tree t)
{
if(t != NULL)
if(((t->L == NULL) && (t->R != NULL)) || ((t->L != NULL) && (t->R == NULL)))
return 1;
return 0;
}
int demnode1con(tree t)
{
int dem=0;
if(t != NULL)
if(isnode1con(t) == 1)
dem = 1 +demnode1con(t->L) + demnode1con(t->R);
else
dem = demnode1con(t->L) + demnode1con(t->R);
return dem;
}
int sumnode1con(tree t)
{
int s=0;
if(t != NULL)
if(isnode1con(t) == 1)
s = t->data + sumnode1con(t->L) + sumnode1con(t->R);
else
s = sumnode1con(t->L) + sumnode1con(t->R);
return s;
}
int isnode2con(tree t)
{
if((t!=NULL) && (t->L != NULL) && (t->R != NULL))
return 1;
else
return 0;
}
int demnode2con(tree t)
{
int dem=0;
if(t!=NULL)
if(isnode2con(t)==1)
dem = 1 + demnode2con(t->L) + demnode2con(t->R);
else
dem = demnode2con(t->L) + demnode2con(t->R);
return dem;
}
int sumnode2con(tree t)
{
int sum = 0;
if(t!=NULL)
if(isnode2con(t)==1)
sum = t->data + sumnode2con(t->L) + sumnode2con(t->R);
else
sum = sumnode2con(t->L) + sumnode2con(t->R);
return sum;
}
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int hight(tree t)
{
int h=0;
if(t!=NULL)
{
h++;
h = h + max(hight(t->L),hight(t->R));
}
return h;
}
int demlever(tree t,int x)
{
int muc=0;
if(t->data < x)
{
muc=1 + demlever(t->R,x);
}
else
if(t->data > x)
{
muc = 1 + demlever(t->L,x);
}
return muc;
}
int isnguyento(int x)
{
int i;
if(x>=2)
{
for(i=2;i<x;i++)
if(x%i == 0)
break;
if(x==i)
return 1;
}
return 0;
}
void removenguyento(tree &t)
{
if(t!=NULL)
{
if(isnguyento(t->data)==1)
removenode(t,t->data);
removenguyento(t->L);
removenguyento(t->R);
}
}
void outRNL(tree t)
{
if(t != NULL)
{
outRNL(t->R);
printf("%3d",t->data);
outRNL(t->L);
}
}
void outLNR(tree t)
{
if(t != NULL)
{
outLNR(t->L);
printf("%3d",t->data);
outLNR(t->R);
}
}
void outNRL(tree t)
{
if(t != NULL)
{
printf("%3d",t->data);
outNRL(t->R);
outNRL(t->L);
}
}
void outNLR(tree t)
{
if(t != NULL)
{
printf("%3d",t->data);
outNLR(t->L);
outNLR(t->R);
}
}
void main()
{
tree t;
node *p;
int x,k;
inputtree(t);
printf("\ncay duoc in ra kieu LNR:\n");
outLNR(t);
printf("\nso nut trong cay la: %d ",demnode(t));
printf("\ntong cac nut trong cay la: %d ",sumnode(t));
printf("\nso nut chan trong cay la: %d ",demnodechan(t));
printf("\ntong cac nut chan trong cay la: %d ",sumnodechan(t));
printf("\nso nut la trong cay la: %d ",demnodela(t));
printf("\ntong cac nut la trong cay la: %d ",sumnodela(t));
printf("\nso nut 1 con trong cay la: %d ",demnode1con(t));
printf("\ntong so nut 1 con trong cay la: %d ",sumnode1con(t));
printf("\nso nut 2 con trong cay la: %d ",demnode2con(t));
printf("\ntong so nut 2 con trong cay la: %d ",sumnode2con(t));
printf("\nchieu cao cua cay la: %d",hight(t));
printf("\nkhoa lon nhat cua cay la:");
if(nodemax(t) == NULL)
printf("cay rong!");
else
printf("%d",nodemax(t)->data);
printf("\nkhoa nho nhat cua cay la:");
if(nodemin(t) == NULL)
printf("cay rong!");
else
printf("%d",nodemin(t)->data);
removenguyento(t);
printf("\ncay duoc in ra kieu LNR sau khi xoa nguyen to:\n");
outLNR(t);
printf("\nmoi ban nhap phan tu muon xoa:\n");
scanf("%d",&x);
removenode(t,x);
printf("cay duoc in ra kieu LNR:\n");
outLNR(t);
printf("\nmoi ban nhap phan tu muon xac dinh muc hay do sau:\n");
scanf("%d",&k);
p = searchnode(t,k);
if(p == NULL)
printf("khong tim thay!");
else
{
printf("tim thay\n");
printf("muc hay do sau cua phan tu vua nhap la: %d",demlever(t,k));
}
getch();
}
PHP Code:
int isnguyento(int x)
{
int i;
if(x>=2)
{
for(i=2;i<x;i++)
if(x%i == 0)
break;
if(x==i)
return 1;
}
return 0;
}
void removenguyento(tree &t)
{
if(t!=NULL)
{
if(isnguyento(t->data)==1)
removenode(t,t->data);
removenguyento(t->L);
removenguyento(t->R);
}
}
Comment