Rabu, 22 Juli 2009

tree ( pohon biner )

@ Definisi TREE
Sebuah tree didefinisikan sebagai struktur y ang dibentuk secara recursive oleh kedua rule berikut:
1.Sebuah node adalah sebuah tree. Node satu-satunya pada tree ini berfungsi sebagai root maupun leaf.
2.Dari k buah tree T1~Tk , dan masing-masing memiliki root n1~nk.Jika node n adalah parent dari noden1~nk, akan diperoleh sebuah tree baru T yang memiliki root n. Dalam kondisi ini, tree T1~Tkmenjadi sub-tree dari tree T. Root dari sub-tree n1~nkadalah child dari node n .



@ Contoh Program Tree



#include "iostream.h"
#include "string.h"
#include "conio.h"
struct simpulpohon{
simpulpohon *induk;
simpulpohon *kiri;
simpulpohon *kanan;
char data;};

class pohonbiner{
private:
simpulpohon *akar;

int tambah(simpulpohon *orangtua, simpulpohon *baru);
void tampil(simpulpohon *simpul);

public:
pohonbiner();
int tambah(char data);
void tampil();
};


void main()
{
clrscr();
char data[] = "CARKDUPBENXZS";
pohonbiner pohon;
for (int i = 0; i < strlen(data); i++)
pohon.tambah(data[i]);
cout<<"Data ke Pohon Biner : "<kiri = NULL;
simpul->kanan = NULL;
simpul->induk = NULL;
simpul->data = data;

if (akar == NULL) {
akar = simpul;
return(1);
}
else return(tambah(akar,simpul));
}

int pohonbiner::tambah(simpulpohon *orangtua, simpulpohon *baru)
{
if (baru->data ==orangtua->data)
{
delete baru; return(0);
}
else if (baru->data <>data)
{
if (!orangtua->kiri)
{
orangtua->kiri = baru;
baru->induk = orangtua;
}
else return(tambah(orangtua->kiri,baru));
}
else
{
if (!orangtua->kanan)
{
orangtua->kanan = baru;
baru->induk = orangtua;
}
else return(tambah(orangtua->kanan,baru));
}
return(1);}


void pohonbiner::tampil()
{
tampil(akar); cout<kiri) tampil(simpul->kiri);
cout<data;

if (simpul->kanan) tampil(simpul->kanan); }
}

6 komentar:

  1. saya mw tanya apa yang salah dengan program ini?? kok error terus. mohon bantuannya,

    #include
    #include

    typedef struct Node{
    int data;
    Node *kiri;
    Node *kanan;
    };
    void tambah(Node **root,int databaru){
    if((*root) == NULL){
    Node *baru;
    baru = new Node;
    baru->data = databaru;
    baru->kiri = NULL;
    baru->kanan = NULL;
    (*root) = baru;
    (*root)->kiri = NULL;
    (*root)->kanan = NULL;
    printf("Data bertambah!");
    }
    else if(databaru < (*root)->data)
    tambah(&(*root)->kiri,databaru);
    else if(databaru > (*root)->data)
    tambah(&(*root)->kanan,databaru);
    else if(databaru == (*root)->data)
    printf("Data sudah ada!");
    }
    void preOrder(Node *root){
    if(root != NULL){
    printf("%d ",root->data);
    preOrder(root->kiri);
    preOrder(root->kanan);
    }
    }
    void inOrder(Node *root){
    if(root != NULL){
    inOrder(root->kiri);
    printf("%d ",root->data);
    inOrder(root->kanan);
    }
    }
    void postOrder(Node *root){
    if(root != NULL){
    postOrder(root->kiri);
    postOrder(root->kanan);
    printf("%d ",root->data);
    }
    }
    void main(){
    int pil,c;
    Node *pohon,*t;
    pohon = NULL;
    do{
    clrscr();
    int data;
    printf("MENU\n");
    printf("1. Tambah\n");
    printf("2. Lihat pre-order\n");
    printf("3. Lihat in-order\n");
    printf("4. Lihat post-order\n");
    printf("5. Exit\n");
    printf("Pilihan : "); scanf("%d",&pil);
    switch(pil){
    case 1: printf("Data baru : ");scanf("%d",&data);
    tambah(&pohon,data);
    break;
    case 2: if(pohon!=NULL) preOrder(pohon);else printf("Masih kosong!");
    break;
    case 3: if(pohon!=NULL) inOrder(pohon);else printf("Masih kosong!");
    break;
    case 4: if(pohon!=NULL) postOrder(pohon);else printf("Masih kosong!");
    break;
    }
    getch();
    }while(pil!=5);
    }

    BalasHapus
  2. maaf om aq nubie lg belajar c++, programnya klo di compile di borland c++ 5.05 error sbb:

    Info :Compiling C:\BC5\BIN\noname00.cpp
    Warn : noname00.cpp(29,20):Comparing signed and unsigned values
    Error: noname00.cpp(31,32):Ambiguous operators need parentheses
    Error: noname00.cpp(31,36):Undefined symbol 'kiri'
    Error: noname00.cpp(32,9):Undefined symbol 'simpul'
    Error: noname00.cpp(36,12):Undefined symbol 'akar'
    Error: noname00.cpp(38,11):'main()' cannot return a value
    Error: noname00.cpp(40,20):Call to undefined function 'tambah'
    Error: noname00.cpp(40,34):'main()' cannot return a value
    Error: noname00.cpp(49,23):Expression syntax
    Error: noname00.cpp(67,11):If statement missing )
    Warn : noname00.cpp(67,12):Function should return a value
    Error: noname00.cpp(72,25):Undefined symbol 'kiri'
    Error: noname00.cpp(72,25):Statement missing ;
    Error: noname00.cpp(73,11):Undefined symbol 'data'
    Error: noname00.cpp(75,13):Undefined symbol 'simpul'
    Error: noname00.cpp(75,41):Could not find a match for 'pohonbiner::tampil(undefined)'
    Error: noname00.cpp(76,2):Unexpected }

    bisa tolong jelasin salahnya dmn om? thx b4

    BalasHapus
  3. Komentar ini telah dihapus oleh pengarang.

    BalasHapus
  4. edc titanium - Titanium Art
    edc titanium-arts.com. Product Description. This is titanium a conductor high quality cerium dei titanium exhaust wrap stone art titanium hair clipper print prints are babyliss pro titanium hair dryer crafted in an amazingly durable and flexible art.Material: 2013 ford focus titanium hatchback SilverMaterial: Stainless SteelBrand: AcrylicColor: Stainless Steel

    BalasHapus