vantuan2141
20-10-2011, 12:49 PM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
//CHUONG TRINH TAO VA SO SANH HAI CAY NHI PHAN
struct Tree2
{
int So;
Tree2 *Left,*Right;
};
typedef Tree2 *PTree2;
PTree2 Root1,Root2,p;
int nd,nd2,cv,k,h;
//==================================
//cap phat mot nut la cho cay nhi phan
PTree2 makenode (int x)
{
PTree2 p;
p= new Tree2;
(*p).So=x;
(*p).Left=NULL;
(*p).Right=NULL;
return (p);
}
//==================================
//tim mot nut co noi dung x
PTree2 search(PTree2 &root ,int x)
{
PTree2 p;
if((*root).So==x) return (root);
if(root==NULL) return NULL;
p=search((*root).Left,x);
if(p==NULL)
p=search(
(*root).Right,x);
return p;
}
//=================================
//Kiem tra 2 cay
int Test(PTree2 Root1,PTree2 Root2)
{
if(Root1 != NULL && Root2 != NULL)
if(Test((*Root1).Left,(*Root2).Right) && Test((*Root1).Right,(*Root2).Left))
return(1);
else return(0);
else
if(Root1==NULL && Root2==NULL)return(1);
else return(0);
}
//==================================
// chieu cao cua cay
int ChieuCao(PTree2 Root)
{
int trai,phai;
if (Root==NULL) return 0;
else
{
trai=ChieuCao((*Root).Left);
phai=ChieuCao((*Root).Right);
if(trai>phai) return (trai+1);
else return (phai+1);
}
}
//==================================
//ham tao cay nhi phan
void Create(PTree2 &root)
{
PTree2 k,h;
int nd,nd2,cv;
{
printf("\n NHAP NUT GOC:");
scanf("%d",&nd);
root=makenode(nd);
if (root!=NULL)
do {
printf("\n YOUR SECLECT:");
scanf("%d",&cv);
if(cv>0&&cv<4)
{
printf("\n Nhap nut cha:");
scanf("%d",&nd2);
p=search(root,nd2);
if (p==NULL)
{
printf ("\n Khong co nut cha");
}
else if(cv==1)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
if((*p).Left!=NULL)
{
k=(*p).Left;
(*p).Left=makenode(nd);
h=(*p).Left;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if((*p).Left==NULL)
(*p).Left=makenode(nd);
}
else if(cv==2)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
if((*p).Right!=NULL)
{
k=(*p).Right;
(*p).Right=makenode(nd);
h=(*p).Right;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if ((*p).Right==NULL)
(*p).Right=makenode(nd);
}
else if(cv==3)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
k=search(root,nd);
if(k==NULL)
{
printf("\n khong co nut nay");
}
else if((*k).Left!=NULL ||(*k).Right!=NULL)
{
printf("\n %d khong phai nut la khong xoa duoc",(*k).So);
}
else if(k==(*p).Left)
(*p).Left=NULL;
else if(k==(*p).Right)
(*p).Right=NULL;
}
}
}while (cv!=0);
}
}
//================================================== =============
main()
{
char chon;
//clrscr();
printf("\nXin Chao ^_^ ");
printf("\n\n\ HUONG DAN CACH NHAP CAY NHI PHAN");
printf("\n1. Chon 1 de nhap nut con ben trai");
printf("\n2. Chon 2 de nhap nut con ben phai");
printf("\n3. Chon 3 de xoa nut la");
printf("\n4. Chon 0 de dung nhap");
Root1=NULL;
Root2=NULL;
printf("\n\n NHAP CAY NHI PHAN THU I (seclect: 0,1,2,3)\n");
Create(Root1);
printf("\n NHAP CAY NHI PHAN THU II (seclect: 0,1,2,3)\n");
Create(Root2);
if(Test(Root1,Root2))
printf("\n\n --> HAI CAY NHI PHAN LA DANG CAU\n");
else printf("\n\n --> HAI CAY NHI PHAN KHONG DANG CAU VOI NHAU\n");
getch();
}
mình nhập cây nó cứ báo là ko có nút cha hoài,mong các bạn chỉ giúp mình chương trình còn thiếu sót gì hay sai chổ nào với.:khi30: gần tới hạn bảo vệ đồ án rồi mà chương trình lỗi hoài.
#include<conio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
//CHUONG TRINH TAO VA SO SANH HAI CAY NHI PHAN
struct Tree2
{
int So;
Tree2 *Left,*Right;
};
typedef Tree2 *PTree2;
PTree2 Root1,Root2,p;
int nd,nd2,cv,k,h;
//==================================
//cap phat mot nut la cho cay nhi phan
PTree2 makenode (int x)
{
PTree2 p;
p= new Tree2;
(*p).So=x;
(*p).Left=NULL;
(*p).Right=NULL;
return (p);
}
//==================================
//tim mot nut co noi dung x
PTree2 search(PTree2 &root ,int x)
{
PTree2 p;
if((*root).So==x) return (root);
if(root==NULL) return NULL;
p=search((*root).Left,x);
if(p==NULL)
p=search(
(*root).Right,x);
return p;
}
//=================================
//Kiem tra 2 cay
int Test(PTree2 Root1,PTree2 Root2)
{
if(Root1 != NULL && Root2 != NULL)
if(Test((*Root1).Left,(*Root2).Right) && Test((*Root1).Right,(*Root2).Left))
return(1);
else return(0);
else
if(Root1==NULL && Root2==NULL)return(1);
else return(0);
}
//==================================
// chieu cao cua cay
int ChieuCao(PTree2 Root)
{
int trai,phai;
if (Root==NULL) return 0;
else
{
trai=ChieuCao((*Root).Left);
phai=ChieuCao((*Root).Right);
if(trai>phai) return (trai+1);
else return (phai+1);
}
}
//==================================
//ham tao cay nhi phan
void Create(PTree2 &root)
{
PTree2 k,h;
int nd,nd2,cv;
{
printf("\n NHAP NUT GOC:");
scanf("%d",&nd);
root=makenode(nd);
if (root!=NULL)
do {
printf("\n YOUR SECLECT:");
scanf("%d",&cv);
if(cv>0&&cv<4)
{
printf("\n Nhap nut cha:");
scanf("%d",&nd2);
p=search(root,nd2);
if (p==NULL)
{
printf ("\n Khong co nut cha");
}
else if(cv==1)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
if((*p).Left!=NULL)
{
k=(*p).Left;
(*p).Left=makenode(nd);
h=(*p).Left;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if((*p).Left==NULL)
(*p).Left=makenode(nd);
}
else if(cv==2)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
if((*p).Right!=NULL)
{
k=(*p).Right;
(*p).Right=makenode(nd);
h=(*p).Right;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if ((*p).Right==NULL)
(*p).Right=makenode(nd);
}
else if(cv==3)
{
printf("\n Nhap nut la:");
scanf("%d",&nd);
k=search(root,nd);
if(k==NULL)
{
printf("\n khong co nut nay");
}
else if((*k).Left!=NULL ||(*k).Right!=NULL)
{
printf("\n %d khong phai nut la khong xoa duoc",(*k).So);
}
else if(k==(*p).Left)
(*p).Left=NULL;
else if(k==(*p).Right)
(*p).Right=NULL;
}
}
}while (cv!=0);
}
}
//================================================== =============
main()
{
char chon;
//clrscr();
printf("\nXin Chao ^_^ ");
printf("\n\n\ HUONG DAN CACH NHAP CAY NHI PHAN");
printf("\n1. Chon 1 de nhap nut con ben trai");
printf("\n2. Chon 2 de nhap nut con ben phai");
printf("\n3. Chon 3 de xoa nut la");
printf("\n4. Chon 0 de dung nhap");
Root1=NULL;
Root2=NULL;
printf("\n\n NHAP CAY NHI PHAN THU I (seclect: 0,1,2,3)\n");
Create(Root1);
printf("\n NHAP CAY NHI PHAN THU II (seclect: 0,1,2,3)\n");
Create(Root2);
if(Test(Root1,Root2))
printf("\n\n --> HAI CAY NHI PHAN LA DANG CAU\n");
else printf("\n\n --> HAI CAY NHI PHAN KHONG DANG CAU VOI NHAU\n");
getch();
}
mình nhập cây nó cứ báo là ko có nút cha hoài,mong các bạn chỉ giúp mình chương trình còn thiếu sót gì hay sai chổ nào với.:khi30: gần tới hạn bảo vệ đồ án rồi mà chương trình lỗi hoài.