1 / 89

BAB 6

BAB 6. Binary Tree. (Pohon Biner). Sturucture. FATHER. INFO. INFO. INFO. LEFT RIGHT. LEFT RIGHT. LEFT RIGHT. Sturucture. INFO. LEFT RIGHT. atau. typedef struct Node { int INFO; struct Node *LEFT; struct Node *RIGHT; }; typedef struct Node Simpul;.

Télécharger la présentation

BAB 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BAB 6 Binary Tree (Pohon Biner)

  2. Sturucture FATHER INFO INFO INFO LEFT RIGHT LEFT RIGHT LEFT RIGHT

  3. Sturucture INFO LEFT RIGHT atau typedef struct Node { int INFO; struct Node *LEFT; struct Node *RIGHT; }; typedef struct Node Simpul; typedef struct Node { struct Node *LEFT; int INFO; struct Node *RIGHT; }; typedef struct Node Simpul;

  4. Sturucture INFO LEFT RIGHT atau typedef struct Node { int INFO; struct Node *LEFT; struct Node *RIGHT; }; typedef struct Node Simpul; typedef struct Node { struct Node *LEFT; int INFO; struct Node *RIGHT; }; typedef struct Node Simpul;

  5. FATHER typedef struct Node { struct Node *FATHER; int INFO; struct Node *LEFT; struct Node *RIGHT; }; typedef struct Node Simpul; INFO LEFT RIGHT atau typedef struct Node { int INFO; struct Node *FATHER; struct Node *LEFT; struct Node *RIGHT; }; typedef struct Node Simpul;

  6. 6.1.1 Beberapa contoh pohon biner dengan kedalaman (depth) d = 3. root level A 0 B C 1 G D E F 2 J K 3 Gambar-6.2 a Stricly Binary Tree

  7. level A 0 1 2 3 B C D E J K Gambar-6.2 b Stricly Binary Tree

  8. root level 0 1 2 3 depth = 3 A B C D E F G H I J K L M N O Gambar-6.2 c Complete Binary Tree

  9. root level A 0 1 2 3 B C D E F G H I J K L M N Gambar-6.2 d Almost Complete Binary Tree

  10. 6.2 Penomoran Simpul Pohon Biner n 5 2n 2n+1 10 11

  11. A B C D E F G H I 1 0 A B C 3 1 2 2 D E F 4 5 7 G H I 3 10 11 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  12. 6.4 Proses (Operasi) Pada Pohon Biner. 1. Insialisasi 2. Pembuatan sebuah simpul. 3. Pembuatan simpul akar 4. Penambahan (insert) simpul kedalam sebuah pohon 5. Penghapusan (delete) simpul dari sebuah pohon 6. Pembacaan / Penelusuran pohon biner

  13. Mendeklarasikan struktur Simpul Struktur SIMPUL struct Node { struct Node *Left; char INFO; struct Node *Right; }; typedef struct Node Simpul; Simpul *Root, *P, *Q, *R; char X; INFO Left Right X *Root *P *Q *R Pointer Root digunakan khusus menunjuk simpul akar. Pointer P digunakan khusus menunjuk simpul yang baru dibuat Pointer Q, R digunakan sebagai pointer pembantu Pointer-pointer lain dapat ditambahkan bilamana diperlukan Selain itu dideklarasi juga sebuah variabel X bertipe sama dengan tipe INFO yaitu tipe : char

  14. 6.6. Pembuatan Sebuah Simpul P Fungsi untuk Pembuatan Sebuah Simpul void BuatSimpul( char X) { P = (Simpul*) malloc(sizeof(Simpul)); if(P != NULL) { P->INFO = X; P->Left = NULL; P->Right = NULL; } else { printf(“Memory Heap Full”); exit(1); } } A

  15. Instruksi Membuat Sebuah Simpul P = (Simpul*) malloc(sizeof(Simpul)); P

  16. P P->INFO P->RIGHT P->LEFT

  17. Mengisi P->INFO dengan data Misal variabel X Berisi nilai Karakter ‘A’ P->INFO = X; P A

  18. Mengisi P->LEFT dan P->RIGHT Dengan NULL P->Left = NULL; P->Right = NULL; P A

  19. 6.7 Menjadikan Sebuah Simpul Sebagai Simpul Akar Suatu Pohon Fungsi untuk Menjadikan Sebuah Simpul Sebagai Simpul Akar void BuatSimpulAkar( ) { if(Root == NUL) { if(P != NULL) { Root = P; Root->Left = NULL; Root->Right = NULL; } else printf(“\n Simpul Belum Dibuat”); } else printf(“Pohon Sudah Ada”); } Root P A

  20. 6.8 Menambahkan (Insert) Sebuah Simpul ke Pohon Yang Sudah Ada. 6.8.1 Insert urut nomor simpul atau insert level per level. P Root Root A B A P B

  21. 6.8 Menambahkan (Insert) Sebuah Simpul ke Pohon Yang Sudah Ada. 6.8.1 Insert urut nomor simpul atau insert level per level. P Root Root A B A P B Root->LEFT= P;

  22. Root P Root B A P A B

  23. Root P A B Root->RIGHT = P;

  24. 6.8.1 Insert urut nomor simpul atau insert level per level.

  25. Root Root Root Root Root 1 1 1 1 A A A 1 A A 2 3 2 3 2 B B C B C B C 3 2 a b 4 5 D D E c 4 d e Root Root Root 1 1 1 A A A 2 3 2 3 B C B C B C 2 3 5 5 D E F D E F G D E F G 4 6 4 6 4 6 7 7 5 H 8 f g h

  26. int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); } #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<ctype.h> #include<math.h> #include<malloc.h> struct Node { struct Node *Left; char INFO; struct Node *Right; }; typedef struct Node Simpul; Simpul *P, *Root, *Current; Simpul *Q[129]; 0 128 *Q

  27. inisialisasi

  28. int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); } void Inisialisasi () { Root = NULL; P = NULL; } Root P

  29. input pertama kali Buat Simpul dan dijadikan Simpul Akar

  30. int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); } void BuatSimpul( char X) { P = (Simpul…………………); P->INFO = X; P->Left = NULL; P->Right = NULL; } P A

  31. int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); } void BuatSimpulAkar( ) { Root = P; Root->Left = NULL; Root->Right = NULL; } Root P A

  32. input data kedua dan Insert di Root->Left

  33. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; while(Flag == 0 && j < 127) { X = getche(); i++; } } int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); } - - - - - - - - - - - - - Root P A

  34. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; while(Flag == 0 && j < 127) { X = getche(); i++; } } { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Left = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } if(Flag == 0) { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Right = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } } i++; - - - - - - - - - - - - - - - - - - -

  35. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; j i 2 0 1 3 4 5 6 7 while(Flag == 0 && j < 127) { X = getche(); i++; } } Q n 2n 2n+1 - - - - - - - - - - - - - - - Disini berisi alamat simpul Akar (Root) Disini berisi alamat simpul no 5 Root

  36. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; while(Flag == 0 && j < 127) { X = getche(); i++; } } { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Left = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } if(Flag == 0) { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Right = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } } i++; - - - - - - - - - - - - - - - - - - -

  37. { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Left = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } Input data untuk diinsert di Q->Left if(Flag == 0) { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Right = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } } Input data untuk diinsert di Q->Right i++;

  38. Root Input data ke-dua dan Insert di Current->Left Current { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Left = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } A 1 P Sekarang Current masih sama dengan Root B 2 i j 0 1 2 3 4 5 6 7 Q Root P Root Current

  39. if(Flag == 0) { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Right = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } } Root Input data ke-tiga dan Insert di Current->Right Current A 1 P Sekarang Current masih sama dengan Root B C 3 2 i j 0 1 2 3 4 5 6 7 Q Root No.2 P Root Current

  40. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; while(Flag == 0 && j < 127) { X = getche(); i++; } } Root Current A 1 Sudah selesai insert dua simpul P - - - - B C 3 2 i j I++ 0 1 2 3 4 5 6 7 Q Root No.2 P Root Current

  41. void InsertUrutNomor() { int i, j, Flag; char X; Flag = 0; i=1; j=1; Q[i] = Root; while(Flag == 0 && j < 127) { X = getche(); i++; } } Root Current A 1 P - - - - B C 3 2 i j I++ 0 1 2 3 4 5 6 7 Q Root No.2 P Root Current

  42. Root Input data ke-4 { X = getche(); if(X != '0') { BuatSimpul(X); Current = Q[i]; Current->Left = P; j++; Q[j] = P; } else { // data habis Flag = 1; j++; Q[j] = NULL; } Current Pindah Nunjuk Simpul No. 2 A 1 Current kemudian data ke-4 dan Insert di Current-> Left B C 3 2 P C 4 i j 0 1 2 3 4 5 6 7 Q Root No.2 P

  43. void Inisialisasi () { Root = NULL; P = NULL; } void BuatSimpul(char X) { } void BuatSimpulAkar( ) { lihat contoh sebelumnya } void InsertSimpulUrutNomor() { } void BacaUrutNomor() { } int main() { int i, j, Flag; char X; clrscr(); Inisialisasi(); X = getche(); BuatSimpul(X); BuatSimpulAkar(); InsertSimpulUrutNomor(); BacaUrutNomor(); }

  44. void BuatSimpul( char X) { P = (Simpul*) malloc(sizeof(Simpul)); if(P != NULL) { P->INFO = X; P->Left = NULL; P->Right = NULL; } else { printf(“Memory Heap Full”); exit(1); } }

  45. void BacaUrutNomor() { int i,j,n,Counter; i=1; j=1; n=1; Counter=0; printf(“\n”); Q[I] = Root; while(Q[i] != NULL) { Current = Q[i]; printf("%c ", Current->INFO); Counter++; if(Counter == n) { printf(“\n”); Counter=0; n = n*2; } j++; Q[j] = Current->Left; j++; Q[j] = Current->Right; i++; }

More Related