Binary Tree
#include <stdio.h>
#include <conio.h>
typedef struct Node{
int data;
Node *kiri;
Node *kanan;
};
//Menambah data pada tree
void tambah(Node **root,int databaru){
if((*root) == NULL){
//Data baru di declare sebagai node -->akar(root)
Node *baru;
baru = new Node;
baru->data = databaru;
//left and right masih empty set
baru->kiri = NULL;
baru->kanan = NULL;
(*root) = baru;
(*root)->kiri = NULL;
(*root)->kanan = NULL;
printf("Data bertambah!");
}
//Menambah susunan data
//jika data yang ditambah(databaru<akarnya-root-->kiri)
else if(databaru < (*root)->data)
tambah(&(*root)->kiri,databaru);
//Jika data yang ditambah(databaru>akarnya-root-->kanan)
else if(databaru > (*root)->data)
tambah(&(*root)->kanan,databaru);
//Jika data yang ditambah(databaru)==akarnya->data sudah ada)
else if(databaru == (*root)->data)
printf("Data sudah ada!");
}
//Fungsi untuk pre order (Node(data)-->Left-->Right)
void preOrder(Node *root){
//kondisi jika root terisi/tidak boleh kosong
if(root != NULL){
printf("%d ",root->data);
preOrder(root->kiri);
preOrder(root->kanan);
}
}
//Fungsi untuk in order (Left-->Node(data)-->Right)
void inOrder(Node *root){
//kondisi jika root terisi/tidak boleh kosong
if(root != NULL){
inOrder(root->kiri);
printf("%d ",root->data);
inOrder(root->kanan);
}
}
//Fungsi untuk post order (Left-->Right-->Node(data))
void postOrder(Node *root){
//kondisi jika root terisi/tidak boleh kosong
if(root != NULL){
postOrder(root->kiri);
postOrder(root->kanan);
printf("%d ",root->data);
}
}
void main(){
int pil,c;
//deklarasi pohon/tree
Node *pohon,*t;
//set kondisi awal pohon=0
pohon = NULL;
do{
clrscr();
//Menu tampilan awal saja-untuk menjalankan fungsi pilihan
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){
//Kondisi jika menjalankan fungsi menambah data
case 1: printf("Data baru : ");scanf("%d",
&data);
tambah(&pohon,data);
break;
//Kondisi jika menjalankan fungsi tampilkan data secara preOrder
case 2: if(pohon!=NULL) preOrder(pohon);
else printf("Masih kosong!");
break;
//Kondisi jika menjalankan fungsi tampilkan data secara inOrder
case 3: if(pohon!=NULL) inOrder(pohon);
else printf("Masih kosong!");
break;
//Kondisi jika menjalankan fungsi tampilkan data secara postOrder
case 4: if(pohon!=NULL) postOrder(pohon);
else printf("Masih kosong!");
break;
}
getch();
//Sampai kondisi false berhenti
}while(pil!=5);
}
Contoh Binary Tree
19.13 |
Langganan:
Posting Komentar (Atom)
3 komentar:
Jadi ingat skripsi kalau lihat coding.
Wangsa Design
Ornamen Besi Tempa
Railing Balkon Besi Tempa
Railing Tangga Besi Tempa
Teralis Besi Tempa
Pagar Besi Tempa
No deposit bonus codes - DrmCD
What do no deposit casino bonuses mean? — What หารายได้เสริม you must know: · There are a variety of casino bonuses in play, 성남 출장안마 but 부천 출장마사지 a few are the most 춘천 출장샵 popular. 보령 출장샵 · No
Some 카지노 사이트 추천 operators flinch on the cost, complication, and time dedication of getting licensed, instead selecting to cross their fingers and hope they'll fly under the radar indefinitely. However, the heavy fines most international locations impose for organizing illicit playing activities are just the start of the problems you’ll face if you decide to run an unlicensed on line casino. Make a clear define of your group, including the corporate construction and whether it's a partnership, company, or restricted legal responsibility company. You also needs to|must also} present data on the stakeholders and different key gamers, properly as|in addition to} making staffing projections. A business plan just isn't solely a necessity for operations, but for making use of for a license or even attracting traders.
Posting Komentar