Download presentation

Presentation is loading. Please wait.

1
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees

2
Discrete Mathematics Ch 4 Trees Transparency No. 8-2 contents Introduction definitions properties of trees Applications binary search trees Decision trees Prefix codes Tree traversal universal address systems inorder, preorder and postorder traversal infix, prefix and postfix notation – linearize(serialize) trees. trees and sorting -- bubble sort, merge-sort spanning trees minimum spanning trees -- Prim's alg; Kruskal's alg.

3
Discrete Mathematics Ch 4 Trees Transparency No. 8-3 8.1 Introduction A connected graph w/o simple circuits useful in CS and many fields Def1: A tree is a connected undirected graph w/o simple circuits (cf: Forest) Ex1: Family Tree: Ex2: x y ==> X is the parent of Y tree has cyclenot connected

4
Discrete Mathematics Ch 4 Trees Transparency No. 8-4 Theorem 1: Theorm1:An undirected graph is a tree iff there is a unique simple path b/t any two of its vertices. Pf:”=>”: T: any tree; x,y: two distinct vertices. Since T is connected, by def., there is a path and hence a simple path from x to y. Assume there is another simple path from x to y. Then by a theorem of ch 7 about simple circuit, there is a simple circuit in T.=> T is not a tree, a contradiction! “ T is connected. Assume T contains a simple cycle containing x and y. (i.e., x -- -- y -- -- x ==> T contains two simple paths from x to y, a contradiction.

5
Discrete Mathematics Ch 4 Trees Transparency No. 8-5 Rooted Tree A rooted tree is a tree T=(V,E) associated with a distinguished vertex r ∈ V called the root of T. Given a rooted tree with root r, we can form a directed graph with the property that (u,v) ∈ V 2 is an edge of the new directed graph iff u appears immediately before v in the unique simple path from the root r to v.

6
Discrete Mathematics Ch 4 Trees Transparency No. 8-6 A tree and Rooted Tree formed by designating two roots a b c d e f g T a b c d e f g With root c a b c d e f g With root a

7
Discrete Mathematics Ch 4 Trees Transparency No. 8-7 Some terminology of rooted tree T= : a rooted tree. The associated digraph of T is a digraph G T = (V,E’) where E’ = {(u,v) ∈ V 2 |{u,v} ∈ E and ∃ a path r---….---u---v in T} if (u,v) ∈ E’ => u is called the parent of v (and v a child of u) Vertices with the same parent are called siblings. All vertices(except v) in the unique path from the root r to a vertex v are ancestors of v. v is a descendant of u iff u is an ancestor of v. Vertices without children are called leaves Vertices having children are called internal nodes.

8
Discrete Mathematics Ch 4 Trees Transparency No. 8-8 subtrees T=(V,E,r): a rooted tree; a: a vertex. The subtree of T with a as root is the tree T’=(V’,E’,a) where V’ = {{a} U {v | v is a descendant of a} E’ = E| V’ (E restricted to V’) = { {u,v} ∈ E | {u,v} ⊆ V’ } Example: root internal nodes leaves parent, child sibling ancestor, descendant a b c d e f g

9
Discrete Mathematics Ch 4 Trees Transparency No. 8-9 m-ary tree Def: A rooted tree is an m-ary tree (m > 0) if every internal vertex has no more than m children. A tree is a full m-ary tree if every internal vertex has exactly m children. A 2-ary tree is also called a binary tree. Ordered rooted tree: the children of each vertex are ordered. The first (second) child of a vertex in an ordered binary tree is called its left (right) child. The subtree rooted at the left [right] child of the root of an ordered binary tree is called its left [right] subtree.

10
Discrete Mathematics Ch 4 Trees Transparency No. 8-10 Example 4: left and right child of d = ? left and right subtree of c = ? Notes: 1. Like graphs, there is no standard terminology used to describe trees, rooted trees, ordered rooted trees and binary trees. 2. Readers should carefully check meanings given to terms dealing with trees whenever they occur. a b c d e f g h i j k l m

11
Discrete Mathematics Ch 4 Trees Transparency No. 8-11 Trees as models

12
Discrete Mathematics Ch 4 Trees Transparency No. 8-12 Properties of trees Theorem 2: A tree with n ( n >0) vertices has exactly n-1 edges. Pf: Let T = (V,E) be a tree and choose a vertex r as the root. Now define the mapping g: E V- {r} by g(e) = v iff e={u,v} and v is the child of u. Obviously g is 1-1 and onto. Hence |E| = |V|-1. 1-1: if e 1 = {u 1,v}, e 2 = {u 2,v} with g(e 1 ) = g(e 2 )=v => both u 1 and u 2 are the vertex appearing immediately before v in the unique simple path from r to v => u 1 = u 2. onto: v ∈ V – {r}, there is a unique simple path of length > 0 from r to v => there is a vertex u occurring immediately before v => g({u,v}) = v.

13
Discrete Mathematics Ch 4 Trees Transparency No. 8-13 Number of vertices in a complete m-ary tree Theorem 3: A full m-ary tree with i internal vertices contains n = mi + 1 vertices. Pf: Note: every edge in a tree T=(V,E,r) contributes one parent-degree and one child- degree. => the number of parent-degrees= v ∈ V #children(v) [ = |E| = number of child-degrees = v ∈ V #parent(v) = |V|-1 : another proof of theorem 2 ] = v ∈ V-all leaves #children(v) --- leaves have no parent- degree. = v ∈ V-all leaves m = mi ----- every internal vertex has m children. => |V| = mi + 1.

14
Discrete Mathematics Ch 4 Trees Transparency No. 8-14 Theorem 4 T: a full m-ary tree with n vertices, i internal vertices and l leaves. Then 1. i = (n-1)/m and l = [(m-1)n+1]/m 2. n = mi +1 and l = (m-1) + i + 1 3. n = (ml-1) / (m-1) and i = (l-1)/(m-1). pf: All are simple results of the following equations: (1) n = mi + 1 (*, Theorem 3) and (2) n = l + i (every vertex is either a leaf or an internal vertex) 1: i = (n-1) /m and l = n - i =n – (n-1)/m =[(m-1)n+1]/m 2,3: Left as exercises.

15
Discrete Mathematics Ch 4 Trees Transparency No. 8-15 Example 9 Someone starts a chain letter. Each person receiving the letter either sends to 4 other people or stops sending letter. ==> 1.#people seeing the letter = ? if no one receiving the letter more than once and the chain letter ends after 100 people reading it but did not send it out. 2. #people sending out the letter = ? Sol: T=(V,E,r) where V = all people receiving (and seeing) the letter r = the person starting the game E = {(u,v) | u sends a letter to v } #leaves = l = 100 => n = l + #internal vertices(i) and n = 4i + 1 n = l + (n-1) /4 => l = n – (n-1)/4 = (3n+1)/4 =100 n = 400 –1 /3 = 133. => i = n –l = 33.

16
Discrete Mathematics Ch 4 Trees Transparency No. 8-16 the Level of nodes and the height of a tree T=(V,E,r) : a tree, a: a vertex. Level(a) = depth(a) = the length of the unique path from root to a. Level(T) = height(T)= max v ∈ V level(v) Example 10: level(v) = ? for each vertex v in the tree? Height(T) = ? A m-ary tree is a complete tree if it is full and all leaves have the same height a b c d e f g h i j k l m

17
Discrete Mathematics Ch 4 Trees Transparency No. 8-17 Balanced trees A tree is balanced if the subtrees at each vertex have approximately the same height. Def: A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h-1. Example 11: Which of the rooted trees are balanced ? T1 T2

18
Discrete Mathematics Ch 4 Trees Transparency No. 8-18 T3

19
Discrete Mathematics Ch 4 Trees Transparency No. 8-19 Maximum number of leaves in a tree. Theorem 5: There are at most m h leaves in an m-ary tree T of height h. Pf: By math ind on h. Basis: h = 0 => T has only 1 = m 0 node, which is a leaf. Ind. case: Assume every m-ary tree of height h has leaves ≤ m h => m-ary tree T of height h+1 has at most m subtrees, each of height h => #leaves(T) = t’ is a subtree of T #leaves(T’) ≤ m x m h = m h+1. Theorem 5’ : Every complete m-ary tree of height h has m h leaves. Pf: Like Theorem 5.

20
Discrete Mathematics Ch 4 Trees Transparency No. 8-20 Corollary 1 Corollary 1. 1. If an m-ary tree of height h has l leaves, then h ≥ [log m l]. 2. If the m-ary tree is full and balanced, then h =[log m l], where [-] is the ceiling function. Pf:1. By Theorem 5, l ≤ m h ; hence h ≥ log m l. Since h is an integer, h ≥ [log m l]. 2. ∵ balanced => h-1 ≤ level of leaves ≤ h ∵ height = h => Some vertex is at level h. => m h-1 < l ≤ m h. => h< 1+ log m l and h ≥ log m l => log m l ≤ h < 1 + log m l => h = [log m l].

Similar presentations

© 2022 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