1 / 42

KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua

Discrete mathematics. The trees. N . V . Bilous. Faculty of computer sciences Software department, KNURE. KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua. The basic definitions. a. b. c. d. g. f.

shadi
Download Presentation

KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete mathematics. The trees. N.V. Bilous Faculty of computer sciences Software department, KNURE KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua

  2. The basic definitions a b c d g f A treeis a connected undirected graph with no simple circuits. Graphs containing no simple circuits that are not necessarily connected are called forests . G2 G3 G4 G1 Subgraphs are form the forest. The graph G1is a Tree , 7.Introduction to Trees

  3. The basic definitions a g b f h c i e d k A particular vertex of a tree is designated as the root. A tree together with its root produces a directed graph called a rooted tree. A Rooted Tree: a – a root 7.Introduction to Trees

  4. The basic definitions g h i k Let’s T is a rooted tree. If v is a vertex in T other than the root, the parent of v is the unique, vertex u such that there is a directed edge from u to v. When u is the parent of v, v is calledachild of u. Example The parentof h and iisg. The children of g are h, and i 7.Introduction to Trees

  5. The basic definitions a g b f h c i d k e Vertices with the same parent are calledsiblings. Theancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root. Example The sibling of h is i. The ancestors of e are c, b, and a. 7.Introduction to Trees

  6. The basic definitions a g b f h c i d k e The descendants of a vertex v are those vertices that have v as an ancestor. A vertex of a tree is called aleaf if it has no children. Vertices that have children (exclude a root) are calledinternal vertices. Example The descendants of b are c, d and e. The leaves are d, e, f, i and k. The internal vertices are a, b, c, g and h. 7.Introduction to Trees

  7. The basic definitions a g g b f h i h c i k d k e If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants. A Rooted Tree and its Subtree. 7.Introduction to Trees

  8. The basic definitions A spainning tree is a tree which contains all verticesof the initial graph. The chordofspainningtree Н in graph G is the edge of graph G, which are not belonging to spainningtree Н (this edge, is between vertices 3 and 4). Spainning tree Hof graph G. 7.Introduction to Trees

  9. Cayley’s Problem Let М is set ofcities. с(a, b)is cost of roadconstructionbetween cities a and b (a, b  M). What should be netof roads between the cities a and b (a, b  M)that on it was possible to pass from any city ainto any city band that cost of this net с(a, b)was minimal? 7.Introduction to Trees

  10. Cayley’s Problem It is possible to constructnn-2trees on n vertices. n=4, then42=16. Quantity of trees with 4 vertices . 7.Introduction to Trees

  11. Tree of the minimal weight (cost) Graph that have a number assigned to each edge is called a weighted graphs. A number assigned to each edge of graphis called a weight. The sum of edges weights for a graph is called a weight of graphfrom G . GraphG. 7.Introduction to Trees

  12. Algorithm for finding the minimal weight tree • Arrange edges use the order of their weight number. • Include in the spainningtree edges use the increasing order of weight number. • Add a next edge. If an added edge generate the circuit in the graph then delete it from the tree. • The tree is constructed when in it is included n-1 edge. 7.Introduction to Trees

  13. Algorithm for finding the minimal weight tree Example. It is given graph G. To construct for it a tree of the minimal weight. 7.Introduction to Trees

  14. Algorithm for finding the minimal weight tree Continuation of example. СЕ=4 АВ=6 АС= 7 SН=6+8+7+4=25. АD = 8 7.Introduction to Trees

  15. Algorithm of tree coding 1)Count the number of vertices n in the tree. The length of the tree code is equal to n-2. 2) Write all leaves of the tree. From of all leaves find the leaf with a minimal number. This leaf is deleted from the tree with the incidence edge. The number of an adjacence vertex write in the tree code. 3) This process repeats until will receive the code by length n-2. 7.Introduction to Trees

  16. Example of coding of trees 3 3 3 3 5 5 5 5 8 8 8 8 13 13 13 13 1 1 1 1 10 10 10 10 7 7 7 7 2 2 2 2 4 4 4 4 6 6 6 6 9 9 9 9 11 11 11 11 12 12 12 12 1 1 3 7 3 7 3 5 10 10 5 7.Introduction to Trees

  17. Algorithm of the tree decoding 1) Consider the tree code. If the code length is equal top, then the number of vertices in the tree is equal to p+2; 2)Find vertices (leaves number that do not enter into a code); 3) Find the leafwith minimal number and connect it with the first unuseable vertex in the code. If the connected vertex does not use in the next positions of the code, write this vertex in the sequence of leaves; 4)Repeatpoint 3),until use all leaves; 5)The last vertex in the code connect with the leaf with maximal number. 7.Introduction to Trees

  18. Algorithm of decoding of trees Example. The code: 1, 1, 1, 5. It is necessary to construct a tree. The decision. 1. Total of vertices equally 4+2=6. 2. Find leaves: 2, 3, 4, 6 1 1 1 1 1 1 5 5 5 1 2 4 6 3 7.Introduction to Trees

  19. The level and height of tree The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level of the root is defined to be zero. The height of a rooted tree is the maximum of the levels of vertices. 7.Introduction to Trees

  20. The level and height of tree Example. The root a is at level 0. b, j, o and k are at level 1. d, g, i, m, n, and s are at level 3. c, e, f, l and p are at level 2. h is at level 4. This tree has height 4. 7.Introduction to Trees

  21. m-ary tree, binary tree A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m = 2 is called abinary tree. A Binary Tree 7.Introduction to Trees

  22. A binary tree. In an ordered binary tree, if an internal vertex has two children, the first child is called the left child and the second child is called the right child. The tree rooted at the left child of a vertex is called theleft subtree of this vertex. The tree rooted at the right child of a vertex is called the right subtree of the vertex . 7.Introduction to Trees

  23. Properties of Trees Properties of Trees • An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. • A tree with n vertices has n –1edges. • A full m-ary tree with i internal vertices contains n = mi + 1 vertices 8.Properties of Trees

  24. Properties of Trees 4. A full m-ary tree (I) with n vertices has i = (n - 1)/m internal vertices and l = [(m - 1)n + 1]/m leaves. (II) with i internal vertices has n = mi + 1 vertices and l = (m - 1)i + 1 leaves. (III)with l leaves has n = (ml - 1)/(m - 1) vertices andi = (l-1)/(m-1) internal vertices. 8.Properties of Trees

  25. Infix, prefix, and postfix notation An ordered rooted tree can be used to represent such expressions, where the internal vertices represent operations, and the leaves represent the variables or numbers. There are infix, prefix, and postfix notation ordered rooted tree. 8.Properties of Trees

  26. Infix, prefix, and postfix notation Infix notation: any operation write between operands. Prefix notation: at first write operations, at second – operands. Postfix notation: at first write operands, at second – operations. 7.Introduction to Trees

  27. Infix, prefix, and postfix notation The Binary Tree for ((x+y)2)+((x– 4)/3) The prefix form: +  + xy 2 / – x 4 3 The infix form: x+y2+x–4/3x+y/2 The postfix form: x y + 2 x 4 – 3 / + 8.Properties of Trees

  28. Tree Traversal r Step 1: Visit r T1 T2 Tn Step 2: Visit T1 in preorder Step 3: Visit T2 in preorder Step n+1: Visit Tn in preorder • T1,T2,…Tnare 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. 8.Properties of Trees

  29. The preorder traversal 1 1 4 4 3 3 6 6 7 7 8 8 5 5 2 2 9 9 1 4 6 7 2 9 3 8 5 8.Properties of Trees

  30. Algorithm of Preorder Traversal AlgorithmPreorder Traversal. procedurepreorder (T ordered rooted tree) r = root of T list r for each child c of r from left to right begin T(c) = subtree with c as its root preorder(T(c)) end 8.Properties of Trees

  31. Prefix notation Obtain the prefix form of an expression when traverse its rooted tree in preorder. Example. By traversing it in preorder receive the prefix notation for this expression: +  + xy 2 / – x 4 3 8.Properties of Trees

  32. Tree Traversal r Step 2: Visit r T1 T2 Tn Step 1: Visit T1 in inorder Step 3: Visit T2 in inorder Step n+1: Visit Tn in inorder • T1,T2,…Tn are the subtrees at r from left to right. The inorder 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. 8.Properties of Trees

  33. The inorder traversal 1 1 4 4 3 3 6 6 7 7 8 8 5 5 2 2 9 9 6 4 2 7 9 1 8 3 5 8.Properties of Trees

  34. Algorithm of Inorder Traversal AlgorithmInorder Traversal procedureinorder(T: ordered rooted tree) r: = root of T if r is a leaf then list r else begin l: = first child of r from left to right T(l) : = subtree with l as its root Inorder (T (l)) list for each child c of r except for l from left to right T (c) : = subtree with c as its root Inorder (T (c)) end 8.Properties of Trees

  35. Infix notation Obtain the infix formof an expression when traverse its rooted tree in inorder. Example. By traversing it in inorder receive the infix notation for this expression: x+y2+x–4/3x+y/2 8.Properties of Trees

  36. Tree Traversal r Step n+1: Visit r T1 T2 Tn Step 1: Visit T1 in postorder Step 2: Visit T2 in postorder Step n: Visit Tn in postorder • T1,T2,…Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T1in postorder, then T2 in postorder,…, then Tn in postorder, and ends by visiting r. 8.Properties of Trees

  37. The postorder traversal 1 1 3 3 4 4 7 7 8 8 5 5 6 6 2 2 9 9 6 2 9 7 4 8 5 3 1 8.Properties of Trees

  38. Algorithm of Postorder Traversal AlgorithmPostorder Traversal. procedurepostorder (T: ordered rooted tree) r : = root of T for each child c of r from left to right begin T (c) = subtree with c as its root postorder(T(c)) end list r 8.Properties of Trees

  39. Postfix notation Obtain the postfix formof an expression when traverse its rooted tree in postorder. Example. By traversing it in postorder receive the postfix notation for this expression: x y + 2 x 4 – 3 / + 8.Properties of Trees

  40. Infix, prefix, and postfix notation Obtain the prefix form of an expression when traverse its rooted tree in preorder. Expressions written in prefix form are said to be in Polishnotation (which is named after the logician Jan Lukasiewicz) . Obtain the infix formof an expression when traverse its rooted tree in inorder. Obtain thepostfix form of an expression by traversing its binary tree in postorder.Expressions written in postfix form are said to be inreverse Polish notation. 8.Properties of Trees

  41. A similar binary trees Two binary trees are called asimilar if they have identical structure, e. m. they are either empty or contain identical number subtreesand their left and right subtreesare similar. 8.Properties of Trees

  42. Equivalent binary trees Binary trees are equivalent, if they are similar and corresponding units contain the same information. 8.Properties of Trees

More Related