1 / 54

Data Structure Tree

Data Structure Tree. Disusun Oleh : Budi Arifitama Pertemuan ke - 8. Objectives . Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss Binary Tree Examine a binary tree example. Sub Topi c. Tree Introduction

ranger
Download Presentation

Data Structure 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. Data Structure Tree DisusunOleh : Budi Arifitama Pertemuan ke-8

  2. Objectives • Define trees as data structures • Define the terms associated with trees • Discuss tree traversal algorithms • Discuss Binary Tree • Examine a binary tree example

  3. Sub Topic • Tree Introduction • Term Associated With Tree • Binary Tree • Traverse Tree

  4. Trees • A treeis a nonlinear data structure used to represent entities that are in some hierarchical relationship • Examples in real life: • Family tree • Table of contents of a book • Class inheritance hierarchy in Java • Computer file system (folders and subfolders) • Decision trees • Top-down design

  5. Example: Computer File System Root directory of C drive Documents and Settings Program Files My Music Desktop Favorites Start Menu Adobe Microsoft Office

  6. Tree Example

  7. Tree Definition • Tree: a set of elements of the same type such that • It is empty • Or, it has a distinguished element called the root from which descend zero or more trees (subtrees) • What kind of definition is this? • What is the base case? • What is the recursive part?

  8. Tree Terminology Interior nodes Root Leaf nodes

  9. Tree Terminology • Nodes: the elements in the tree • Edges: connections between nodes • Root: the distinguished element that is the origin of the tree • There is only one root node in a tree • Leaf node: a node without an edge to another node • Interior node: a node that is not a leaf node • Empty tree has no nodes and no edges

  10. Tree Terminology • Parent or predecessor: the node directly above in the hierarchy • A node can have only one parent • Childor successor: a node directly below in the hierarchy • Siblings: nodes that have the same parent • Ancestors of a node: its parent, the parent of its parent, etc. • Descendants of a node: its children, the children of its children, etc.

  11. Discussion • Does a leaf node have any children? • Does the root node have a parent? • How many parents does every node other than the root node have?

  12. Height of a Tree • Apathis a sequence of edges leading from one node to another • Length of a path: number of edges on the path • Height of a(non-empty)tree : length of the longest path from the root to a leaf • What is the height of a tree that has only a root node? • By convention, the height of an empty tree is -1

  13. Level of a Node • Level of a node : number of edges between root and node • It can be defined recursively: • Level of root node is 0 • Level of a node that is not the root node is level of its parent + 1 • Question: What is the level of a node in terms of path length? • Question: What is the height of a tree in terms of levels?

  14. Level of a Node Level 0 Level 1 Level 2 Level 3

  15. Subtrees • Subtree of a node: consists of a child node and all its descendants • A subtree is itself a tree • A node may have many subtrees

  16. Subtrees Subtrees of the root node

  17. Subtrees E Subtrees of the node labeled E

  18. More Tree Terminology • Degreeorarityof anode: the number of children it has • Degreeorarityof atree: the maximum of the degrees of the tree’s nodes

  19. Tree Terminolgy

  20. Ancestor (F)? Predesesor (F) ? Descendant (B)? Succesor (B)? Parent (I)? Child (C)? Sibling (G)? Size? Height? Root? Leaf? Degree (C)? Latihan

  21. Latihan Gambarkan tree dari representasi berikut : DF DB KJ KL BA BC H D HK FE FG JI Tentukan : Root Leaf Heigth Child H Parent A

  22. Binary Tree • Finite (possibly empty) collection of elements. • A nonempty binary tree has a root element. • The remaining elements (if any) are partitioned into two binary trees. • These are called the left and right subtrees of the binary tree.

  23. Binary Tree • There are many variations on trees but we will start with binary trees • binary tree: each node has at most two children • the possible children are usually referred to as the left child and the right child parent right child left child

  24. Full Binary Tree • full binary tree: a binary tree is which each node was exactly 2 or 0 children

  25. Question • What is the maximum height of a full binary tree with 11 nodes? • 1 • 3 • 5 • 7 • Not possible to construct a full binary tree with 11 nodes. Binary Trees

  26. Complete Binary Tree • complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible Where would the next node goto maintain a complete tree? Binary Trees

  27. Perfect Binary Tree • perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. • a perfect binary tree has the maximum number of nodes for a given height • a perfect binary tree has (2(n+1) - 1) nodes where n is the height of the tree • height = 0 -> 1 node • height = 1 -> 3 nodes • height = 2 -> 7 nodes • height = 3 -> 15 nodes Binary Trees

  28. Differences Between A Tree & A Binary Tree • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • A binary tree may be empty; a tree cannot be empty.

  29. a a b b Differences Between A Tree & A Binary Tree • The subtrees of a binary tree are ordered; those of a tree are not ordered. • Are different when viewed as binary trees. • Are the same when viewed as trees.

  30. Contoh Binary Tree • Representasi ekspresi arithmatik

  31. Minimum Number Of Nodes • Minimum number of nodes in a binary tree whose height is h. • At least one node at each of first h levels. minimum number of nodes is h

  32. Maximum Number Of Nodes • All possible nodes at first h levels are present. Maximum number of nodes = 1 + 2 + 4 + 8 + … + 2h-1 = 2h - 1

  33. Representation of binary tree • A binary tree can bee represented by an Array or a linked list.

  34. RepresentasiTree (Array) Array H D K B F J L A C E G I 0 1 3 4 6 8 9 11 7 10 2 5

  35. I A H K B G D J E F C L Linked Representation root element rightChild leftChild

  36. b 1 a 2 3 c 4 5 6 7 d f e g 8 9 10 h i j tree[] 5 10 Array Representation a b c d e f g h i j 0

  37. 1 a 3 b 15 7 d c Latihan

  38. 1 a 3 b 15 7 d c tree[] a - b - - - c - - - - - - - d 0 5 10 15 Right-Skewed Binary Tree

  39. Binary Tree Traversal

  40. Definisi • Penelusuran seluruh node pada binary tree. • Metode : • Preorder • Inorder • Postorder • Level order

  41. PreOrder Traversal • Preorder traversal • Cetak data pada root • Secara rekursif mencetak seluruh data pada subpohon kiri • Secara rekursif mencetak seluruh data pada subpohon kanan

  42. a b c Preorder Example (visit = print) a c b

  43. a b c f e d j g h i Preorder Example (visit = print) a b d g h e i c f j

  44. / + * e f + - a b c d Preorder Of Expression Tree / * + a b - c d + e f Gives prefix form of expression!

  45. InOrder Traversal • Inorder traversal • Secara rekursif mencetak seluruh data pada subpohon kiri • Cetak data pada root • Secara rekursif mencetak seluruh data pada subpohon kanan

  46. a b c Inorder Example (visit = print) b a c

  47. a b c f e d j g h i Inorder Example (visit = print) g d h b e i a f j c

  48. Postorder Traversal • Postorder traversal • Secara rekursif mencetak seluruh data pada subpohon kiri • Secara rekursif mencetak seluruh data pada subpohon kanan • Cetak data pada root

  49. a b c Postorder Example (visit = print) b c a

  50. a b c f e d j g h i Postorder Example (visit = print) g h d i e b j f c a

More Related