SlideShare uma empresa Scribd logo
1 de 15
Baixar para ler offline
Trees - 10                                                              CSC1001 Discrete Mathematics            1

 CHAPTER
                                                             ต้นไม้
   10                                                       (Trees)

   1         Introduction to Trees
1. Deffinition of Trees
  Definition 1

 A tree is a connected undirected graph with no simple circuits. Because a tree cannot have a simple circuit,
 a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph.

Example 1 (5 points) Is it tree or not tree?
 1.                                     2.                                   3.
       a           d        f                  a       d         f                a          d           f

       b                    g                  b                 g                b                      g

       c           e        h                  c       e         h                c           e          h

 4.
       a           d        f           5.
                                               a       d         f           6.
                                                                                  a          d           f

       b                    g                  b                 g                b                      g

       c           e        h                  c       e         h                c           e          h

  Definition 2

 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.

The Terminology of Trees
Suppose that T is a rooted tree and 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 called a child of u.
   Vertices with the same parent are called siblings.
   The ancestors 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.
   The descendants of a vertex v are those vertices that have v as an ancestor.
มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                               เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
2         CSC1001 Discrete Mathematics                                                                     10 - Trees


      A vertex of a rooted tree is called a leaf if it has no children.
      Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only
      vertex in the graph, in which case it is a leaf.
      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.
Example 2 (5 points) In the rooted tree T (with root a) shown in Figure, can you find;
1) The parent of c ……………………………………………………………………………………………………………                        a
2) The children of g ……………………………………………………………………………………………………….....
3) The siblings of h …………………………………………………………………………………………………………..
4) All ancestors of e ………………………………………………………………………………………………...……….     b                  g
                                                                                    f
5) All descendants of b ……………………………………………………………………………………………………..      c         h            j
6) All internal vertices ……………………………………………………………………………………………………….                      i
7) All leaves …………………………………………………………………………………………………………………..
                                                                      d      e      k      l   m
8) What is the subtree rooted at b, g and j




    Definition 3

    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 a binary
    tree.

Example 3 (4 points) Are the rooted trees in Figure full m-ary trees for some positive integer m?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                   เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                                 CSC1001 Discrete Mathematics               3
  Definition 4

 A tree with n vertices has n–1 edges.

  Definition 5

 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 vertices has n = mi + 1 vertices and l = (m–1)i + 1 leaves,
     (3) l leaves has n = (ml–1)/(m–1) vertices and i = (l–1)/(m–1) internal vertices.

Example 4 (4 points) From an Example 3, find the number of edges of trees T1, T2, T3 and T4?




Example 5 (2 points) Find the number of internal vertices and leaves if full 3-ary tree has 15 vertices?




Example 6 (2 points) Find the number of vertices and leaves if full 4-ary tree has 39 internal vertices?




Example 7 (2 points) Find the number of vertices and internal vertices if full 4-ary tree has 50 leaves?




Example 8 (4 points) 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

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                     เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
4       CSC1001 Discrete Mathematics                                                                10 - Trees


letter ends after there have been 100 people who read it but did not send it out? How many people sent out
the letter?




Example 9 (4 points) A chain letter starts when a person sends a letter to five others. Each person who
receives the letter either sends it to five other people who have never received it or does not send it to
anyone. Suppose that 10,000 people send out the letter before the chain ends and that no one receives more
than one letter. How many people receive the letter, and how many do not send it out?




Example 10 (4 points) Suppose 1000 people enter a chess tournament. Use a rooted tree model of the
tournament to determine how many games must be played to determine a champion, if a player is eliminated
after one loss and games are played until only one entrant has not lost. (Assume there are no ties.)?




Example 11 (4 points) Draw the Fibonacci trees of f(5), how many the number of internal vertices and leaves
of Fibonacci tree?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                            เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                                CSC1001 Discrete Mathematics            5
   2         Applications of Trees
1. Binary Search Trees
  Definition 1

 A binary search tree is 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, and each vertex is labeled with a key, which is one of
 the items. 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.

Example 12 (5 points) Draw a binary search tree of set {19, 8, 27, 23, 11, 4, 6, 2, 37, 30, 20, 22, 10, 3}.




Example 13 (5 points) Draw a binary search tree of set {50, 25, 73, 35, 63, 84, 10, 15, 31, 22, 44, 4, 1, 72}.




Example 14 (4 points) Show the path of binary search tree in an Example 13 if find 10, 31, 35, 19.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                 เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
6         CSC1001 Discrete Mathematics                                                                       10 - Trees


2. Decision Trees
    Definition 2

    A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for
    each possible outcome of the decision, is called a decision tree.

Example 15 (5 points) Draw a decision tree to find the possible solutions when the coin are rolled at 3 times.




Example 16 (5 points) Draw a decision tree of sorted a, b and c variables.




3. Trees Traversal
   We will describe three of the most commonly used such algorithms, preorder traversal, inorder traversal,
and postorder traversal.
    Definition 3

    Let T be an ordered 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 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.


มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                     เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                                  CSC1001 Discrete Mathematics               7
  Definition 4

 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 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.

  Definition 5

 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T.
 Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The postorder traversal
 begins by traversing T1 in postorder, then T2 in postorder, . . . , then Tn in postorder, and ends by visiting r.

Example 17 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the
ordered rooted tree T shown in Figure?




Example 18 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the
ordered rooted tree shown in Figure?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                    เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
8         CSC1001 Discrete Mathematics                                                                     10 - Trees


Example 19 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the
ordered rooted tree shown in Figure?




4. Infix, Prefix, and Postfix Notation
    Definition 6

    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. Each operation operates on its left and
    right subtrees (in that order). There are 3 forms to represent the expressions, (1) Infix form A+B, (2) Prefix
    form +AB, and (3) Postfix form AB+

Example 20 (4 points) Show the prefix and postfix form of the expression a + b * c – d / e?




Example 21 (8 points) Draw a tree which represented the expression a + b * c – d / e and show prefix and
postfix form by using preorder and postorder traversal?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                   เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                               CSC1001 Discrete Mathematics            9
Example 22 (8 points) Draw a tree which represented the expression (a / b – c * d) / (e * f + g – h) and show
prefix and postfix form by using preorder and postorder traversal?




Example 23 (8 points) Draw a tree which represented the expression a / b – c * (d + e) * (f / g + h) – i * j and
show prefix and postfix form by using preorder and postorder traversal?




   3         Spanning Trees
1. Introduction to Spanning Trees
  Definition 1

 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 with a spanning tree must be connected, because there is a path in the spanning tree
 between any two vertices.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
10      CSC1001 Discrete Mathematics                                                                 10 - Trees


Example 24 (15 points) Find a spanning tree for the graph shown by removing edges in simple circuits.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                             เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                            CSC1001 Discrete Mathematics        11
2. Depth-First Search and Breadth-First Search
    We can also produce a spanning tree of a simple graph by the use of depth-first search and breadth-
first search algorithm.
1) Example of depth-first search of graph G (start with the vertex f)




2) Example of breadth-first search of graph G (start with the vertex e)




Example 25 (15 points) Use depth-first search and breadth-first search to produce a spanning tree for the
given simple graph. Choose a as the root of this spanning tree and assume that the vertices are ordered
alphabetically.

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                            เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
12      CSC1001 Discrete Mathematics                                                               10 - Trees




3. Minimum Spanning Trees
 Definition 2
 A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible
 sum of weights of its edges.

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                           เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                             CSC1001 Discrete Mathematics        13
  Definition 3

 The Prim’s Algorithm

 procedure Prim(G: weighted connected undirected graph with n vertices) {
   T = a minimum-weight edge
   for i = 1 to n − 2 {
     e = an edge of minimum weight incident to a vertex in T and
         not forming a simple circuit in T if added to T
     T = T with e added
   }
   return T //a minimum spanning tree of G
 }




  Definition 4
 The Kruskal’s Algorithm

 procedure Kruskal(G: weighted connected undirected graph with n vertices) {
   T = empty graph
   for i = 1 to n − 1 {
     e = any edge in G with smallest weight that does not form a simple
         circuit when added to T
     T = T with e added
   }
   return T //a minimum spanning tree of G
 }




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)             เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
14      CSC1001 Discrete Mathematics                                                               10 - Trees


Example 26 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in the
weighted graph shown in Figure.
1) Use Prim’s algorithm




2) Use Kruskal’s algorithm




Example 27 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in the
weighted graph shown in Figure.
1) Use Prim’s algorithm




2) Use Kruskal’s algorithm




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                           เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Trees - 10                                                           CSC1001 Discrete Mathematics         15
Example 28 (5 points) The roads represented by this graph are all unpaved. The lengths of the roads
between pairs of towns are represented by edge weights. Which roads should be paved so that there is a path
of paved roads between each pair of towns so that a minimum road length is paved?
1) Use Prim’s algorithm




2) Use Kruskal’s algorithm




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                            เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 
b+ tree
b+ treeb+ tree
b+ tree
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graph
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
Red black tree
Red black treeRed black tree
Red black tree
 
Trees
Trees Trees
Trees
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Selection sort
Selection sortSelection sort
Selection sort
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 

Destaque

Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)IIUM
 
Tree in Discrete structure
Tree in Discrete structureTree in Discrete structure
Tree in Discrete structureNoman Rajput
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Fahrul Usman
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
Relations digraphs
Relations  digraphsRelations  digraphs
Relations digraphsIIUM
 
chapter 2 memory and process management
chapter 2 memory and process managementchapter 2 memory and process management
chapter 2 memory and process managementAisyah Rafiuddin
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 

Destaque (17)

Tree (Data Structure & Discrete Mathematics)
Tree (Data Structure & Discrete Mathematics)Tree (Data Structure & Discrete Mathematics)
Tree (Data Structure & Discrete Mathematics)
 
Trees (slides)
Trees (slides)Trees (slides)
Trees (slides)
 
Tree in Discrete structure
Tree in Discrete structureTree in Discrete structure
Tree in Discrete structure
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
Avltrees
AvltreesAvltrees
Avltrees
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
Relations digraphs
Relations  digraphsRelations  digraphs
Relations digraphs
 
chapter 2 memory and process management
chapter 2 memory and process managementchapter 2 memory and process management
chapter 2 memory and process management
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
The Political History Of Bangladesh
The Political History Of BangladeshThe Political History Of Bangladesh
The Political History Of Bangladesh
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Semelhante a Discrete-Chapter 10 Trees

Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5DrkhanchanaR
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
Basic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptxBasic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptx22001003058
 
Pattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_treesPattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_treesNathan Gabriel
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Himanshu Choudhary
 
B.TECH Math project
B.TECH Math projectB.TECH Math project
B.TECH Math projectarunsarkar9
 
Read Section 11.4, pages 540 ( 547. Type in the answers below .docx
Read Section 11.4, pages 540 ( 547. Type in the answers below .docxRead Section 11.4, pages 540 ( 547. Type in the answers below .docx
Read Section 11.4, pages 540 ( 547. Type in the answers below .docxmakdul
 
Quant 125 awesome questions
Quant 125 awesome questionsQuant 125 awesome questions
Quant 125 awesome questionsRushabh Vora
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data StructureOm Prakash
 
counting principle.ppt
counting principle.pptcounting principle.ppt
counting principle.pptRizaCatli2
 

Semelhante a Discrete-Chapter 10 Trees (20)

trees-and-forest.pdf
trees-and-forest.pdftrees-and-forest.pdf
trees-and-forest.pdf
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
 
Euler and hamilton paths
Euler and hamilton pathsEuler and hamilton paths
Euler and hamilton paths
 
trees.ppt
trees.ppttrees.ppt
trees.ppt
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Trees ayaz
Trees ayazTrees ayaz
Trees ayaz
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Basic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptxBasic Terminologies of Tree and Tree Traversal methods.pptx
Basic Terminologies of Tree and Tree Traversal methods.pptx
 
Pattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_treesPattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_trees
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
7 Trees.pptx
7 Trees.pptx7 Trees.pptx
7 Trees.pptx
 
B.TECH Math project
B.TECH Math projectB.TECH Math project
B.TECH Math project
 
Read Section 11.4, pages 540 ( 547. Type in the answers below .docx
Read Section 11.4, pages 540 ( 547. Type in the answers below .docxRead Section 11.4, pages 540 ( 547. Type in the answers below .docx
Read Section 11.4, pages 540 ( 547. Type in the answers below .docx
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Quant 125 awesome questions
Quant 125 awesome questionsQuant 125 awesome questions
Quant 125 awesome questions
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
DSA IV Unit.pptx
DSA IV Unit.pptxDSA IV Unit.pptx
DSA IV Unit.pptx
 
counting principle.ppt
counting principle.pptcounting principle.ppt
counting principle.ppt
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 
7.tree
7.tree7.tree
7.tree
 

Mais de Wongyos Keardsri

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsWongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 

Mais de Wongyos Keardsri (20)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming Language
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and Objects
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 

Discrete-Chapter 10 Trees

  • 1. Trees - 10 CSC1001 Discrete Mathematics 1 CHAPTER ต้นไม้ 10 (Trees) 1 Introduction to Trees 1. Deffinition of Trees Definition 1 A tree is a connected undirected graph with no simple circuits. Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph. Example 1 (5 points) Is it tree or not tree? 1. 2. 3. a d f a d f a d f b g b g b g c e h c e h c e h 4. a d f 5. a d f 6. a d f b g b g b g c e h c e h c e h Definition 2 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. The Terminology of Trees Suppose that T is a rooted tree and 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 called a child of u. Vertices with the same parent are called siblings. The ancestors 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. The descendants of a vertex v are those vertices that have v as an ancestor. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 2. 2 CSC1001 Discrete Mathematics 10 - Trees A vertex of a rooted tree is called a leaf if it has no children. Vertices that have children are called internal vertices. The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf. 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. Example 2 (5 points) In the rooted tree T (with root a) shown in Figure, can you find; 1) The parent of c …………………………………………………………………………………………………………… a 2) The children of g ………………………………………………………………………………………………………..... 3) The siblings of h ………………………………………………………………………………………………………….. 4) All ancestors of e ………………………………………………………………………………………………...………. b g f 5) All descendants of b …………………………………………………………………………………………………….. c h j 6) All internal vertices ………………………………………………………………………………………………………. i 7) All leaves ………………………………………………………………………………………………………………….. d e k l m 8) What is the subtree rooted at b, g and j Definition 3 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 a binary tree. Example 3 (4 points) Are the rooted trees in Figure full m-ary trees for some positive integer m? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 3. Trees - 10 CSC1001 Discrete Mathematics 3 Definition 4 A tree with n vertices has n–1 edges. Definition 5 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 vertices has n = mi + 1 vertices and l = (m–1)i + 1 leaves, (3) l leaves has n = (ml–1)/(m–1) vertices and i = (l–1)/(m–1) internal vertices. Example 4 (4 points) From an Example 3, find the number of edges of trees T1, T2, T3 and T4? Example 5 (2 points) Find the number of internal vertices and leaves if full 3-ary tree has 15 vertices? Example 6 (2 points) Find the number of vertices and leaves if full 4-ary tree has 39 internal vertices? Example 7 (2 points) Find the number of vertices and internal vertices if full 4-ary tree has 50 leaves? Example 8 (4 points) 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 มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 4. 4 CSC1001 Discrete Mathematics 10 - Trees letter ends after there have been 100 people who read it but did not send it out? How many people sent out the letter? Example 9 (4 points) A chain letter starts when a person sends a letter to five others. Each person who receives the letter either sends it to five other people who have never received it or does not send it to anyone. Suppose that 10,000 people send out the letter before the chain ends and that no one receives more than one letter. How many people receive the letter, and how many do not send it out? Example 10 (4 points) Suppose 1000 people enter a chess tournament. Use a rooted tree model of the tournament to determine how many games must be played to determine a champion, if a player is eliminated after one loss and games are played until only one entrant has not lost. (Assume there are no ties.)? Example 11 (4 points) Draw the Fibonacci trees of f(5), how many the number of internal vertices and leaves of Fibonacci tree? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 5. Trees - 10 CSC1001 Discrete Mathematics 5 2 Applications of Trees 1. Binary Search Trees Definition 1 A binary search tree is 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, and each vertex is labeled with a key, which is one of the items. 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. Example 12 (5 points) Draw a binary search tree of set {19, 8, 27, 23, 11, 4, 6, 2, 37, 30, 20, 22, 10, 3}. Example 13 (5 points) Draw a binary search tree of set {50, 25, 73, 35, 63, 84, 10, 15, 31, 22, 44, 4, 1, 72}. Example 14 (4 points) Show the path of binary search tree in an Example 13 if find 10, 31, 35, 19. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 6. 6 CSC1001 Discrete Mathematics 10 - Trees 2. Decision Trees Definition 2 A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree. Example 15 (5 points) Draw a decision tree to find the possible solutions when the coin are rolled at 3 times. Example 16 (5 points) Draw a decision tree of sorted a, b and c variables. 3. Trees Traversal We will describe three of the most commonly used such algorithms, preorder traversal, inorder traversal, and postorder traversal. Definition 3 Let T be an ordered 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 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. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 7. Trees - 10 CSC1001 Discrete Mathematics 7 Definition 4 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 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. Definition 5 Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, . . . , then Tn in postorder, and ends by visiting r. Example 17 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the ordered rooted tree T shown in Figure? Example 18 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the ordered rooted tree shown in Figure? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 8. 8 CSC1001 Discrete Mathematics 10 - Trees Example 19 (6 points) Show the order list of preorder, inorder and postorder traversal visit the vertices in the ordered rooted tree shown in Figure? 4. Infix, Prefix, and Postfix Notation Definition 6 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. Each operation operates on its left and right subtrees (in that order). There are 3 forms to represent the expressions, (1) Infix form A+B, (2) Prefix form +AB, and (3) Postfix form AB+ Example 20 (4 points) Show the prefix and postfix form of the expression a + b * c – d / e? Example 21 (8 points) Draw a tree which represented the expression a + b * c – d / e and show prefix and postfix form by using preorder and postorder traversal? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 9. Trees - 10 CSC1001 Discrete Mathematics 9 Example 22 (8 points) Draw a tree which represented the expression (a / b – c * d) / (e * f + g – h) and show prefix and postfix form by using preorder and postorder traversal? Example 23 (8 points) Draw a tree which represented the expression a / b – c * (d + e) * (f / g + h) – i * j and show prefix and postfix form by using preorder and postorder traversal? 3 Spanning Trees 1. Introduction to Spanning Trees Definition 1 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 with a spanning tree must be connected, because there is a path in the spanning tree between any two vertices. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 10. 10 CSC1001 Discrete Mathematics 10 - Trees Example 24 (15 points) Find a spanning tree for the graph shown by removing edges in simple circuits. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 11. Trees - 10 CSC1001 Discrete Mathematics 11 2. Depth-First Search and Breadth-First Search We can also produce a spanning tree of a simple graph by the use of depth-first search and breadth- first search algorithm. 1) Example of depth-first search of graph G (start with the vertex f) 2) Example of breadth-first search of graph G (start with the vertex e) Example 25 (15 points) Use depth-first search and breadth-first search to produce a spanning tree for the given simple graph. Choose a as the root of this spanning tree and assume that the vertices are ordered alphabetically. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 12. 12 CSC1001 Discrete Mathematics 10 - Trees 3. Minimum Spanning Trees Definition 2 A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 13. Trees - 10 CSC1001 Discrete Mathematics 13 Definition 3 The Prim’s Algorithm procedure Prim(G: weighted connected undirected graph with n vertices) { T = a minimum-weight edge for i = 1 to n − 2 { e = an edge of minimum weight incident to a vertex in T and not forming a simple circuit in T if added to T T = T with e added } return T //a minimum spanning tree of G } Definition 4 The Kruskal’s Algorithm procedure Kruskal(G: weighted connected undirected graph with n vertices) { T = empty graph for i = 1 to n − 1 { e = any edge in G with smallest weight that does not form a simple circuit when added to T T = T with e added } return T //a minimum spanning tree of G } มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 14. 14 CSC1001 Discrete Mathematics 10 - Trees Example 26 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in the weighted graph shown in Figure. 1) Use Prim’s algorithm 2) Use Kruskal’s algorithm Example 27 (10 points) Use Prim’s algorithm and Kruskal’s algorithm to find a minimum spanning tree in the weighted graph shown in Figure. 1) Use Prim’s algorithm 2) Use Kruskal’s algorithm มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 15. Trees - 10 CSC1001 Discrete Mathematics 15 Example 28 (5 points) The roads represented by this graph are all unpaved. The lengths of the roads between pairs of towns are represented by edge weights. Which roads should be paved so that there is a path of paved roads between each pair of towns so that a minimum road length is paved? 1) Use Prim’s algorithm 2) Use Kruskal’s algorithm มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี