Trees • Tree – is a connected undirected graph with no simple circuits. • Forest – is a graph with each its connected component being a tree. • Theorem( unique simple path ) A undirected graph is a tree there is a unique simple path between any two of its vertices. • 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. Terminologies: parent-child, leaf-internal node, siblings, descendants-ancestors 2010/9/23 DISC MATH, NCHU 1 M-ary Tree (1/4) • A rooted tree is called a m-ary tree if every internal vertex has no more than m children. • Applications:檔案系統架構、平行電腦架構 • Property: 1. A tree with N vertices has N-1 edges. 2. A full m-ary tree with i internal vertices contains n = m * i +1 vertices. Conversely, n vertices has i = ( n -1 ) / m internal vertices. and L = [ ( m – 1 ) * n + 1 ] / m leaves. Here “full” means each internal vertex has m children 2010/9/23 DISC MATH, NCHU 2 M-ary Tree (2/4) • Theorem : A full m-ary tree with <1> n vertices has i = ( n – 1 ) / m internal vertices and L = [ ( m – 1 ) * n + 1 ] / m leaves. <2> i internal nodes has n = m * i + 1 vertices and L = ( m – 1 ) * i + 1 leaves. <3> L leaves has n = ( m * L – 1 ) / ( m – 1 ) vertices and i = ( L – 1 ) / ( m – 1 ) internal vertices. Note: n = m*i + 1 & L = n-I =(m – 1)*i + 1 2010/9/23 DISC MATH, NCHU 3 M-ary Tree (3/4) E.g. Suppose someone starts a chain letter. Each person receives the letter is asked to send it on to 4 people. Some do it, some doesn’t send the letter. How many people have seen the letter, if no one has received more than one letter and the chain letter ends after having 100 people who read it but did not send it out ? Answer: 4-ary tree L = 100, internal nodes = (L – 1) / ( 4 – 1 )=33 n = 4 * 33 + 1 = 133 2010/9/23 DISC MATH, NCHU 4 M-ary Tree (4/4) The level of a vertex in a rooted tree = the length of the unique path from the root to the vertex. Level of root = 0. A rooted tree is of height h if the max of levels of vertices = h. A rooted m-ary tree of height h is balanced if all leaves are at levels h or ( h – 1 ), h is the height of the tree. 2010/9/23 DISC MATH, NCHU 5 Application of Trees (1/2) Binary Search Tree 左小右大,alphabetical ordering E.g. Math, Geography, Meteorology, Chemistry, Physics, Zoology, Geology, Psychology, Oceanography Prefix Codes bit string of a letter never occurs as the 1st part of another letter. e:0 a : 10 t : 110 n : 1110 s : 1111 2010/9/23 DISC MATH, NCHU 6 Application of Trees (2/2) Huffman Codes : Prefix output code so that frequently used letter has fewer bit strings. 設A,B,C,D,E,F當coding symbols,其出現頻率依序 為0.08, 0.10, 0.12, 0.15, 0.20, 0.35 2010/9/23 DISC MATH, NCHU 7 Tree Traversal Universal address system of the ordered rooted tree. (Revision Control System, OIDs ) root is labeled by 0, its children is labeled from left to right with 1,2,3,… For each vertex v at level n with label A, label its children from left to right with A.1, A.2, A.3, ... Traversal Algorithms: Preorder, Inorder, Postorder 中左右 左中右 左右中 2010/9/23 DISC MATH, NCHU 8 Traversal Algorithms PreorderabejknopfcdglmhI Postorderjnopkefbclmghida Let T be an ordered rooted tree. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that T1, T2, …, Tn are the subtrees at r from left to right in T. The preorder traversal begins by visiting r. It continues by traversing T1 in preorder, then T2 in preorder and so on until Tn is traversed in preorder. 2010/9/23 DISC MATH, NCHU 9 Inorder Traversal Inorder jenkopbfaclgmdhi Let T be an ordered rooted tree. If T consists only of r, then r is the inorder traversal of T. Otherwise, suppose that T1, T2, …, Tn are the subtrees at r from left to right in T. The inorder traversal begins by traversing T1 in inorder, then visiting r. It continues traversing T2 in inorder and so on until Tn is traversed in inorder. 2010/9/23 DISC MATH, NCHU 10 Infix, Prefix and Postfix (1/2) ((x+y)^2)+((x-4)/3) Inorder traversal of this binary tree infix form Infix form requires fully parentheses. Prefix traversal called Polish Notation Binary tree representation using prefix form +^+xy2/-x43 (1) 從右到左 (2) 每遇到operator時執行運算 2010/9/23 DISC MATH, NCHU 11 Infix, Prefix and Postfix (2/2) Preorder- + ^ + x y 2 / – x 4 3 從右到左,取operator右邊的operands. Postorder- x y + 2 ^ x 4 – 3 / + 從左到右,取operator左邊的operands. Inorder traversals of all have the same outputs as : x + y / x + 3 2010/9/23 DISC MATH, NCHU 12 Spanning Tree Let G be a simple graph. A Spanning tree of G is a subgraph of G that is a tree containing every vertex in G. Theorem A simple graph is connected if has a spanning tree. 2010/9/23 DISC MATH, NCHU 13 Depth-First-Search (DFS) & Breadth-First-Search (BFS) 產生Spanning tree的方法: 1. Removing edges from simple circuits. 2. Adding edges from ( an arbitrary vertex ) either Depth-First or Breadth-First. 2010/9/23 DISC MATH, NCHU 14 BFS example 2010/9/23 DISC MATH, NCHU 15 Minimum Spanning Tree Prim’s algorithm:先選一minimum edge, 再加入鄰近cost較小之 edge (不造成circle) Kruskal’s algorithm:逐一加入 minimum edge, 只要不造成 circle即可 2010/9/23 DISC MATH, NCHU Prim’s Kruskal’s b,f c,d a,b k,l f,i b,f a,e c,g i,j a,b f,g f,j c,g b,c c,d j,k g,h g,h h,l i,j k,l a,e 16 Kruskal’s Algorithm Procedure Kruskal (G: weighted connected undirected graph with n vertices) T := an empty graph for i:= 1 to n-1 begin e = an edge in G with smallest weight that does not form a simple circuit in T if added to T. T := T with e added end 2010/9/23 DISC MATH, NCHU 17 Prim’s Algorithm Procedure Prim (G: weighted connected undirected graph with n vertices) T := a min-weight edge for i:= 1 to n-2 begin e = an edge, not in T, of min weight and not forming a simple circuit in T if added to T. T := T with e added end 2010/9/23 DISC MATH, NCHU 18 MEMO Read chapter 10 in details. Get familiar with the relationship between the number of edges and vertices in m-ary tree, tree traversals, prefix codes and Huffman codes, and spanning tree. HW #17,19-21 of §10.1, #1,13,14,19,21,23,24 of §10.2, #17,18,22,23 of §10.3, # 16,17 of §10.4, #3,4,7,8 of §10.5 2010/9/23 DISC MATH, NCHU 19
© Copyright 2026 Paperzz