kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Chủ Nhật, 30 tháng 3, 2014

Tạo 1 file tên là tknp.cpp với nội dung như sau:


#include <iostream>
#include <malloc.h>

using namespace
std;
typedef
int KeyType;
typedef struct
Node{
KeyType Key;
Node* Left;
Node* Right;
};

typedef
Node* Tree;

void
InsertNode(KeyType X, Tree &T){
if
(T==NULL){
T=(Node*)malloc(sizeof(Node));
T->Key = X;
T->Left = NULL;
T->Right = NULL;
}

else if
( T->Key < X) InsertNode(X,T->Right);
else if
( T->Key > X) InsertNode(X,T->Left);
}

KeyType DeleteMin(Tree &T){
if
(T->Left == NULL)
DeleteMin(T->Left);
else
{
KeyType K = T->Key;
T = T->Right;
return
K;
}
}


void
DeleteNode(KeyType X, Tree &T){
if
(T->Key < X) DeleteNode(X,T->Right);
else if
(T->Key > X) DeleteNode(X,T->Left);
else if
(T->Left == NULL && T->Right==NULL)
T = NULL;
else if
(T->Left == NULL) T=T->Right;
else if
(T->Right == NULL) T=T->Left;
else
T->Key = DeleteMin(T->Right);
}


int
Search(KeyType X, Tree T){
if
(T==NULL) return 0;
if
(T->Key==X) return 1;
if
(T->Key < X) Search(X,T->Right);
else
Search(X,T->Left);
}


void
InOrder(Tree T){
if
(T!=NULL){
InOrder(T->Left);
cout << T->Key<<" ; ";
InOrder(T->Right);
}
}

int
sl=0;
void
InOrder2(Tree T, KeyType *&ds ){
if
(T!=NULL){
InOrder2(T->Left,ds);
ds[sl++]=T->Key;
InOrder2(T->Right,ds);
}
}

int
getAllValue(Tree T,KeyType *&ds){
sl=0;
ds=new KeyType[100];
InOrder2(T, ds );
return
sl;
}


Tiếp tục, tạo 1 file tên taphop.cpp cùng thư mục với file tknp.cpp ở trên, gõ nội dung sau:


#include "tknp.cpp"
typedef KeyType ElementType;
typedef
Tree Set;

void
Insert(ElementType X, Set &S){
InsertNode(X,S);
}

void
Delete(ElementType X, Set &S){
DeleteNode(X,S);
}

int
Member(ElementType X, Set S){
return
Search(X,S);
}

void
PrintSet(Set S){
InOrder(S);
}

#include <conio.h>
Set Union(Set A, Set B){
KeyType *ds;
Set C = NULL;
int
n = getAllValue(A,ds);
for
(int i = 0;i<n;i++) Insert(ds[i],C);
n = getAllValue(B,ds);
for
(int i = 0;i<n;i++) Insert(ds[i],C);
return
C;
}

Set Intersect(Set A, Set B){
Set C = NULL;
KeyType *ds;
int
n = getAllValue(A,ds);
for
(int i = 0;i<n;i++)
if
(Member(ds[i],B))
Insert(ds[i],C);
return
C;
}

Set Sub(Set A, Set B){
Set C = NULL;
KeyType *ds;
int
n = getAllValue(A,ds);
for
(int i = 0;i<n;i++)
if
(!Member(ds[i],B))
Insert(ds[i],C);
return
C;
}

int
Equal(Set A, Set B){
KeyType *dsA,*dsB;
int
n = getAllValue(A,dsA);
int
m = getAllValue(B,dsB);
if
(n!=m) return 0;
for
(int i = 0;i<n;i++)
if
(!Member(dsA[i],B))
return
0;
return
1;
}


int
main(){
Set A=NULL;Set B = NULL; Set C = NULL;
Insert(3,A);Insert(4,A);Insert(7,A);
Insert(6,B);Insert(4,B);Insert(5,B);

cout<<"tap hop A = "; PrintSet(A);
cout<<"\ntap hop B = "; PrintSet(B);
cout<<"\n----------\n";
cout<<"\nUnion(A,B) = "; PrintSet(Union(A,B));
cout<<"\nIntersect(A,B) = "; PrintSet(Intersect(A,B));
cout<<"\nSub(A,B) = "; PrintSet(Sub(A,B));
cout<<"\nEqual (A,B) = "<<Equal(A,B);
PrintSet(C);
}
30 Mar 2014

0 nhận xét:

Đăng nhận xét

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

domain, domain name, premium domain name for sales

Bài đăng phổ biến