530 likes | 543 Views
Learn about trees in computer science, efficient algorithms, modeling procedures, and properties of rooted trees. Explore Bernoulli family mathematicians and various tree terminology.
E N D
Discrete Mathematics Chapter-10 Trees
Introduction to Tree (§10.1) • Def 1. A connected (undirected) graph that contains no simple circuits is called a tree. • Trees are particularly useful in computer science, where they are employed in a wide range of algorithms. • Construct efficient algorithms for locating items in a list. • Construct efficient codes saving costs in data transmission and storage. • Study games such as checkers and chess. • Model procedure carried out using sequence of decisions.
The Bernoulli Family of Mathematicians Nikolaus (1623-1708) Johann (1667 - 1748 ) Jacob (1654 -1705) Nikolaus (1662 - 1716) Nikolaus (1687 - 1759) Nikolaus (1695 -1726) Daniel (1700 - 1782) Johann (1710 - 1790) Johann (1746-1807) Jacob (1759 - 1789)
EXAMPLE2Which of the graphs shown below are trees ? a a a a b b b b d c c c d d c d e e f e f f e f Tree Not a Tree Not a Tree Tree
Theorem 1 • An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. • Pf:T is a tree. T is connected, no simple circuit. For any vertices x and y, there is a simple path. If there is a different path from x to y, then two different paths will form a circuit. This is a contradiction. Thus the path is from x to y is unique. Assume that there is a unique path between any two vertices in graph T. T is connected. Suppose that T contains a simple circuit. Then every pair of vertices in this circuit, say x and y, have two different paths from x to y. That contradicts to the uniqueness of the path. Thus T has no circuit, and then T is a tree.
Def 1. 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. f g c a d b a b c e d e g e d f b a c A root tree with root a g f A Tree A root tree with root c
Terminology • Ancestors • Descendants • parent • Child • Siblings • Leaf • Internal vertex • Subtree
Terminology a is the parent of f, b and g j is the parent of l and m A Rooted Tree T a f is the child of a e is the child of c b b g subtree of b f f, b and g are siblings e and d are siblings j h c c subtree of g i m e e l d d k a, b are ancestors of c e, c are descendants of b leaves: d, e, f, k, i, l, m internal vertices: others
Def 3 • A rooted tree is called an m-ary tree if every interval vertex has no more than m children. • The tree is called a full m-ary tree if every interval vertex has exactly m children. • An m-ary tree with m = 2 is called a binary tree.
Example 3 A full 3-ary tree. A full binary tree.
A m-ary tree. (m3) Not a full m-ary tree A full 5-ary tree.
More Terminology • An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered (from left to right). • In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second one is called the right child. • The tree rooted at the left (right) child of a vertex is called the left (right) subtree of this vertex.
Example 4 a The Right Subtree of the Vertex c. b c The Left Subtree of the Vertex c. d i i h h e l l g f j j k k m m A Binary Tree T
Trees as Models H H H H H H C H H C C C H C H H H H C H C H H H H C H Isobutane H Butane The Two Isomers of Butane
Representing Organizations President VP Services VP R&D VP Marketing VP Finance Chief Field Operations Director Accounting AVP Sales Director Software Development Director Resourch AVP Marketing Director MIS Chief Field Operations Director Software Development
Theorem 2 • A tree with n verices has n1 edges. • Pf: Prove by induction. Let P(n) be the statement “A tree with n verices has n1 edges.” Basis Step: When n = 1, a tree with one vertex has no edge. P(1) is true. Inductive Step: Assume that P(k) is true. Suppose that T is a tree with k+1 vertices and v is a leaf. Let w is the parent of v. Remove v and the edge connecting v and w. We’ll get a tree T’ with k vertices and then has k1edges. It follows that T has k edges. Then P(k+1) is true.
Theorem 3 A full m-ary tree with i internal vertices contains mi+1 vertices. A full binary tree m = 2, i = 3, 7 vertices. A full 3-ary tree m = 3, i = 4, 13 vertices
A full 5-ary tree m = 5, i = 3, 16 vertices Not a full m-ary tree Not satisfy the theorem
Example 9 • A chain letter: Each people sends to 4 people. Some people do this, but others do not send any letters. How many people have seen this letter, including the first person, if no one receives more than one letter and if the chain letter ends after there have 100 people who read it but didn’t send it out? How many people sent out the letter? • Sol: This is a 4-ary (m = 4) tree. Note there are n = mi +1 vertices and l = n i leaves. 100(= l) leaves n = 4(n100) + 1 4001 = 3n There are n = 133 vertices. There are 133100 = 33 internal vertices.
Theorem 4 • A full m-ary tree with • n vertices has i = (n 1)/m internal vertices and l = [(m1)n + 1]/m leaves, • i interval vertices has n = mi + 1 vertices and l = (m1)i + 1 leaves , • l leaves has n = (ml1)/(m1) vertices and i = (l1)/(m1) internal vertices.
More Terminology • The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex v. • The level of the root is defined to be zero. • The height of a rooted tree is the maximum of the levels of vertices. • That is, the height is the length of the longest path from the root to any vertex. • A rooted m-ary tree of height h is balancedif all leaves are at levels h or h1.
Example a LevelVertices 0 a 1 b, j, k 2 c, e, f, l 3 d, g, i, m, n 4 h b k j f c l e g n d m i h The height of this rooted tree is 4.
Example Not balanced Balanced
Introduction • How should items in a list be stored so that an item can be easily located ? • What series of decisions should be made to find an object with a certain property in a collection of objects of a certain type ? • How should a set of characters be efficiently coded by bit strings ?
Binary Search Tree • 簡介:二元搜尋樹 是一種二元樹。它可能是空的,若不是空的,它具有下列特性: (1) 每一個元素有一鍵值,而且每一元素的鍵值都不相同,即每一個鍵值都是唯一的。 (2) 在非空的左子樹上的鍵值,必小於在該子樹的根節點中的鍵值。 (3) 在非空的右子樹上的鍵值,必大於在該子樹的根節點中的鍵值。 (4) 左子樹和右子樹也都是二元搜尋樹。
Example1 • Form a binary search tree for the words mathematic ,physics ,geography ,zoology ,meteorology ,geology ,psychology ,and chemistry (using alphabetical order)
Constructing a Binary Search Tree • 按照題目所給的字順序排,如題Mathematics就是root • 接下Physic>Mathematics,所以在M右下加一個child • Geography<Mathematics,所以再M左下加一個child • Zoology>Mathematics且>Physics,所以再P右下加一個child • Meteorology>Mathematics但<Physics,所以在P左下加一個child
Geology<Mathematics但>Geography,所以在Geography的右下加一個childGeology<Mathematics但>Geography,所以在Geography的右下加一個child • Psychology>Mathematics且>Physics但<Zoology所以在Z左下加一個child • Chemistry<Mathematics且<Geography,所以在Geography的左下加一個child
Huffman Codes • 簡介:Huffman 編制程式是試圖減少相當數量位元必需代表標誌串的一個統計技術。 • 修造Huffman 樹 (1)選擇二個parentless 結以最低的可能性。 (2)創造是二個最低的可能性結的父母的一個新結。 (3)分配新結可能性相等與它的兒童的可能性的總和。 (4)重覆步驟1 直到有只一個parentless 結剩下
Example 5 • Use Huffman coding to encode the fallowing symbols with the frequencies listed: A:0.08, B:0.10, C:0.12, D:0.15, E:0.20, F:0.35. What is the average number of bits used to encode a character ?
Example 5 • 於題目中找尋最小兩點→A:0.08和B:0.10(A於右下B於左下∵B>A) • 接著把A+B視為新的一點0.18,再找最小兩點→C:0.12和D:0.15如同AB方法接起來 • C+D也視為新的一點0.27再比大小,之後依此方法作完全部的點 • 而全圖於右下角的支線都為1,左下角都之線都為0 • 因此A by 111,B by 110,C by 011,D by 010, E by 10, F by 00. So the average number of bits used to encode a symbol using this encoding is 3×0.08+3×0.10+3×0.12+3×0.15+2×0.20+2×0.35=2.45
Universal address system We will describe one way to order totally the vertices of an ordered rooted tree. To produce this ordering, we must first label all the vertices. We do this recursively : 1. Label the root with the integer 0. Then label its k children (at label 1) from left to right with 1,2,...,k. 2. For each vertex v at level n with label A, label its k v children, as they are drawn from left to right, with A.1,A.2,…A.k v.
Example 1 we display the labelings of the universal address system next to the vertices in the ordered rooted tree. The lexicographic ordering of the labelings is0 < 1 < 1.1 < 1.2 < 1.3 < 2 < 3 < 3.1 < 3.1.1 < 3.1.2 < 3.1.2.1 < 3.1.2.2 < 3.1.2.3< 3.1.2.4 < 3.1.3 < 3.2 < 4 < 4.1 < 5 < 5.1 < 5.1.1 < 5.2 < 5.3 0 3 4 1 2 5 3.1 1.1 1.2 1.3 3.2 4.1 5.1 5.2 5.3 3.1.2 3.1.1 3.1.3 5.1.1 3.1.2.1 3.1.2.2 3.1.2.3 3.1.2.4
Definition 1 Let T be an order rooted tree with root r . If T consists only of r , then r is the preorder traversal of T . Otherwise , suppose that T1 , T2 , … , Tn are the subtree 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 .
Example 2 In which order does a preorder traversal visit the vertices inthe ordered rooted tree T shown below . a b d c g e f h i k j l m n o p
g b e f c d The preorder traversal of T is a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i . h i a k m j l n o p f a b e j k n o p c d g l m h i
Definition 2 Let T be an ordered rooted tree with root r . If T consists Only of r , then r is the inorder traversal of T . Otherwise , suppose that T1 , T2 , … , Tn are the subtree at r from left to right in T . Theinorder traversal begins by traversing T1 in inorder , then visiting r . It continues by traversing T2 in inorder , then T3 in inorder ,…, and finally Tn in inorder .
Example 3 In which order does a inorder traversal visit the vertices inthe ordered rooted tree T shown below . a b d c g e f h i k j l m n o p
b d c a g e f h i The preorder traversal of T is a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i . k j l m n o p g h f c m d i j e k b l a n o p
Definition 3 Let T be an ordered rooted tree with root r . If T Consists only of r , then r is the postrder traversal of T . Otherwise , suppose that T1 , T2 , … , Tn are the subtreesat r from left to right in T . The postorder traversal begins by traversing T1 in postorder , then T2 in inorder , then T3 in inorder ,…, and ends by visiting r .
Example 4In which order does a postorder traversal visit the vertices inthe ordered rooted tree T shown below . a b d c g e f h i k j l m n o p
b d c a g e f h i The preorder traversal of T is a , b , e , j , k , n , o , p , f , c , d , g , l , m , h , i . k j l m n o p g h f c m d i j e k b l a n o p
Example 5what is the ordered rooted tree that represents the expression((x+y)↑2)+((x-4)/3)? _ + + x x y 4 ↑ ↑ / / _ _ 2 2 + 3 + 3 x x x y x y 4 4
Example 6what is the prefix from for ((x+y)↑2)+((x-4)/3)? + ↑ / _ 2 + 3 x x y 4 Example 8what is the postfix from for ((x+y)↑2)+((x-4)/3)? This produces the prefix expression : +↑+ x y 2 / - x 4 3 This produces the postefix expression : x y + 2 ↑ x 4 - 3 / +