Data Structure Tree
description
Transcript of Data Structure Tree
![Page 1: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/1.jpg)
DATA STRUCTURE
TREEDisusun Oleh : Budi Arifitama
Pertemuan ke-8
![Page 2: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/2.jpg)
OBJECTIVES• Define trees as data structures• Define the terms associated with trees• Discuss tree traversal algorithms• Discuss Binary Tree• Examine a binary tree example
![Page 3: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/3.jpg)
SUB TOPIC Tree Introduction Term Associated With Tree Binary Tree Traverse Tree
![Page 4: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/4.jpg)
TREES• A tree is 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
10-4
![Page 5: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/5.jpg)
EXAMPLE: COMPUTER FILE SYSTEM
10-5
Root directory of C drive
Documents and Settings Program Files My Music
Desktop Favorites Start Menu Microsoft OfficeAdobe
![Page 6: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/6.jpg)
TREE EXAMPLE
![Page 7: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/7.jpg)
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?
10-7
![Page 8: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/8.jpg)
TREE TERMINOLOGY
10-8
Leaf nodes
RootInterior nodes
![Page 9: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/9.jpg)
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-9
![Page 10: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/10.jpg)
• Parent or predecessor: the node directly above in the hierarchy• A node can have only one parent
• Child or 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.
10-10
Tree Terminology
![Page 11: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/11.jpg)
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?
10-11
![Page 12: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/12.jpg)
HEIGHT OF A TREE• A path is 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
10-12
![Page 13: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/13.jpg)
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?
10-13
![Page 14: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/14.jpg)
LEVEL OF A NODE
10-14
Level 0
Level 1
Level 2
Level 3
![Page 15: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/15.jpg)
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
10-15
![Page 16: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/16.jpg)
SUBTREES
10-16
Subtrees of the root node
![Page 17: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/17.jpg)
SUBTREES
10-17
Subtrees of the node labeled E
E
![Page 18: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/18.jpg)
MORE TREE TERMINOLOGY• Degree or arity of a node: the number of
children it has
• Degree or arity of a tree: the maximum of the degrees of the tree’s nodes
10-18
![Page 19: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/19.jpg)
TREE TERMINOLGY
![Page 20: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/20.jpg)
LATIHANAncestor (F)?Predesesor (F) ?Descendant (B)?Succesor (B)?Parent (I)?Child (C)?Sibling (G)?Size?Height?Root?Leaf?Degree (C)?
![Page 21: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/21.jpg)
LATIHANGambarkan tree dari representasi berikut :
DFDBKJKLBABCH DHKFEFGJI
Tentukan :1. Root2. Leaf3. Heigth4. Child H5. Parent A
![Page 22: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/22.jpg)
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.
![Page 23: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/23.jpg)
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 childparent
left child right child
![Page 24: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/24.jpg)
24
FULL BINARY TREE full binary tree: a binary tree is which
each node was exactly 2 or 0 children
![Page 25: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/25.jpg)
QUESTION What is the maximum height of a full
binary tree with 11 nodes?A. 1B. 3C. 5D. 7E. Not possible to construct a full binary
tree with 11 nodes.
CS314Binary Trees 25
![Page 26: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/26.jpg)
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 CS314
![Page 27: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/27.jpg)
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 CS314
![Page 28: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/28.jpg)
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.
![Page 29: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/29.jpg)
DIFFERENCES BETWEEN A TREE & A BINARY TREE
The subtrees of a binary tree are ordered; those of a tree are not ordered.
a
b
a
b
• Are different when viewed as binary trees.• Are the same when viewed as trees.
![Page 30: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/30.jpg)
CONTOH BINARY TREE Representasi ekspresi arithmatik
![Page 31: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/31.jpg)
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
![Page 32: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/32.jpg)
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
![Page 33: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/33.jpg)
REPRESENTATION OF BINARY TREE A binary tree can bee represented by an
Array or a linked list.
![Page 34: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/34.jpg)
REPRESENTASI TREE (ARRAY)
H D K B F J L A C E G I
Array
0 1 2 3 4 5 6 7 8 9 10 11
![Page 35: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/35.jpg)
LINKED REPRESENTATIONH
KD
B
A
LJ
I
leftChild element rightChild
root
F
CE G
![Page 36: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/36.jpg)
ARRAY REPRESENTATION
tree[] 5 10
a b c d e f g h i j
b
a
c
d e f gh i j
1
2 3
4 5 6 78 9 10
0
![Page 37: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/37.jpg)
LATIHAN
ab
13
c7
d 15
![Page 38: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/38.jpg)
RIGHT-SKEWED BINARY TREE
ab
13
c7
d 15
tree[] 0 5 10
a - b - - - c - - - - - - -15d
![Page 39: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/39.jpg)
BINARY TREE TRAVERSAL
![Page 40: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/40.jpg)
DEFINISI Penelusuran seluruh node pada binary
tree. Metode :
Preorder InorderPostorderLevel order
![Page 41: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/41.jpg)
PREORDER TRAVERSAL Preorder traversal
1. Cetak data pada root2. Secara rekursif mencetak seluruh data
pada subpohon kiri3. Secara rekursif mencetak seluruh data
pada subpohon kanan
![Page 42: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/42.jpg)
PREORDER EXAMPLE (VISIT = PRINT)
a
b c
a b c
![Page 43: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/43.jpg)
PREORDER EXAMPLE (VISIT = PRINT)
a
b c
d e f
g h i j
a b d g h e i c f j
![Page 44: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/44.jpg)
PREORDER OF EXPRESSION TREE
+
a b
-
c d
+
e f
*
/
Gives prefix form of expression!
/ * + a b - c d + e f
![Page 45: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/45.jpg)
INORDER TRAVERSAL Inorder traversal
1.Secara rekursif mencetak seluruh data pada subpohon kiri
2.Cetak data pada root3.Secara rekursif mencetak seluruh data pada
subpohon kanan
![Page 46: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/46.jpg)
INORDER EXAMPLE (VISIT = PRINT)
a
b c
b a c
![Page 47: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/47.jpg)
INORDER EXAMPLE (VISIT = PRINT)
a
b c
d e f
g h i j
g d h b e i a f j c
![Page 48: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/48.jpg)
POSTORDER TRAVERSAL Postorder traversal
1.Secara rekursif mencetak seluruh data pada subpohon kiri
2.Secara rekursif mencetak seluruh data pada subpohon kanan
3.Cetak data pada root
![Page 49: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/49.jpg)
POSTORDER EXAMPLE (VISIT = PRINT)
a
b c
b c a
![Page 50: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/50.jpg)
POSTORDER EXAMPLE (VISIT = PRINT)
a
b c
d e f
g h i j
g h d i e b j f c a
![Page 51: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/51.jpg)
POSTORDER OF EXPRESSION TREE
+
a b
-
c d
+
e f
*
/
Gives postfix form of expression!
a b + c d - * e f + /
![Page 52: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/52.jpg)
LATIHAN Telusuri pohon biner berikut dengan
menggunakan metode pre, in, post, dan level traversal.
![Page 53: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/53.jpg)
LATIHAN 1
a. b.
+
*
3 5
-
2 /
8 4
![Page 54: Data Structure Tree](https://reader033.fdocuments.in/reader033/viewer/2022061610/56816668550346895dd9fde0/html5/thumbnails/54.jpg)
LATIHAN 2