Download presentation

Presentation is loading. Please wait.

1
**BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS CHAPTER 4 TREES BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS SITI ZANARIAH SATARI FIST/FSKKP UMP I0910 CHAPTER 4 1

2
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CONTENT CHAPTER 4 TREES 4.1 Introduction to Trees 4.2 Application of Trees 4.3 Tree Traversal 4.4 Trees and Sorting (Data structure) 4.5 Spanning Trees 4.6 Minimum Spanning Trees CHAPTER 4 2

3
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES 4.1 INTRODUCTION TO TREES Define and recognize a tree Define and recognize a rooted tree Define and recognize a m-ary tree CHAPTER 4 3

4
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

A Tree 4.1 INTRODUCTION TO TREES A tree is a connected undirected graph with no simple circuit. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. THEOREM: A tree with n vertices has n – 1 edges. EXAMPLES a b c a b c a b c d e f d e f d e f NOT A TREE A TREE NOT A TREE CHAPTER 4

5
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.1 4.1 INTRODUCTION TO TREES Which of the following graphs are trees? a b c a b c d e d e FIGURE 1 FIGURE 2 a v2 v1 v3 c d e v5 b v4 CHAPTER 4 FIGURE 3 FIGURE 4

6
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

A Rooted Tree 4.1 INTRODUCTION TO TREES A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. Different choice of root produce different rooted tree EXAMPLES c f g a a b d d c b e b d a e f g e f g c A TREE with root a A TREE with root c A TREE CHAPTER 4

7
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Properties of Rooted Trees 4.1 INTRODUCTION TO TREES Parent – A vertex other than root is a parent if it has one or more children The parent of c is b Children – If A is a vertex with successors B and C, then B and C are the children of A. The children of a is b, f and g Siblings – Children with the same parent vertex. h, i and j are siblings Level – the length of the unique path from the root to a vertex Vertex a is at level 0 Vertices d and e is at level 3 EXAMPLES a b g f j c h i d e k l m Height – The maximum level of all the vertices The height of this tree is 3. CHAPTER 4

8
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Properties of Rooted Trees 4.1 INTRODUCTION TO TREES Ancestor of a vertex (v) – the vertices in the path from the root to this vertex excluding this vertex. The ancestors of e are c, b and a Descendent of a vertex (v) – vertices that have v as ancestor. The descendants of b are c, d and e Leaf – A vertex with no children The leaves are d, e, f, i, k, l and m Internal Vertices – vertices that have children The internal vertices are a, b, c, g, h and j EXAMPLES a b g f j c h i d e k l m A subtree rooted at g Subtree – A subgraph of the tree consisting of a root and its descendent and all edges incident to these descendent. CHAPTER 4

9
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.1 4.1 INTRODUCTION TO TREES Answer these questions about the rooted tree illustrated. Which vertex is the root? Which vertices are internal? Which vertices are leaves? Which vertices are children of g? Which vertex is the parent of o? Which vertices are siblings of e? Which vertices are ancestors of m? Which vertices are descendants of d? a b d Find a subgraph. Find the level of each vertex. What is the height of this tree? Is this tree a balanced tree? c e g f h i o j k l m n p r q CHAPTER 4 s

10
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

m-ary Tree 4.1 INTRODUCTION TO TREES A rooted tree is called an m-ary tree if every vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. A rooted m-ary tree is balanced if all leaves are at levels h or h-1. EXAMPLES c a a a b e c d e b d c b d f g k h i j l m f g e f g A full binary TREE A 3-ary TREE A full 4-ary TREE CHAPTER 4

11
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

m-ary Tree 4.1 INTRODUCTION TO TREES THEOREM 1: A full m-ary tree with i internal vertices contains n = mi + 1 vertices THEOREM 2: A full m-ary tree with n vertices has internal vertices and leaves. i internal vertices has vertices and leaves. l leaves has vertices and internal vertices. THEOREM 3: There are at most leaves in an m-ary tree of height h. CHAPTER 4

12
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE: m-ary tree 4.1 INTRODUCTION TO TREES Suppose that someone starts a chain letter. Each person who receives the letter is asked to send it on to four other people. Some people do this, but others do not send any letters. How many people have seen the letter, including the first person, if no one receives more than one letter and if the chain letter ends after there have been 100 people, who read it but did not send it out? Solution: Send letter to four other people ⇛ 4-ary tree ⇛ m = 4 People send out the letter ⇛ internal vertices ⇛ i People do not send out the letter ⇛ leaves ⇛ l = 100 By theorem 2 (iii): Number of people have seen this letter ⇛ n = (4·100 – 1) / (4 – 1) = 133 Number of people send out the letter ⇛ i = 133 – 100 = 33 CHAPTER 4

13
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.1 4.1 INTRODUCTION TO TREES A chain letter starts with a person sending a letter out to 10 others. Each person is asked to send the letter out to 10 others, and each letter contains a list of previous six people in the chain. Unless there are fewer than six names in the list, each person sends one dollar to the first person from the list, moves up each of other five names one position, and insert his names at the end of this list. If no person breaks the chain and no one receives more than one letter, how much money will a person in the chain ultimately receive? CHAPTER 4

14
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.1 : EXTRA 4.1 INTRODUCTION TO TREES PAGE : 693, 694 and 695 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. CHAPTER 4

15
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES 4.2 APPLICATION OF TREES Introduce Binary Search Trees Introduce Decision Trees Introduce Game Trees CHAPTER 4

16
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Binary Search Trees 4.2 APPLICATION OF TREES EXAMPLE A binary tree in which each child of a vertex is designated as a right or left child No vertex has more than one right child or left child Each vertex is labeled with a key Vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree. Binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry using alphabetical order mathematics geography physics chemistry geology zoology metereology psycology CHAPTER 4

17
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Decision Trees 4.2 APPLICATION OF TREES EXAMPLE A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of decision. The possible solutions of the problem correspond to the paths to the leaves of this rooted tree. A Decision tree that orders the elements of the list a, b, c a : b a > b a < b a : c b : c a > c a < c b > c b < c b : c c > a > b a : c c > b > a b > c b < c a < c a > c a > b > c a > c > b b > a > c b > c > a CHAPTER 4

18
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Game Trees 4.2 APPLICATION OF TREES Use trees to analyze certain types of games Vertices represent the positions can be in as it progresses Edges represent legal moves between this position Games tree is infinite if the games they represent never end EXAMPLE A Game tree that represents the first two level of the tic-tac-toe game. CHAPTER 4

19
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.2 : EXTRA 4.2 APPLICATION OF TREES PAGE : 708, 709 and 710 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. CHAPTER 4

20
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES 4.3 TREE TRAVERSAL Determine preorder traversal, inorder traversal and postorder traversal of an ordered rooted tree. Determine the infix, prefix, and postfix form of an expression. CHAPTER 4

21
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Tree Traversal 4.3 TREE TRAVERSAL Ordered trees are often used to restore data/info. Tree traversal is a procedure for systematically visiting each vertex of an ordered rooted tree to access data. If the tree is label by Universal Address System we can totally order the vertices using lexicographic ordering Example: 0 < 1 < 1.1 < 1.2 < < 1.3 < 2 < 3 < 3.1 < 3.1.1 < < < < 4 < 4.1 Tree traversal algorithm Preorder, inorder and postorder traversal CHAPTER 4

22
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Preorder Traversal 4.3 TREE TRAVERSAL Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. If T1, T2, …, Tn are subtrees at r from left to right in T, then the preorder traversal begins by visiting r, continues by traversing T1 in preorder, then T2 in preorder, and so on until Tn is traversed in preorder. STEP 1 Visit r r TIPS Preorder Traversal: Visit root, visit subtrees left to right T1 T2 … Tn STEP Visit T1 in preorder STEP Visit T2 in preorder STEP n Visit Tn in preorder CHAPTER 4

23
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE: Preorder Traversal 4.3 TREE TRAVERSAL a a• b• e f• c• d• g h• i• T d b c e f k m g h l i j j k m l n ITERATION 2 p o n p o a• b• e• j• k f• c• d• g• l• m• h• i• a• b c• d n ITERATION 3 p o e f g h i a• b• e• j• k• n• o• p• f• c• d• g• l• m• h• i• k j m l ITERATION 4 n p o ITERATION 1 The preorder traversal of T CHAPTER 4

24
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Inorder Traversal 4.3 TREE TRAVERSAL Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. If T1, T2, …, Tn are subtrees at r from left to right in T, then the inorder traversal begins by traversing T1 in inorder, then visiting r, continues by traversing T2 in inorder, and so on until Tn is traversed in inorder. STEP 2 Visit r r TIPS Inorder Traversal: Visit leftmost subtree, Visit root, Visit other subtrees left to right. T1 T2 … Tn STEP Visit T1 in inorder STEP Visit T2 in inorder STEP n Visit Tn in inorder CHAPTER 4

25
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE: Inorder Traversal 4.3 TREE TRAVERSAL a e b• f• a• c• g d• h• i• T d b c e f k m g h l i j j k m l n ITERATION 2 p o n p o j• e• b• f• a• c• k l• g• m• d• h• i• b a• c• d n ITERATION 3 p o e f g h i j• e• n• k• o• p• b• f• a• c• l• g• m• d• h• i• k j m l ITERATION 4 n p o ITERATION 1 The inorder traversal of T CHAPTER 4

26
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Postorder Traversal 4.3 TREE TRAVERSAL Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. If T1, T2, …, Tn are subtrees at r from left to right in T, then the preorder traversal begins by traversing T1 in postorder, then T2 in postorder, and so on until Tn is traversed in postorder and ends by visiting r. STEP n+1 Visit r r TIPS Postorder Traversal: Visit subtrees left to right, Visit root. T1 T2 … Tn STEP Visit T1 in postorder STEP Visit T2 in postorder STEP n Visit Tn in postorder CHAPTER 4

27
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE: Postorder Traversal 4.3 TREE TRAVERSAL a e f• b• c• g h• i• d• a• T d b c e f k m g h l i j j k m l n ITERATION 2 o p n p o j• e• f• b• c• l• m• g• h• i• d• a• k b c• d a• n ITERATION 3 o p e f g h i j• n• o• p• k• e• f• b• c• l• m• g• h• i• d• a• k j m l ITERATION 4 n p o ITERATION 1 The preorder traversal of T CHAPTER 4

28
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.3 4.3 TREE TRAVERSAL Determine the order in which a preorder, inorder, and postorder traversal visits the vertices of the following rooted tree. a a b d b g f c e g f j c h h i i j k l o m d e p k l n m r q s FIGURE 1 FIGURE 2 CHAPTER 4

29
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Represent Expression by Rooted Tree 4.3 TREE TRAVERSAL We can represent complicated expression (propositions, sets, arithmetic) using ordered rooted trees. EXAMPLE: A binary tree representing ((x+y)↑2)+((x-4)/3) + ↑ / _ + 2 3 x y x 4 CHAPTER 4

30
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Infix, Prefix & Postfix Notation 4.3 TREE TRAVERSAL We obtain the Infix form of an expression when we traverse its rooted tree in Inorder. The infix form for expression ((x+y)↑2)+((x-4)/3) is x + y ↑2 + x – 4 / 3 or ((x+y)↑2)+((x-4)/3) We obtain the Prefix form of an expression when we traverse its rooted tree in Preorder. The prefix form for expression ((x+y)↑2)+((x-4)/3) is + ↑ + x y 2 / - x 4 3 We obtain the Postfix form of an expression when we traverse its rooted tree in Postorder. The postfix form for expression ((x+y)↑2)+((x-4)/3) is x y + 2 ↑ x 4 – 3 / + CHAPTER 4

31
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Evaluating Prefix Expression 4.3 TREE TRAVERSAL Working right to left and performing operations using the operands on the right. EXAMPLE: The value of the prefix expression + - * / ↑ is 3 + - * / ↑ 2 3 4 + - * 2 ↑ 3 = 8 2 * 3 = 6 + - * / 8 4 8/4 =2 6 – 5 =1 + - * + 1 2 2 * 3 = 6 1 + 2 = 3 CHAPTER 4

32
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Evaluating Postfix Expression 4.3 TREE TRAVERSAL Working left to right and performing operations using the operands on the left. EXAMPLE: The value of the postfix expression * - 4 ↑ 9 3 / + is 4 7 2 3 * - 4 ↑ 9 3 / + 1 4 ↑ 9 3 / + 2 * 3 = 6 1 ↑ 4 = 1 ↑ 9 3 / + 1 9 3 / + 7 - 6 = 1 9/3 = 3 1 4 ↑ 9 3 / + 1 3 + 1 ↑ 4 = 1 1 + 3 = 4 CHAPTER 4

33
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.3 4.3 TREE TRAVERSAL Represent the following expression using binary trees. Then write these expression in infix, prefix and postfix notations. ((x+2)↑3)*(y – (3+x)) – 5 (A∩B) – (A∪(B – A)) What is the value of these expression in prefix expression? + – ↑3 2 ↑ 2 3 / 6 – 4 2 * ↑ What is the value of these expression in postfix expression? 3 2 * 2 ↑ 5 3 – 8 4 / * – 9 3 / – * CHAPTER 4

34
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.3 : EXTRA 4.3 TREE TRAVERSAL PAGE : 722, 723 and 724 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. CHAPTER 4

35
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES 4.5 SPANNING TREES Find spanning trees of a simple graph CHAPTER 4

36
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Spanning Trees 4.5 SPANNING TREES Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. A simple graph is connected if and only if it has a spanning tree. Applied in IP multitasking. a b c d a b c d Not Spanning tree e f g a b c d e f g Spanning tree A simple graph e f g CHAPTER 4

37
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.5 4.5 SPANNING TREES Find a spanning tree for the following graphs. b a b f e a c g h d e d c FIGURE 1 FIGURE 2 b a b a e c c d d FIGURE 3 FIGURE 4 CHAPTER 4

38
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 4.5 : EXTRA 4.5 SPANNING TREES PAGE : 734, 735, 736 and 737 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. CHAPTER 4

39
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES 4.6 MINIMUM SPANNING TREES Find minimum spanning tree using Prim’s algorithm Find minimum spanning tree using Kruskal’s algorithm CHAPTER 4

40
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS Minimum Spanning Trees 4.6 MINIMUM SPANNING TREES A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of it edges. Two algorithms can be used: Prim’s Algorithm Robert Prim, 1957 Kruskal’s Algorithm Joseph Kruskal, 1956 CHAPTER 4

41
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS Prim’s Algorithm 4.6 MINIMUM SPANNING TREES Chose an edge with the least weight. Include it in spanning tree, T. Select an edge of least weight that is incident with a vertex of an edge in T. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. Repeat STEPS 3 and 4 until T contains n-1 edges. There may be more than one minimum spanning tree for a given connected weighted simple graph. If there are two edges with similar smallest weight, chose either one. CHAPTER 4

42
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXAMPLE: Prim’s Algorithm 4.6 MINIMUM SPANNING TREES c CHOICE EDGE WEIGHT SPANNING TREE 1 {a, b} 7 2 {a, d} 10 3 {b, e} 11 4 {e, c} 12 13 13 b 10 b d 16 7 12 15 7 a 10 11 a b d e 12 7 10 FIGURE 1 a The minimum spanning tree is given by: b d 7 10 c 11 a e d 12 b c d 12 b 7 10 11 e a 7 10 11 e Total weight = 40 a CHAPTER 4

43
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS Kruskal’s Algorithm 4.6 MINIMUM SPANNING TREES Arrange the edges in G in increasing order. Chose an edge with the minimum weight. Include it in spanning tree, T. Add an edge of least weight to T. If it does not create a cycle (simple circuit) with the edges in T, then include it in T; otherwise discard it. Repeat STEPS 4 and 5 until T contains n-1 edges. There may be more than one minimum spanning tree for a given connected weighted simple graph. If there are two edges with similar smallest weight, chose either one. CHAPTER 4

44
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXAMPLE: Kruskal’s Algorithm 4.6 MINIMUM SPANNING TREES c CHOICE EDGE WEIGHT SPANNING TREE 1 {a, b} 7 2 {b, d} 10 3 {b, e} 11 4 {e, c} 12 13 13 b 10 b d 16 7 12 15 7 a 10 11 a b d e 12 10 7 FIGURE 1 a The minimum spanning tree is given by: b d 10 7 c 11 a e b d c 10 12 b 7 d 10 11 e 12 a 7 11 e Total weight = 40 a CHAPTER 4

45
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 4.6 4.6 MINIMUM SPANNING TREES Construct a minimum spanning tree for each of the following connected weighted graphs using Prim’s and Kruskal’s algorithm. a b c 8 3 11 f 1 6 1 2 1 e 2 8 a b e 5 3 7 6 f 8 1 2 1 3 h g 1 5 d 7 c d FIGURE 1 FIGURE 2 CHAPTER 4

46
**4.6 MINIMUM SPANNING TREES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 4.6 : EXTRA 4.6 MINIMUM SPANNING TREES PAGE : 742 and 743 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. CHAPTER 4

47
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 4 TREES Tree is a connected undirected graph with no simple circuits Trees have been employed to solve problems in a wide variety of disciplines. SUMMARY CHAPTER 4

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google