loading...
loading...

Thursday, June 9, 2016

Contoh Coding Binary Search Tree C++


Contoh Coding Binary Search Tree C++

Hai teman teman

kali ini saya akan mengeshare tentang Coding untuk binary search tree

sebelumnya untuk lebih jelasnya kita review dulu yah tentang binary search tree.

secara sederhana :


sebuah binary search tree (bst) adalah sebuah pohon biner yang boleh kosong, dan setiap nodenya harus memiliki identifier/value. value pada semua node subpohon sebelah kiri adalah selalu lebih kecil dari value dari root, sedangkan value subpohon di sebelah kanan adalah sama atau lebih besar dari value pada root, masing – masing subpohon tersebut (kiri&kanan)

tanpa basa basi untuk coding C++ nya sebagai berikut

#include<iostream>

#include<cstdlib>
using namespace std;

class BinarySearchTree{
      private:
      struct node{
             node* left;
             node* right;
             int data;
      };
      node* root;
      
      public:
             BinarySearchTree(){
             root = NULL;
             }
             
             bool isEmpty() const {return root==NULL;}
             struct node* newNode(int data){
             struct node* node = new(struct node);
                    node->data = data;
                    node->left = NULL;
                    node->right = NULL;
                    return(node);
             }
                    
             struct node* insert(struct node* node, int data){
                    if(node == NULL){
                             return(newNode(data));
                    }
                    else{
                         
                    if (data <= node->data) node->left = insert(node->left, data);
                    else node->right = insert(node->right, data);
                    return(node);
                    }
             }
             void insert(int data){
                  if(isEmpty()){
                                 root = newNode(data);
                                      }else{
                                            insert(root,data);
                                      }
                                 }
                                 
             void printTree(struct node* node){
                  if(node == NULL)return;
                  /*InOrder
                  printTree(node->left);
                  cout<<" "<<node->data;
                  printTree(node->right); 
                  
                  /*PreOrder
                  cout<<" "<<node->data;
                  printTree(node->left);
                  printTree(node->right);*/
                  
                  /*PostOrder
                  printTree(node->left);
                  printTree(node->right);
                  cout<<" "<<node->data;*/
             }
             void callPrintTree(){
                  printTree(root);
             }
             
             int callLookup(int target){
                 return lookup(root,target);
                 }
                 int lookup(struct node* node,int target){
                     if(node==NULL) return 0;
                     
                     if(node->data == target)return 1;
                     else{
                          if(target < node->data)
                          return lookup(node->left,target);
                          else
                          return lookup(node->right,target);
                          }
                     }
              
             int callMin(){
                 return min(root);
                 }
                 int min(struct node* node){
                     if(node->left==NULL) return node->data;
                     else return min(node->left);
                     }
                     
             int callMax(){
                 return max(root);
                 }
                 int max(struct node* node){
                     if(node->right==NULL)return node->data;
                     else return max(node->right);
                     }
                     
             int callDept(){
                 return dept(root);
                 }
                 int dept(struct node* node){
                     if(node == NULL) return 0;
                     else{
                          int l = dept(node->left);
                          int r = dept(node->right);
                          if(l < r) return 1 + r;
                               else return 1 + l;
                                     
                               }
                          }
                 
};


int main(){
    BinarySearchTree b;
    b.insert(5);
    b.insert(3);
    b.insert(9);
    b.insert(1);
    b.insert(4);
    b.insert(6);
    //b.callPrintTree();
    cout<<b.callLookup(4)<<endl; //output : 1
    cout<<b.callLookup(2)<<endl; //output : 0
    cout<<b.callMin()<<endl; //output : 1
    cout<<b.callMax()<<endl; //output : 9
    cout<<b.callDept()<<endl; // output : 3
    
    system("pause");
    return 0;
}

sekian postingan dari saya

jangan lupa like commend dan share yah

terima kasih




www.ayeey.com www.resepkuekeringku.com www.desainrumahnya.com www.yayasanbabysitterku.com www.luvne.com www.cicicookies.com www.tipscantiknya.com www.mbepp.com www.kumpulanrumusnya.com www.trikcantik.net

Membuat Contoh Coding C++ Enqueue Dequeue Queue



Membuat Contoh Coding C++ Enqueue Dequeue Queue 


Hai teman teman

kali ini saya akan mengeshare contoh coding C++ untuk enqueue dequeue dan queue

untuk lebih jelasnya mari kita simak bahasan berikut
Enqueue adalah proses untuk memasukkan elemen artinya menambah data baru. Jika elemen data tidak bisa dimasukkan karena melebihi kapasitas queue akan muncul error yang disebut Overflow.
Dequeue adalah proses untuk mengeluarkan elemen artinya menghapus data. Jika tidak bisa mengeluarkan elemen data satupun karena kosong akan terjadi error yang disebut dengan Underflow.
Queue
adalah antrian dimana dalam antrian ini diterapkan sistem FIFO atau First In First Out, artinya data yang pertama masuk, dialah data yang di proses dahulu.

berikut adalah kelemahan queue diantaranya
Data yang terahir tidak akan diproses dahulu sebelum data yang pertama diproses, sehingga waktu tunggu untuk memproses data yang ahir tadi tidak bisa diperkirakan sampai kapan.

untuk coding C++ nya sebagai berikut
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool cek=false;
struct node{
       int data;
       node *next;
       };
       
class queue{
      node *rear,*front;
public:
      queue(){
          rear=NULL;
          front=NULL;
          }
      void enqueue();
      void enqueue2(int d);
      void dequeue();
      void display();
      void counter();
      void input();
      void tebak();
      int t,z;
      ~queue();
};

void queue::enqueue(){
     node *temp;
     temp=new node;
     cout<<"Data : ";
     cin>>temp->data;
     temp->next=NULL;
     if(rear==NULL){
         rear=temp;
         front=temp;
         }
     else{
          rear->next=temp;
          rear=temp;
          }
}
void queue::enqueue2(int d){
     node *temp;
     temp=new node;
     temp->data=d;
     temp->next=NULL;
     if(rear==NULL){
         rear=temp;
         front=temp;
         }
     else{
          rear->next=temp;
          rear=temp;
          }
}

void queue::dequeue(){
     if(front!=NULL){
         node *temp = front;
         front=front->next;
         delete temp;
         if(front==NULL){
             rear=NULL;
             }
     }else{
         cout<<"Queue Empty. . ";
         }
}
void queue::display(){
     node *temp = front;
     while(temp!=NULL){
             cout<<temp->data<<"\t";
             temp=temp->next;
             }
     cout<<endl;
     }

queue::~queue(){
     while(front!=NULL){
               node *temp = front;
               front=front->next;
               delete temp;
               }
     }

void queue::counter(){
     int c,j=0;
     srand(time(NULL));
     t = rand()%6+1;
     while(j<t){
         c=front->data;
         queue::dequeue();
         queue::enqueue2(c);
         j++;
        }
     z=front->data;
     queue::dequeue();              
}

void queue::input(){
//int inputan;
//cout << "Mau berapa inputan ? " ;
// cin>>inputan;
     int i =0,d,e;
     while(i<6){
         cout<<"Masukkan Data ke "<<i+1<<" :";
         cin>>d;
         queue::enqueue2(d);
         i++;
         }
     cout<<"\n";
     }
void queue::tebak(){
     int e;
     cout<<"\nTebak Angka yang hilang : ";
     cin>>e;
     if(e==queue::z){
                 cout<<"\nSelamat Anda Menang"<<endl;
                 cek=true;
                 cout<<"Angka Counter = "<<queue::t<<"\tAngka yang hilang =  "<<queue::z<<endl;
                 }
     else{
          cout<<"Maaf Anda salah, silakan coba lagi"<<endl;
          cout<<"Angka Counter = "<<queue::t<<"\tAngka yang hilang =  "<<queue::z<<endl;
          cek=false;
          Sleep(2000);
          system("cls");
          
          }
     }
int main(){
    queue obj;
    obj.input();
    int i=6;
    while(cek==false&&i>1){
        cout<<"Data : "<<endl;
        obj.display();
        obj.counter();
        cout << "\n\n";
cout << "Hasil Enqueue dan Dequeue : "; 
        obj.display();        
        obj.tebak();
        i--;
        }
    if(i<=1){
cout<<"Maaf anda kalah, coba lagi di lain waktu :D "<<endl;
}
    //obj.display();
    system("pause");              
    return 0;
}

sekian untuk postingan kali ini

jangan lupa like commend dan share yah

Terima kasih

www.ayeey.com www.resepkuekeringku.com www.desainrumahnya.com www.yayasanbabysitterku.com www.luvne.com www.cicicookies.com www.tipscantiknya.com www.mbepp.com www.kumpulanrumusnya.com www.trikcantik.net

Membuat Game Matematis Sederhana C++

HOW TO MAKE SIMPLE GAME C++

Hallo teman teman
kali ini saya akan berbagi tentang bagaimana cara membuat game matematis sederhana
tanpa basa basi
berikut adalah coding untuk C++ nya

#include <iostream.h>
#include <ctime>


void tulispitlab(){
cout<<"\n\n";
        cout<<"                  *****************************************"<<endl;
        cout<<"                  *          .^,     ,*,    ,/;           *"<<endl;
        cout<<"                  *           '.;;.,(0v0),.;/;;           *"<<endl;
        cout<<"                  *             ';, (   ) ,;'             *"<<endl;
        cout<<"                  *                ,':=:',                *"<<endl;
        cout<<"                  *                ;/'.'^;                *"<<endl;
        cout<<"                  *               Laboratory              *"<<endl;
        cout<<"                  *****************************************"<<endl;
        cout<<"\n\n";
}

//function & procedure
void loading ()
{
    for (int o= 0;o<100;o+=1)
    {
        cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
        cout<<"+  ____________________________________________________  +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |             Loading    "<<o<<"                          | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |                                                    | +                 + "<<endl;
        cout<<"+ |____________________________________________________| +                 + "<<endl;
        cout<<"+                                                        +                 + "<<endl;
        cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
        system("cls");
    }
}

int cetakmenu(int lv){
    int pilihan;
    system("cls");
    cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
    cout<<"+                                                                          + "<<endl;
    cout<<"+                       .;**;.    .;**;.    .;**;.                         + "<<endl;
    cout<<"+                       ( -_- )   ( +_+ )   ( ',' )                        + "<<endl;
    cout<<"+                       >') ('<   >') ('<   >') ('<                        + "<<endl;
    cout<<"+                     ------------------------------                       + "<<endl;
    cout<<"+                    | ........TEBAK ANGKA......... |                      + "<<endl;
    cout<<"+                     ------------------------------                       + "<<endl;
    cout<<"+                                                                          + "<<endl;
    cout<<"+                                                                          + "<<endl;
    cout<<"+                                                                          + "<<endl;
    cout<<"+                                                                          + "<<endl;
    cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
cout << "   Kesempatan kamu untuk menebak : " << lv << " kali \n\n";
cout << "   1. Ubah jumlah kesempatan untuk menebak permainan(COUNTER)\n";
cout << "   2. Mulai permainan\n";
    cout << "   3. Keluar dari permainan\n\n";
cout << "  Masukkan pilihan kamu (1-3): ";
cin >> pilihan;
    return pilihan;
}

int pilihlevel(){
    int pilihan;
    while ((pilihan < 4) || (pilihan > 10)){
        system("cls");
        cout << "\n\n Level yang dimaksud di sini adalah jumlah kesempatan menebak.";
        cout << "\n Semakin kecil angka semakin sulit permainan.";
        cout << "\n Masukkan level yang diinginkan (4-10) : ";
        cin >> pilihan;
    }
    return pilihan;
}

int putar(){
    srand(time(NULL));
    int x;
    x =rand() % 10;
    return x;
}


void mulaimain(int lv){
    system ("cls");
    int acak[4],tebak[4];
    int live;
    live = lv;

//acak angka
    for (int x=0000 ;x <= 3; x++){
        acak[x] = putar();
        for (int y=0000; y <= 30; y++){
            cout << " Mengacak digit ke " << x+1;
            system ("cls");
        }
    }



    cout << "*******************************************" << endl << endl ;
//Mulai penebakan
    while (live != 0) {

        cout << "Kesempatan anda menebak tinggal : " << live << endl << endl;

    // input tebakan
        for (int x=0000; x <=3; x++){
            //cout << acak[x] << endl;
            cout << "Masukkan satu tebakan digit ke " << x+1 << ": ";
            cin >> tebak[x];
        }
        cout << endl;

    //periksa tebakan
        if (((tebak[0000] == acak[0000]) && (tebak[1111] == acak[1111])) && ((tebak[2222] == acak[2222]) && (tebak[3333] == acak[3333]))){
            //benar semua
            cout << "selamat anda menang";
            break;
        } else {
            //masih ada yang salah
            for (int x=0000; x <= 3; x++) {
                cout << "Tebakan anda yang ke-" << x+1 << " :";
                if (tebak[x]==acak[x]){
                    cout << "tepat" << endl;
                } else if (tebak[x] > acak[x]) {
                    cout << "terlalu besar" << endl;
                } else {
                    cout << "terlalu kecil" << endl;
                }
            }
            cout << endl;
        }

        live -= 1;
        cout << "*******************************************" << endl ;
    }

    if (live==0) {
        cout << "anda kalah" << endl;
    } else {
        cout << "anda menang" << endl;
    }
    tulispitlab();
    system("pause");
}






// Program utama
int main(){
loading();
        cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
        cout<<"+  ____________________________________________________  +                 + "<<endl;
        cout<<"+ |     ___________ ______ _______ _______ ________    | +                 + "<<endl;
        cout<<"+ |    /____  ____// ____// ___  // ___  //   ____/    | +                 + "<<endl;
        cout<<"+ |        / /    / /___ / /__/ // /__/ //   |         | +                 + "<<endl;
        cout<<"+ |       / /    / ____// ___  // ___  // /| |         | +                 + "<<endl;
        cout<<"+ |      / /    / /___ / /__/ // /  / // / | |         | +                 + "<<endl;
        cout<<"+ |     /_/    /_____//______//_/  /_//_/  |_|         | +                 + "<<endl;
        cout<<"+ |        _______ ___   __ _______ _______ _______    | +                 + "<<endl;
        cout<<"+ |       / ___  //   | / // _____//  ____// ___  /    | +                 + "<<endl;
        cout<<"+ |      / /__/ // /| |/ // /____ /   |   / /__/ /     | +    ,,,   ,,,    + "<<endl;
        cout<<"+ |     / ___  // / |   // ___  // /| |  / ___  /      | +  ( ',') ('0')/  + "<<endl;
        cout<<"+ |    / /  / // /  |  // /__/ // / | | / /  / /  2009 | +  <|''|> /|''|   + "<<endl;
        cout<<"+ |   /_/  /_//_/   |_//______//_/  |_|/_/  /_/.::..::.| +   |__|   |__|   + "<<endl;
        cout<<"+ |____________________________________________________| +  ./  L  ./  L   + "<<endl;
        cout<<"+                                                        +                 + "<<endl;
        cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "<<endl;
    cout<<"\n\n";
system ("pause");

    int pilih, live;
    live = 10;


    while(pilih != 3){
        pilih = cetakmenu(live);
        if ((pilih > 3) || (pilih < 1)) continue;
        if (pilih==1) {
            live = pilihlevel();
        }
        if (pilih==2) {
            mulaimain(live);
        }
    }

}

sekian postingan kali ini

jangan lupa like commend and share yah
terima kasih













www.ayeey.com www.resepkuekeringku.com www.desainrumahnya.com www.yayasanbabysitterku.com www.luvne.com www.cicicookies.com www.tipscantiknya.com www.mbepp.com www.kumpulanrumusnya.com www.trikcantik.net

Contoh Coding Binary Tree C++



BINARY TREE


Hallo teman teman

kali ini saya akan mengeshare bagaimana coding untuk Binary Tree

untuk lebih jelasnya  Dalam ilmu komputer, sebuah pohon biner (binary tree) adalah
sebuah 
pohon struktur data di mana setiap simpul memiliki paling banyak dua anak. Secara khusus anaknya dinamakan kiri dan kanan. Penggunaan secara umum pohon biner adalah Pohon biner terurut, yang lainnnya adalah heap biner.

berikut adalah code C++ nya

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

/* A <span id="mvuhno2x61oe_1" class="mvuhno2x61oe">binary tree</span> node has data, pointer to left child
   and a pointer to right child */
struct node
{
   int data;
   struct node* left;
   struct node* right;
};

/* Prototypes for funtions needed in printPaths() */
void printPathsRecur(struct node* node, int path[], int pathLen);
void printArray(int ints[], int len);

/*Given a binary tree, print out all of its root-to-leaf
 paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
  int path[1000];
  printPathsRecur(node, path, 0);
}

/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
  if (node==NULL)
    return;

  /* append this node to the path array */
  path[pathLen] = node->data;
  pathLen++;

  /* it's a leaf, so print the path that led to here  */
  if (node->left==NULL && node->right==NULL)
  {
    printArray(path, pathLen);
  }
  else
  {
    /* otherwise try both subtrees */
    printPathsRecur(node->left, path, pathLen);
    printPathsRecur(node->right, path, pathLen);
  }
}


/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
  int i;
  for (i=0; i<len; i++)
  {
    printf("%d ", ints[i]);
  }
  printf("\n");
}   

/* utility that allocates a new node with the
   given data and NULL left and right pointers. */  
struct node* newnode(int data)
{
  struct node* node = (struct node*)
                       malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;
  
  return(node);
}
  
/* Driver program to test above functions*/
int main()
{
  
 struct node *root = newnode(5);
  root->left        = newnode(4);
  root->right       = newnode(8);
  root->left->left  = newnode(11);
  root->right->left  = newnode(13);
  root->right->right  = newnode(4);
  root->left->left->left  = newnode(7);
  root->left->left->right = newnode(2);
  root->right->right->right = newnode(1);
  
  printPaths(root);
  
  double number1 = 0.0;
double number2 = 0.0;
double number3 = 0.0;
double number4 = 0.0;
double answer;
cout <<"masukkan 4 angka dari path di atas"<<endl;
cin>>number1;
cin>>number2;
cin>>number3;
cin>>number4;

answer = (number1 + number2 + number3 + number4) /4;
cout<<"rata-ratanya adalah "<<answer<<endl;


  
  system ("pause");
  getchar();
  return 0;}

sekian untuk postingan kali ini

jangan lupa like commend and share yah

Terima kasih :)


www.ayeey.com www.resepkuekeringku.com www.desainrumahnya.com www.yayasanbabysitterku.com www.luvne.com www.cicicookies.com www.tipscantiknya.com www.mbepp.com www.kumpulanrumusnya.com www.trikcantik.net