1 / 19

Tree

Tree. Binary Tree. Tree yang punya anak tak lebih dari 2, yaitu kiri atau kanan. Binary Search Tree. Yaitu binary tree yang data anak kiri < data root < data anak kanan. Representasi Array. Representasi Linked List. Insert data ke BST. Algoritma

Download Presentation

Tree

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. Tree

  2. Binary Tree • Tree yang punya anak tak lebih dari 2, yaitu kiri atau kanan

  3. Binary Search Tree • Yaitu binary tree yang data anak kiri < data root < data anak kanan

  4. Representasi Array

  5. Representasi Linked List

  6. Insert data ke BST Algoritma • Telusuri tree, cari tempat yang cocok untuk data yang mau dimasukkan • Ketika sampai di tempat yang cocok, data yang mau dimasukkan dibandingkan dengan parent, kalau lebih kecil maka taruh di sisi kiri, kalau lebih besar taruh di sisi kanan

  7. Insert data ke BST • Taruh dimana? 55

  8. Insert data ke BST 20 • Taruh dimana? 55

  9. Insert data ke BST 55 20

  10. Traversal / Kunjungan Traversal : Mengunjungisetiap node pada tree Adatigacaramengunjungi node-node padasuatu tree : Preorder : root, kiri, kanan Inorder : kiri, root, kanan Postorder : kiri, kanan, root

  11. A A B C D E F B C H G D E F G Preorder : root, kiri, kanan A B D E C F G A B D G H C E F

  12. A B C D E F H G Inorder : kiri, root, kanan void inorder(structtnode *p) { if(p != NULL) { inorder(p->lchild); printf("%d\t",p->data); inorder(p->rchild); } } • Buat program untukmenelusuri tree secara preorder danpostorder B G D H A E C F

  13. Buat program untukmenelusuri tree secara preorder danpostorder

  14. A B C D E F H G Postorder : kiri, kanan, root G H D B E F C A

  15. Traversal • Z,A,Q,P,Y,X,C,B = Preorder root, kiri, kanan • Q,A,Z,Y,P,C,X,B = Inorderkiri, root, kanan • Z,A,P,X,B,C,Y,Q =postorderkiri, kanan, root

  16. 70 60 85 40 65 20 void inorder(structtnode *p) { if(p != NULL) { inorder(p->lchild); printf("%d\t",p->data); inorder(p->rchild); } } 70 60 85 40 65 20 • Inorder : kiri root kanan 20 40 60 65 70 85

  17. 70 60 85 40 65 20 void preorder(structtnode *p) { if(p != NULL) { printf("%d\t",p->data); preorder(p->lchild); preorder(p->rchild); } } 70 60 85 40 65 20 • Preorder : root, kiri, kanan70 60 40 20 65 85

  18. 70 60 85 40 65 20 void postorder(struct node *p) { if(p != NULL) { postorder(p->lchild); postorder(p->rchild); printf("%d\t",p->data); } } 70 60 85 40 65 20 • Postorder : kiri, kanan, root 20 40 65 60 85 70

  19. Menghitung node pada tree int count(structtnode *p) { if( p == NULL) return(0); else if( p->lchild == NULL && p->rchild == NULL) return(1); else return(1 + (count(p->lchild) + count(p->rchild))); } 70 60 85 40 65 20

More Related