1 Pertemuan 11 Tree & Binary Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat mengembangkan program modular yang menggunakan ADT tree
3 Outline Materi pengertian dan kegunaan TreeADT tree Terminologi Tree Pengertian Binary Tree Operasi Traversal Tree Representasi Tree: Array & linked-list
4 One-to-Many data relational (Hierarchy). Definition : A tree is a finite set of one or more nodes such that : –There is a specially designed node called the ROOT. –The remaining node a partitioned into n>=0 disjoint sets T1,…,Tn, where each of these set is a tree. T1,...,Tn are subtrees of the ROOT. There is an instance of recursive definition since the subtrees as trees. Two types of data presentation (genealogical) : –Pedigree chart (figure A: S ancestor) –Lineal chart (figure B : descendant) TREE Dusty Honey BearBrandy BrundhildeTerry Figure B. Lineal Chart Proto Indo-European Italic Hellenic Osco-Umbrian Latin Germanic Figure A. Pedigree Chart
5 Normally we draw trees with root at the top. The degree of node is the number of subtrees of the node. The degree of tree is the maximum degree of the node in the tree. Height/depth is the maximum level of any node in the tree. Parent-Child. Siblings are Children of the same Parent Ancestors Descendant TREE A BCD EFG DEGREE of C : 2 DEGREE of Tree : 3 HEIGHT: 3 PARENT of C : A CHILDREN of A : B, C, D SIBLING of F : G ANCESTOR of F : A, C DESCENDANT of C :F, G TREE Level 1 Level 2 Level 3
6 Set of nodes that is either empty or consist of a root and two disjoint binary trees called left subtree and right subtree. Degree of any given node must not exceed two. Maximum number of nodes : –On level i : 2, i >= 1 –In a Binary Tree of depth k : 2 -1, k >=1 Types –Complete Binary Tree –Skewed Binary Tree Full Binary Tree is Binary Tree that has maximum number of nodes (2 -1) Binary Tree A BC D Complete Binary Tree E A C G Skewed Binary Tree i -1 k k
7 Traversing is visiting each node in the tree. Type/name of traversals : 1.PreOrder(VLR) 2.InOder(LVR) 3.PostOrder(LRV) Binary Tree Traversals N T1T2 Traversal Algorithms on Binary Tree PreOrder : –Visit Root (N) –Move Left SubTree(T1) –Move Right SubTree(T2) InOrder : –Move Left SubTree(T1) –Visit Root (N) –Move Right SubTree(T2) PostOrder : –Move Left SubTree(T1) –Move Right SubTree(T2) –Visit Root (N)
8 PreOrder (ROOT n) {if (n != NULL){ printf(n->info); PreOrder(n->Left); PreOrder(n->Right);} InOrder (ROOT n) {if (n != NULL){ InOrder(n->Left); printf(n->info); InOrder(n->Right);} PostOrder (ROOT n) {if (n != NULL){ PostOrder(n->Left); PostOrder(n->Right); printf(n->info);} Traversal Implementations Traversals Code on C
9 Array Representation Root’s Index is 0 Left Child’s Index is 2p + 1, p is parent’s index Right Child’s Index is 2p + 2 Parent’s Index is (c-1)/2, c is child’s index A BC DEF Binary Tree GH ABCDEFGH Array
10 Linked List Representation Data struct node { Elemen_Type Data; struct node *Left; struct node *Right; } struct node { Elemen_Type Data; struct node *Left; struct node *Right; struct node *Parent;} LeftRight Data LeftRight Data LeftRight Data LeftRight Data LeftRight Parent Data LeftRight Parent Data LeftRight Parent Data LeftRight Parent NULL Double Linked List Multiple Linked List
11 For each node contains operand or operator for arithmetic expression. Traversals : –InOrder, produce Infix –PreOrder, produce Prefix –PostOrder, produce Postfix Expression Tree + x7 5 8 TraversalNotasi InOrder5 x PreOrder+ x PostOrder5 8 x 7 +