i hope it can be useful, lets check it out
binnery seacrh
#include <stdio.h>
void tukar(int *, int *);
int angka,angka1,angka2, key1,key2,key3, pilih, tampung[10], tampung1[10], tampung2[10] , kondisi,mode,jml,jml1,jml2,data[10],data1[10],data2[10];
void selection(int x[], int n)
{
int i, j, find, index, ubah=0;
for(i=0; i<n; i++)
printf("%d ", tampung[i]);
for(i=0; i < n-1; i++) {
find = i;
kondisi = 0;
for(j=i+1; j<n; j++) {
if(mode == 1)
kondisi = x[j] < x[find];
else
kondisi = x[j] > x[find];
if(kondisi) {
find = j;
ubah = 1;
}
}
if(ubah) {
tukar(&x[find], &x[i]);
puts("");
for(index=0; index<n; index++)
printf("%d ", x[index]);
}
}
}
int modeUrut()
{
int pil;
do {
printf("\n\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2");
puts("");
puts("Pengurutan yang dipilih : ");
puts("1. Ascending");
puts("2. Descending");
printf("Pilihan anda [1/2] : ");
scanf("%d", &pil);
printf("\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n");
} while((pil<1) || (pil>2));
if(pil == 1)
return 1;
else
return 2;
}
void tukar (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void input_angka()
{int i;
printf("\nPROGRAM BINARY SEARCH MENGGUNAKAN METODE SELECTION SEARCH\n ");
printf("\nMASUKKAN JUMLAH ELEMENNYA(max 10) = ");
scanf("%d", &jml);
for(i=0; i<jml; i++)
{
printf("data ke-%d = ",i);
scanf("%d", &data[i]);
tampung[i] = data[i];
}
for(i=0; i<jml; i++)
data[i] = tampung[i];
mode = modeUrut();
selection(data, jml);
}
int binary (int k[], int key, int n)
{
int low, hi, mid;
low=0;
hi=n-1;
while (low<=hi)
{
mid=(low+hi)/2;
if (key==k[mid])
return(mid);
if (key<k[mid])
hi=mid-1;
else
low=mid+1;
}
return (-1);
}
void main()
{
int hasil;
char ulang;
input_angka();
do
{
printf("\ncari: ");
scanf("%d", &key1);
hasil=binary(data, key1, jml);
if(hasil==-1)
printf("\nANGKA TIDAK DITEMUKAN\n");
else
printf("\nANGKA %d KETEMU PADA INDEX KE %d\n", key1, hasil);
fflush(stdin);
printf("APAKAH ANDA INGIN MENGULANG PENCARIAN (y/t)? ");
scanf("%c", &ulang);
}
while(ulang=='y'||ulang=='Y');
}
void tukar(int *, int *);
int angka,angka1,angka2, key1,key2,key3, pilih, tampung[10], tampung1[10], tampung2[10] , kondisi,mode,jml,jml1,jml2,data[10],data1[10],data2[10];
void selection(int x[], int n)
{
int i, j, find, index, ubah=0;
for(i=0; i<n; i++)
printf("%d ", tampung[i]);
for(i=0; i < n-1; i++) {
find = i;
kondisi = 0;
for(j=i+1; j<n; j++) {
if(mode == 1)
kondisi = x[j] < x[find];
else
kondisi = x[j] > x[find];
if(kondisi) {
find = j;
ubah = 1;
}
}
if(ubah) {
tukar(&x[find], &x[i]);
puts("");
for(index=0; index<n; index++)
printf("%d ", x[index]);
}
}
}
int modeUrut()
{
int pil;
do {
printf("\n\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2");
puts("");
puts("Pengurutan yang dipilih : ");
puts("1. Ascending");
puts("2. Descending");
printf("Pilihan anda [1/2] : ");
scanf("%d", &pil);
printf("\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n");
} while((pil<1) || (pil>2));
if(pil == 1)
return 1;
else
return 2;
}
void tukar (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void input_angka()
{int i;
printf("\nPROGRAM BINARY SEARCH MENGGUNAKAN METODE SELECTION SEARCH\n ");
printf("\nMASUKKAN JUMLAH ELEMENNYA(max 10) = ");
scanf("%d", &jml);
for(i=0; i<jml; i++)
{
printf("data ke-%d = ",i);
scanf("%d", &data[i]);
tampung[i] = data[i];
}
for(i=0; i<jml; i++)
data[i] = tampung[i];
mode = modeUrut();
selection(data, jml);
}
int binary (int k[], int key, int n)
{
int low, hi, mid;
low=0;
hi=n-1;
while (low<=hi)
{
mid=(low+hi)/2;
if (key==k[mid])
return(mid);
if (key<k[mid])
hi=mid-1;
else
low=mid+1;
}
return (-1);
}
void main()
{
int hasil;
char ulang;
input_angka();
do
{
printf("\ncari: ");
scanf("%d", &key1);
hasil=binary(data, key1, jml);
if(hasil==-1)
printf("\nANGKA TIDAK DITEMUKAN\n");
else
printf("\nANGKA %d KETEMU PADA INDEX KE %d\n", key1, hasil);
fflush(stdin);
printf("APAKAH ANDA INGIN MENGULANG PENCARIAN (y/t)? ");
scanf("%c", &ulang);
}
while(ulang=='y'||ulang=='Y');
}
*******************************************************************************
Algoritma tree c++
#include <stdlib.h>
class BinarySearchTree
{
//private:
struct nodeTree
{
nodeTree* kiri;
nodeTree* kanan;
int data;
};
nodeTree* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void print_inorder();
void inorder(nodeTree*);
void print_preorder();
void preorder(nodeTree*);
void print_postorder();
void postorder(nodeTree*);
void insert(int);
void remove(int);
};
void BinarySearchTree::insert(int d)
{
nodeTree* t = new nodeTree;
nodeTree* parent;
t->data = d;
t->kiri = NULL;
t->kanan = NULL;
parent = NULL;
if(isEmpty()) root = t;
else
{
nodeTree* current;
current = root;
while(current)
{
parent = current;
if(t->data > current->data) current = current->kanan;
else current = current->kiri;
}
if(t->data < parent->data)
parent->kiri = t;
else
parent->kanan = t;
}
}
void BinarySearchTree::remove(int d)
{
//Locate the element
bool found = false;
if(isEmpty())
{
cout<<" pohon kosong "<<endl;
return;
}
nodeTree* current;
nodeTree* parent;
current = root;
while(current != NULL)
{
if(current->data == d)
{
found = true;
break;
}
else
{
parent = current;
if(d>current->data) current = current->kanan;
else current = current->kiri;
}
}
if(!found)
{
cout<<" data tidak di dirikan "<<endl;
return;
}
// Node dengan single child
if((current->kiri == NULL && current->kanan != NULL)|| (current->kiri != NULL
&& current->kanan == NULL))
{
if(current->kiri == NULL && current->kanan != NULL)
{
if(parent->kiri == current)
{
parent->kiri = current->kanan;
delete current;
}
else
{
parent->kanan = current->kanan;
delete current;
}
}
else
{
if(parent->kiri == current)
{
parent->kiri = current->kiri;
delete current;
}
else
{
parent->kanan = current->kiri;
delete current;
}
}
return;
}
// node tidak mempunyai child node
if( current->kiri == NULL && current->kanan == NULL)
{
if(parent->kiri == current ) parent->kiri = NULL;
else parent->kanan = NULL;
delete current;
return;
}
//Node dengan 2 child node
// ganti node dengan nilai terkecil di subtree bagain kanan
if (current->kiri != NULL && current->kanan != NULL)
{
nodeTree* temp;
temp = current->kanan;
if((temp->kiri == NULL) && (temp->kanan == NULL))
{
current = temp;
delete temp;
current->kanan = NULL;
}
else
{
if((current->kanan)->kiri != NULL)
{
nodeTree* lcurrent;
nodeTree* lcurrp;
lcurrp = current->kanan;
lcurrent = (current->kanan)->kiri;
while(lcurrent->kiri != NULL)
{
lcurrp = lcurrent;
lcurrent = lcurrent->kiri;
}
current->data = lcurrent->data;
delete lcurrent;
lcurrp->kiri = NULL;
}
else
{
nodeTree* tmp2;
tmp2 = current->kanan;
current->data = tmp2->data;
current->kanan = tmp2->kanan;
delete tmp2;
}
}
return;
}
}
void BinarySearchTree::print_inorder()
{
inorder(root);
}
void BinarySearchTree::inorder(nodeTree* p)
{
if(p != NULL)
{
if(p->kiri) inorder(p->kiri);
cout<<" "<<p->data<<" ";
if(p->kanan) inorder(p->kanan);
}
else return;
}
void BinarySearchTree::print_preorder()
{
preorder(root);
}
void BinarySearchTree::preorder(nodeTree* p)
{
if(p != NULL)
{
cout<<" "<<p->data<<" ";
if(p->kiri) preorder(p->kiri);
if(p->kanan) preorder(p->kanan);
}
else return;
}
void BinarySearchTree::print_postorder()
{
postorder(root);
}
void BinarySearchTree::postorder(nodeTree* p)
{
if(p != NULL)
{
if(p->kiri) postorder(p->kiri);
if(p->kanan) postorder(p->kanan);
cout<<" "<<p->data<<" ";
}
else return;
}
int main()
{
BinarySearchTree b;
int ch,tmp,tmp1;
while(1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ------------- "<<endl;
cout<<" 1. Input nilai "<<endl;
cout<<" 2. In-Order Traversal "<<endl;
cout<<" 3. Pre-Order Traversal "<<endl;
cout<<" 4. Post-Order Traversal "<<endl;
cout<<" 5. Hapus "<<endl;
cout<<" 6. Exit "<<endl;
cout<<" Masukkan Pilihan : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Input Angka : ";
cin>>tmp;
b.insert(tmp);
break;
case 2 : cout<<endl;
cout<<" In-Order Traversal "<<endl;
cout<<" -------------"<<endl;
b.print_inorder();
break;
case 3 : cout<<endl;
cout<<" Pre-Order Traversal "<<endl;
cout<<" -------------"<<endl;
b.print_preorder();
break;
case 4 : cout<<endl;
cout<<" Post-Order Traversal "<<endl;
cout<<"-------------"<<endl;
b.print_postorder();
break;
case 5 : cout<<" Masukkan data yang akan dihapus : ";
cin>>tmp1;
b.remove(tmp1);
break;
case 6 :
return 0;
}
}
}
*********************************************************************************
source : segokarak.blogspot.com
No comments:
Post a Comment