Not seeing a Scroll to Top Button? Go to our FAQ page for more info. [C++] Heap Tree Menggunakan array | -

My Imaginations

-
Follow Me

[C++] Heap Tree Menggunakan array



By  Galeh Fatma Eko Ardiansa     Mei 22, 2014     
Kali ini saya akan menposting sebuah program C++ yang bertujuan untuk membuat susunan tree dengan menyusunnya dari yang terkecil sampai ke yang besar....ini source codenya untuk short ascending dan sort descending untuk heap tree....ini menggunakan aplikasi dev C++ 5.4 jadi kaga usah pake #include <conio.h> dan getch();

#include<iostream>
using namespace std;
void menu(){
cout<<"==========MENU==========\n";
cout<<"1. Masukkan Angka\n";
cout<<"0. keluar\n";
cout<<"Masukkan Pilihan: ";
}
void in(){
int temp,x,y;
int data[7];
for (int i=1;i<=7;i++){
cout<<"Masukkan data : ";
cin>>data[i];
}
do{
cout<<"\n-----------------------\n";
cout<<"1. Min Heap\n";
cout<<"2. Max Heap\n";
cout<<"0. Kembali ke menu\n";
cout<<"Masukkan Pilihan Anda : ";
cin>>y;
switch (y){
case 1 : 
for (int i=1;i<=7;i++){
x=i;
while(x!=1)
{
if(data[x]< data[x/2]) 
temp=data[x];       
data[x]=data[x/2]; 
data[x/2]=temp;    
}
x = x/2;  
}
}
//tampilkan min heap tree
cout<<"========Min Heap Tree========"<<endl;
cout<<" "<<data[1]<<endl;
cout<<" "<<data[2]<<" "<<data[3]<<endl;
cout<<"  "<<data[4]<<"     "<<data[5]<<"   "<<data[6]<<"      "<<data[7]<<endl;
break;
case 2 : 
//max heap tree
for (int i=1;i<=7;i++){
x=i;
while(x!=1)
{
if(data[x]> data[x/2]) 
temp=data[x];       
data[x]=data[x/2]; 
data[x/2]=temp;    
}
x = x/2;  
}
}
//tampilkan min heap tree
cout<<"========Max Heap Tree========"<<endl;
cout<<" "<<data[1]<<endl;
cout<<" "<<data[2]<<" "<<data[3]<<endl;
cout<<"  "<<data[4]<<"     "<<data[5]<<"   "<<data[6]<<"      "<<data[7]<<endl;
break;
}
}while(y!=0);
}

int main(){
int x;  
do{
menu();
cin>>x;
switch (x){
case 1 :  in();break;
}
}while(x!=0);

Oke sekian dulu...semoga bermanfaat.....:)

About Galeh Fatma Eko Ardiansa

If you can dream it, you can do it | Genius is one percent inspiration and ninety-nine percent perspiration | If you don’t make mistakes, you’re not working on hard enough problems | The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart | I can't change the direction of the wind, but I can adjust my sails to always reach my destination.

1 komentar:

  1. Halo bang galeh...
    cuma nyapa aja... hahaha
    jangan lupa juga kunjungi blog saya
    samngalamfirmansyah.blogspot.com

    BalasHapus


Formulir Kontak

Nama

Email *

Pesan *

Advertisement

Disqus Shortname