SlideShare una empresa de Scribd logo
1 de 62
Descargar para leer sin conexión
Trees




Birla Institute of Technology
Trees
  A very important type of graph in CS is
    called a tree:




  Real
  Tree        transformation

L23                                         2
Trees – Their Definition
  Let A be a set and let T be a relation on
  A. We say that T is a tree if there is a
  vertex v0 with the property that there
  exists a unique path in T from v0 to
  every other vertex in A, but no path
  from v0 to v0.
Trees – Our Definition

  We need a way to describe a tree,
  specifically a “rooted” tree.
      First, a rooted tree has a single root, v0,
       which is a vertex with absolutely no edges
       coming into it. (in-degree of v0 = 0)
      Every other vertex, v, in the tree has
       exactly one path to it from v0. (in-degree
       of v = 1)
      There may be any number of paths coming
       out from any vertex.
      Denoted (T,v0) is a rooted tree
Definitions
  Levels – all of the vertices located n-
  edges from v0 are said to be at level n.
                           Level 0



                                Level 1


                                     Level 2

                                      Level 3
More Definitions
  A vertex, v, is considered the parent of
  all of the vertices connected to it by
  edges leaving v.
  A vertex, v, is considered the offspring
  of the vertex connected to the single
  edge entering v.
  A vertex, v, is considered the sibling of
  all vertices at the same level with the
  same parent.
More Definitions
  A vertex v2 is considered a descendant
  of a vertex v1 if there is a path from v1
  to v2.
  The height of a tree is the number of
  the largest level.
  The vertices of a tree that have no
  offspring are considered leaves.
  If the vertices of a level of a tree can be
  ordered from left to right, then the tree
  is an ordered tree.
More Definitions
  If every vertex of a tree has at most n
  offspring, then the tree is considered an
  n-tree.
  If every vertex of a tree with offspring
  has exactly n offspring, then the tree is
  considered a complete n-tree.
  When n=2, this is called a binary tree.
Giving Meaning to
Vertices and Edges
  Trees implied that a vertex is simply an entity
  with parents and offspring much like a family
  tree.
  What if the position of a vertex relative to its
  siblings or the vertex itself represented an
  operation.
  Examples:
    Edges from a vertex represent cases from
     a switch statement in software
    Vertex represented a mathematical
Mathematical Order of
Precedence Represented with
Trees
  Consider the equation:
       (3 – (2  x)) + ((x – 2) – (3 + x))
  Each element is combined with another using
  an operator, i.e., this expression can be
  broken down into a hierarchy of (a  b) where
  “” represents an operation used to combine
  two elements.
  We can use a binary tree to represent this
  equation with the elements as the leaves.
Precedence Example Tree
                    +


        –                       –


    3                      –           +



            2       x   x       2   3       x
Positional Tree
   A positional tree is an n-tree that
   relates the direction/angle an edge
   comes out of a vertex to a characteristic
   of that vertex. For example:
 Yes        Maybe   Left   Right   X=0         X=2
       No                                X=1




   When n=2, then we have a positional
   binary tree.
Tree to Convert Base-2 to Base-
       10
                 Starting with the first digit, take the left or right edge
                 to follow the path to the base-10 value.
       First digit                     0                   1




Second digit  0                1                                   0            1


3rd
digit  0           1           0           1           0           1            0           1


4th
 0        1    0       1   0       1   0       1   0   1       0       1    0       1   0       1

   0      1     2       3   4   5       6       7   8   9       10      11   12 13 14 15
For-Loop Represented with Tree
for i = 1 to 3
    for j = 1 to 5
        array[i,j] = 10*i + j
    next j
next i
For Loop Positional Tree
            i=1                    i=3
                    i=2


 j=1
 j=2
 j=3
 j=4
 j=5


 11 12 13 14 15   21 22 23 24 25    31 32 33 34 35
Storing Binary Trees in
Computer
  “linked lists”. Each item in the list was
  comprised of two components:
     Data
     Pointer to next item in list
  Positional binary trees require two links,
  one following the right edge and one
  following the left edge. This is referred
  to as a “doubly linked list.”

                Left Pointer   Data   Right
Represent in computer using
 Linked List
                                 + (2)


               – (3)                              – (8)


       3 (4)             (5)             – (9)           + (12)



                       2 (6)    x (7)    x (10)    2 (11)   3 (13)   x (14)


The numbers in parenthesis represent the index from which
they Can be shown in the linked list
Doubly Linked List
 Index    Left          Data      Right
  1        2            -------    0
  2        3              +        8
  3        4     root     –        5
  4        0              3        0
  5        6                      7
  6        0              2        0
  7        0              x        0
  8        9              –        12
  9       10              –        11
  10       0              x        0
  11       0              2        0
  12      13              +        14
  13       0              3        0
  14       0              x        0
Huffman Code
  Depending on the frequency of the
  letters occurring in a string, the
  Huffman Code assigns patterns of
  varying lengths of 1’s and 0’s to
  different letters.
  These patterns are based on the paths
  taken in a binary tree.
  A Huffman Code Generator can be
  found at:
               http://www.inf.puc-
      rio.br/~sardinha/Huffman/Huffman.html
Searching Trees
Terminology

  “Visiting” a vertex – the act of
  performing a task at a vertex, e.g.,
  perform a computation or make a
  decision.
  “Searching” the tree – the process of
  visiting each vertex in a specific order
  or path. The term “searching” can be
  misleading. Just think of it as
  “traversing” the tree.
Tree Search
  The application of some trees involves
  traversing the tree in a methodical pattern so
  as to address every vertex.
  Our book uses the term “search”, but
  sometimes search can imply we’re looking for
  a particular vertex. This is not the case.
  Example:
  Assume we want to compute the average
  age, maximum age, and minimum age of all
  of the children from five families. (Tree is on
  next slide.)
Search Example
                               Neighborhood
                               families



        A. Jones   Halls            Smith           Taylor            B. Jones




Katy      Tommy            Taylor           Lori             Karen            Mike
age 3     age 5            age 1            age 4            age 14           age 6

                   Phil             Lexi            Bart              Ben
                   age 8            age 2           age 12            age 2
Search Example (continued)
  To calculate the average, max, and
  min ages for all of the children, we
  need to have a method for going
  through the tree so that we don’t miss
  a child.
  By defining a rigorous process, not
  only can a human be sure not to miss
  a vertex, but also an algorithm can be
  defined for a computer
A Suggested Process for
Searching a Tree
1.Starting at the root, repeatedly take the
  leftmost “untraveled” edge until you arrive at
  a leaf which should be a child.
2.Include this child in the average, max, and
  min calculations.
3.One at a time, go back up the edges until you
  reach a vertex that hasn’t had all of its
  outgoing edges traveled.
4.If you get back to the root and cannot find an
  untraveled edge, you are done. Otherwise,
  return to step 1.
Vertices Numbered
        in Order of Visits
                                   1 Neighborhood
                                     families

             2                 5            7                                  14
                                                          11
            A. Jones   Halls              Smith             Taylor            B. Jones


        3        4                    8           10                 13               16
Katy          Tommy            Taylor             Lori               Karen            Mike
age 3         age 5            age 1              age 4              age 14           age 6
                          6                  9                12                 15
                       Phil               Lexi             Bart               Ben
                       age 8              age 2            age 12             age 2
Preorder Search
  This methodical pattern of traversing a tree is
  called a preorder search.
  Assume v is the root of a binary positional
  tree T.
     Each vertex of this tree has at most a left vertex,
      vL, and a right vertex, vR.
     If either vL or vR have offspring, then they are
      subtrees of T, and a search can be performed of
      them too.
     By viewing a tree this way, then the search
      method we described in earlier slides can be
      performed using a recursive algorithm applied to
      each vertex.
     The recursive algorithm is repeatedly applied until
      every leaf has been reached.
Preorder Search Algorithm
   A preorder search of a tree has the
   following three steps:
  1.   Visit the root
  2.   Search the left subtree if it exists
  3.   Search the right subtree if it exists
   The term “search” in steps 2 and 3
   implies that we apply all three steps to
   the subtree beginning with step 1.
Vertices Visited in Alphabetical
Order Using Preorder Search

                     A


             B                   H


     C           E       I           K


D        F                   G   J       L
Prefix or Polish Form
      Binary tree representing: (a – b) × (c + (d ÷ e))
                     x


           –                      +

  a              b        c                 ÷

                                 d                  e
      Preorder search produces: × – a b + c ÷ d e
Polish Form (continued)
  Allows us to write complex arithmetic
  expressions without using parenthesis
  Expression is evaluated by performing
  following steps:
     Move left to right until you find a string of
      the form Fxy, where F is the symbol for a
      binary operation and x and y are numbers.
     Evaluate x F y and substitute answer for
      string Fxy.
     Repeat starting at beginning of string
      again.
Polish Form Example
1.   ×–   6 4 + 5 ÷ 2 2 1st pattern: – 6 4
2.   ×2   +5÷22        2nd pattern: ÷ 2 2
3.   ×2   +51         3rd pattern: + 5 1
4.   ×2   6           4th pattern: × 2 6
5.   12
 (6 – 4) × (5 + (2 ÷ 2)) = 12
Inorder and Postorder
Searches
  Preorder search gets its name from the fact
  that the operator that joins two items is
  evaluated first, e.g., the binary operation 6 –
  4 is visited in the order – 6 4.
  Inorder search evaluates the expression as it
  is written, e.g., the binary operation 6 – 4 is
  visited in the order 6 – 4.
  Postorder search evaluates the operator after
  the elements are read, e.g., the binary
  operation 6 – 4 is visited in the order 6 4 –.
Inorder Search Algorithm
   An inorder search of a tree has the
   following three steps:

  1.   Search the left subtree if it exists
  2.   Visit the root
  3.   Search the right subtree if it exists
Postorder Search Algorithm
   A postorder search of a tree has the
   following three steps:

  1.   Search the left subtree if it exists
  2.   Search the right subtree if it exists
  3.   Visit the root
Evaluation of Tree Using
Inorder Search
                     A


             B                   H


     C           E       I           K


D        F                   G   J       L



    Resulting string: DCBFEGAIJHKL
Evaluation of Tree Using
Postorder Search
                     A


             B                   H


     C           E       I           K


D        F                   G   J       L



    Resulting string: DCFGEBJILKHA
Infix Formrepresenting: (a – b) × (c + (d ÷ e))
     Binary tree
                      x


           –                     +

   a              b       c               ÷

                                 d                e
       Inorder search produces: a – b × c + d ÷ e
       Unfortunately, without parenthesis, we can’t do
       anything with this expression.
Postfix or Reverse Polish Form
      Binary tree representing: (a – b) × (c + (d ÷ e))
                    x


           –                      +

  a              b        c                ÷

                                 d                  e
      Inorder search produces: a b – c d e ÷ + ×
Reverse Polish Form (continued)
  Allows us to write complex arithmetic
  expressions without using parenthesis
  Expression is evaluated by performing
  following steps:
     Move left to right until you find a string of
      the form xyF, where F is the symbol for a
      binary operation and x and y are numbers.
     Evaluate x F y and substitute answer for
      string xyF.
     Repeat starting at beginning of string
      again.
Reverse Polish Form Example
From left-to-right evaluate xyF first.
1. 2 1 – 3 4 2 ÷ + × 1st pattern: 2 1 –
2. 1 3 4 2 ÷ + × 2nd pattern: 4 2 ÷
3. 1 3 2 + ×         3rd pattern: 3 2 +
4. 1 5 ×             4th pattern: 1 5 ×
5. 5
 (2 – 1) × (3 + (4 ÷ 2)) = 5
Converting an Ordered
n-tree to a Positional Binary Tree

  An ordered n-tree where some vertices
  have more than two offspring can be
  converted to a positional binary tree.
  This allows easier computer
  representation with methods such as
  linked lists.
  A process exists for this conversion that
  works on any finite tree.
Process to Convert Ordered n-
tree to Positional Binary Tree
                                               A
  Starting at the root, the first or
  leftmost offspring of a vertex
  remains the leftmost vertex in       B   C       D       E
  the binary tree
  The first sibling to the right
  of the leftmost vertex                       A
  becomes the right offspring
  of the leftmost vertex               B
                                               C
  Subsequent siblings
  become the right offspring                           D
  in succession until last
  sibling is converted.
                                                               E
Conversion Example
                    A


            B       C               D



            F       G       H   I
        E


            J   K       L
Conversion Example
            A                                       A
                                            B
    B       C               D       E               C

                                        F       G           D
    F       G       H   I
E
                                                        H
                                        J
                                                K               I
    J   K       L
                                                        L

                Preorder search:
                 Left tree – ABEFCGJKLHID
                 Right tree – ABEFCGJKLHID
Lets see the                     City 1        City 2    Mileage

      problem.                       Cleveland     Philadelp    400
                                                   hi
                                     Cleveland     Detroit      200
 A small startup airline wants
 to provide service to the 5         Cleveland     Chicago      350
 cities in the table to the right.   Cleveland     Pittsburg    150
 Allowing for multiple
 connecting flights, determine       Philadelphi   Detroit      600
 all of the direct flights that      Philadelphi   Chicago      700
 would be needed in order to
 service all five cities.            Philadelphi   Pittsburg    300
                                     Detroit       Chicago      300
Source: http://www.usembassy         Detroit       Pittsburg    300
malaysia.org.my/distance.html
                                     Chicago       Pittsburg    450
Minimal Spanning Trees
Undirected Tree

      An undirected tree is simply the
      symmetric closure of a tree.
      It is relation that results from a tree
      where all edge are made bidirectional,
      i.e., there is no defined direction.




L23                                             48
Connected Relation
       A relation is connected if for every a
       and b in R, there is a path from a to b.
       It is easier to see a connected relation
       using a digraph than it is to describe in
       using words.
         A                           A
                 C                                C

   B                          B
                     E                                E
             D                           D
Connected                         Not Connected
Spanning Tree of connected
relations
  Textbook definition: “If R is a symmetric,
  connected relation on a set A, we say that a
  tree T on A is a spanning tree for R if T is a
  tree with exactly the same vertices as R and
  which can be obtained from R by deleting
  some edges of R.”
  Basically, a undirected spanning tree is one
  that connects all n elements of A with n-1
  edges.
  To make a cycle connecting n elements, more
  than n-1 edges will be needed. Therefore,
  there are no cycles.
Weighted Graph
       In the past, we have represented a
       undirected graph with unlabeled edges.
       It can also be represented with a
       symmetric binary matrix.      A
                                            C


MT =     0    1   1    0
         1    0   1    0     B
         1    1   0    1              D
         0    0   1    0
Weighted Graph (continued)
  By giving the edges a numeric value
  indicating some parameter in the
  relation between two vertices, we can
  create a weighted tree.
                 A           3
                                     C
             5
                     4
                                 7
         B
                         D
Weighted Graph (continued)
  We can still use matrix notation to represent
  a weighted graph. Replace the 1’s used to
  represent an edge with the edge’s weight. A
  0 indicates no edge.
                 0     5     3     0
       MT =      5     0     4     0
                 3     4     0     7
                 0     0     7     0
Exercise
(Not drawn to scale)

                                         Detroit



                       300                                   600
                                 200         300

                             Cleveland                 400
                 350
                                                                   Philadelphia
                        700         150
                                                         300
 Chicago         450

                                           Pittsburg
                        Note R relation has 5 vertices .
Minimal Spanning Tree
  Assume T represents a spanning tree for
  an undirected graph.
  The total weight of the spanning tree T is
  the sum of all of the weights of all of the
  edges of T.
  The one(s) with the minimum total weight
  are called the minimal spanning
  tree(s).
  As suggested by the “(s)” in the above
  definition, there may be a number of
  minimal spanning trees for a particular
  undirected graph with the same total
  weight.
Nearest neighbour of a vertex
      A vertex u is nearest neighbour of a
      vertex v if u and v are adjacent and no
      other vertex is joined to v by an edge of
      lesser weight.




L23                                          56
Algorithms for Determining the
Minimal Spanning Tree

  There are two algorithms presented in
  our textbook for determining the
  minimal spanning tree of an undirected
  graph that is connected and weighted.

     Prim’s Algorithm: process of stepping from
      vertex to vertex
     Kruskal’s Algoritm: searching through
      edges for minimum weights
Prim’s Algorithm
    Let R be a symmetric, connected
    relation with n vertices.
   1.   Choose a vertex v1 of R. Let V = {v1} and
        E = { }.
   2.   Choose a nearest neighbor vi of V that is
        adjacent to vj, vj  V, and for which the
        edge (vi, vj) does not form a cycle with
        members of E. Add vi to V and add (vi, vj)
        to E.
   3.   Repeat Step 2 until |E| = n – 1. Then V
        contains all n vertices of R, and E contains
        the edges of a minimal spanning tree for R.
Prim’s Algorithm in English
   The goal is to one at a time include a
   new vertex by adding a new edge
   without creating a cycle
   Pick any vertex to start. From it, pick
   the edge with the lowest weight.
   As you add vertices, you will add
   possible edges to follow to new
   vertices.
   Pick the edge with the lowest weight
   to go to a new vertex without creating
   a cycle.
Kruskal’s Algorithm
    Let R be a symmetric, connected
    relation with n vertices and let S =
    {e1, e2, e3, …ek} be the set of all
    weighted edges of R.
   1.   Choose an edge e1 in S of least weight.
        Let E = {e1}. Replace S with S – {e1}.
   2.   Select an edge ei in S of least weight that
        will not make a cycle with members of E.
        Replace E with E  {ei} and S with S –
        {ei}.
   3.   Repeat Step 2 until |E| = n – 1.
Kruskal’s Algorithm in English
   The goal is to one at a time include a
   new edge without creating a cycle.
   Start by picking the edge with the
   lowest weight.
   Continue to pick new edges without
   creating a cycle. Edges do not
   necessarily have to be connected.
   Stop when you have n-1 edges as R
   have n vertices.
Answer: Kruskal to Find MST
  (Not drawn to scale)

                     B                   D
                                                     2
                                                                    H

             3                               2

                                 5       E
  A                      C
                 2
                                                 3
                                     4
                             F                       G


Sequence of edge selections (D,E), (D,H)(A,C)(A,B)(E,G)(E,F)(C,E)
=2+2+2+3+3+4+5=21,

Más contenido relacionado

Similar a Trees ayaz

Pattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_treesPattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_trees
Nathan Gabriel
 
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
22001003058
 

Similar a Trees ayaz (20)

Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Binary trees
Binary treesBinary trees
Binary trees
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Tree data structure.pptx
Tree data structure.pptxTree data structure.pptx
Tree data structure.pptx
 
Pattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_treesPattern_avoidance_in_ternary_trees
Pattern_avoidance_in_ternary_trees
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree
TreeTree
Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
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
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Trees
TreesTrees
Trees
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Trees ayaz

  • 2. Trees A very important type of graph in CS is called a tree: Real Tree transformation L23 2
  • 3. Trees – Their Definition Let A be a set and let T be a relation on A. We say that T is a tree if there is a vertex v0 with the property that there exists a unique path in T from v0 to every other vertex in A, but no path from v0 to v0.
  • 4. Trees – Our Definition We need a way to describe a tree, specifically a “rooted” tree.  First, a rooted tree has a single root, v0, which is a vertex with absolutely no edges coming into it. (in-degree of v0 = 0)  Every other vertex, v, in the tree has exactly one path to it from v0. (in-degree of v = 1)  There may be any number of paths coming out from any vertex.  Denoted (T,v0) is a rooted tree
  • 5. Definitions Levels – all of the vertices located n- edges from v0 are said to be at level n. Level 0 Level 1 Level 2 Level 3
  • 6. More Definitions A vertex, v, is considered the parent of all of the vertices connected to it by edges leaving v. A vertex, v, is considered the offspring of the vertex connected to the single edge entering v. A vertex, v, is considered the sibling of all vertices at the same level with the same parent.
  • 7. More Definitions A vertex v2 is considered a descendant of a vertex v1 if there is a path from v1 to v2. The height of a tree is the number of the largest level. The vertices of a tree that have no offspring are considered leaves. If the vertices of a level of a tree can be ordered from left to right, then the tree is an ordered tree.
  • 8. More Definitions If every vertex of a tree has at most n offspring, then the tree is considered an n-tree. If every vertex of a tree with offspring has exactly n offspring, then the tree is considered a complete n-tree. When n=2, this is called a binary tree.
  • 9. Giving Meaning to Vertices and Edges Trees implied that a vertex is simply an entity with parents and offspring much like a family tree. What if the position of a vertex relative to its siblings or the vertex itself represented an operation. Examples:  Edges from a vertex represent cases from a switch statement in software  Vertex represented a mathematical
  • 10. Mathematical Order of Precedence Represented with Trees Consider the equation: (3 – (2  x)) + ((x – 2) – (3 + x)) Each element is combined with another using an operator, i.e., this expression can be broken down into a hierarchy of (a  b) where “” represents an operation used to combine two elements. We can use a binary tree to represent this equation with the elements as the leaves.
  • 11. Precedence Example Tree + – – 3  – + 2 x x 2 3 x
  • 12. Positional Tree A positional tree is an n-tree that relates the direction/angle an edge comes out of a vertex to a characteristic of that vertex. For example: Yes Maybe Left Right X=0 X=2 No X=1 When n=2, then we have a positional binary tree.
  • 13. Tree to Convert Base-2 to Base- 10 Starting with the first digit, take the left or right edge to follow the path to the base-10 value. First digit  0 1 Second digit  0 1 0 1 3rd digit  0 1 0 1 0 1 0 1 4th  0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 14. For-Loop Represented with Tree for i = 1 to 3 for j = 1 to 5 array[i,j] = 10*i + j next j next i
  • 15. For Loop Positional Tree i=1 i=3 i=2 j=1 j=2 j=3 j=4 j=5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35
  • 16. Storing Binary Trees in Computer “linked lists”. Each item in the list was comprised of two components:  Data  Pointer to next item in list Positional binary trees require two links, one following the right edge and one following the left edge. This is referred to as a “doubly linked list.” Left Pointer Data Right
  • 17. Represent in computer using Linked List + (2) – (3) – (8) 3 (4)  (5) – (9) + (12) 2 (6) x (7) x (10) 2 (11) 3 (13) x (14) The numbers in parenthesis represent the index from which they Can be shown in the linked list
  • 18. Doubly Linked List Index Left Data Right 1 2 ------- 0 2 3 + 8 3 4 root – 5 4 0 3 0 5 6  7 6 0 2 0 7 0 x 0 8 9 – 12 9 10 – 11 10 0 x 0 11 0 2 0 12 13 + 14 13 0 3 0 14 0 x 0
  • 19. Huffman Code Depending on the frequency of the letters occurring in a string, the Huffman Code assigns patterns of varying lengths of 1’s and 0’s to different letters. These patterns are based on the paths taken in a binary tree. A Huffman Code Generator can be found at: http://www.inf.puc- rio.br/~sardinha/Huffman/Huffman.html
  • 21. Terminology “Visiting” a vertex – the act of performing a task at a vertex, e.g., perform a computation or make a decision. “Searching” the tree – the process of visiting each vertex in a specific order or path. The term “searching” can be misleading. Just think of it as “traversing” the tree.
  • 22. Tree Search The application of some trees involves traversing the tree in a methodical pattern so as to address every vertex. Our book uses the term “search”, but sometimes search can imply we’re looking for a particular vertex. This is not the case. Example: Assume we want to compute the average age, maximum age, and minimum age of all of the children from five families. (Tree is on next slide.)
  • 23. Search Example Neighborhood families A. Jones Halls Smith Taylor B. Jones Katy Tommy Taylor Lori Karen Mike age 3 age 5 age 1 age 4 age 14 age 6 Phil Lexi Bart Ben age 8 age 2 age 12 age 2
  • 24. Search Example (continued) To calculate the average, max, and min ages for all of the children, we need to have a method for going through the tree so that we don’t miss a child. By defining a rigorous process, not only can a human be sure not to miss a vertex, but also an algorithm can be defined for a computer
  • 25. A Suggested Process for Searching a Tree 1.Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf which should be a child. 2.Include this child in the average, max, and min calculations. 3.One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled. 4.If you get back to the root and cannot find an untraveled edge, you are done. Otherwise, return to step 1.
  • 26. Vertices Numbered in Order of Visits 1 Neighborhood families 2 5 7 14 11 A. Jones Halls Smith Taylor B. Jones 3 4 8 10 13 16 Katy Tommy Taylor Lori Karen Mike age 3 age 5 age 1 age 4 age 14 age 6 6 9 12 15 Phil Lexi Bart Ben age 8 age 2 age 12 age 2
  • 27. Preorder Search This methodical pattern of traversing a tree is called a preorder search. Assume v is the root of a binary positional tree T.  Each vertex of this tree has at most a left vertex, vL, and a right vertex, vR.  If either vL or vR have offspring, then they are subtrees of T, and a search can be performed of them too.  By viewing a tree this way, then the search method we described in earlier slides can be performed using a recursive algorithm applied to each vertex.  The recursive algorithm is repeatedly applied until every leaf has been reached.
  • 28. Preorder Search Algorithm A preorder search of a tree has the following three steps: 1. Visit the root 2. Search the left subtree if it exists 3. Search the right subtree if it exists The term “search” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1.
  • 29. Vertices Visited in Alphabetical Order Using Preorder Search A B H C E I K D F G J L
  • 30. Prefix or Polish Form Binary tree representing: (a – b) × (c + (d ÷ e)) x – + a b c ÷ d e Preorder search produces: × – a b + c ÷ d e
  • 31. Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps:  Move left to right until you find a string of the form Fxy, where F is the symbol for a binary operation and x and y are numbers.  Evaluate x F y and substitute answer for string Fxy.  Repeat starting at beginning of string again.
  • 32. Polish Form Example 1. ×– 6 4 + 5 ÷ 2 2 1st pattern: – 6 4 2. ×2 +5÷22 2nd pattern: ÷ 2 2 3. ×2 +51 3rd pattern: + 5 1 4. ×2 6 4th pattern: × 2 6 5. 12 (6 – 4) × (5 + (2 ÷ 2)) = 12
  • 33. Inorder and Postorder Searches Preorder search gets its name from the fact that the operator that joins two items is evaluated first, e.g., the binary operation 6 – 4 is visited in the order – 6 4. Inorder search evaluates the expression as it is written, e.g., the binary operation 6 – 4 is visited in the order 6 – 4. Postorder search evaluates the operator after the elements are read, e.g., the binary operation 6 – 4 is visited in the order 6 4 –.
  • 34. Inorder Search Algorithm An inorder search of a tree has the following three steps: 1. Search the left subtree if it exists 2. Visit the root 3. Search the right subtree if it exists
  • 35. Postorder Search Algorithm A postorder search of a tree has the following three steps: 1. Search the left subtree if it exists 2. Search the right subtree if it exists 3. Visit the root
  • 36. Evaluation of Tree Using Inorder Search A B H C E I K D F G J L Resulting string: DCBFEGAIJHKL
  • 37. Evaluation of Tree Using Postorder Search A B H C E I K D F G J L Resulting string: DCFGEBJILKHA
  • 38. Infix Formrepresenting: (a – b) × (c + (d ÷ e)) Binary tree x – + a b c ÷ d e Inorder search produces: a – b × c + d ÷ e Unfortunately, without parenthesis, we can’t do anything with this expression.
  • 39. Postfix or Reverse Polish Form Binary tree representing: (a – b) × (c + (d ÷ e)) x – + a b c ÷ d e Inorder search produces: a b – c d e ÷ + ×
  • 40. Reverse Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps:  Move left to right until you find a string of the form xyF, where F is the symbol for a binary operation and x and y are numbers.  Evaluate x F y and substitute answer for string xyF.  Repeat starting at beginning of string again.
  • 41. Reverse Polish Form Example From left-to-right evaluate xyF first. 1. 2 1 – 3 4 2 ÷ + × 1st pattern: 2 1 – 2. 1 3 4 2 ÷ + × 2nd pattern: 4 2 ÷ 3. 1 3 2 + × 3rd pattern: 3 2 + 4. 1 5 × 4th pattern: 1 5 × 5. 5 (2 – 1) × (3 + (4 ÷ 2)) = 5
  • 42. Converting an Ordered n-tree to a Positional Binary Tree An ordered n-tree where some vertices have more than two offspring can be converted to a positional binary tree. This allows easier computer representation with methods such as linked lists. A process exists for this conversion that works on any finite tree.
  • 43. Process to Convert Ordered n- tree to Positional Binary Tree A Starting at the root, the first or leftmost offspring of a vertex remains the leftmost vertex in B C D E the binary tree The first sibling to the right of the leftmost vertex A becomes the right offspring of the leftmost vertex B C Subsequent siblings become the right offspring D in succession until last sibling is converted. E
  • 44. Conversion Example A B C D F G H I E J K L
  • 45. Conversion Example A A B B C D E C F G D F G H I E H J K I J K L L Preorder search: Left tree – ABEFCGJKLHID Right tree – ABEFCGJKLHID
  • 46. Lets see the City 1 City 2 Mileage problem. Cleveland Philadelp 400 hi Cleveland Detroit 200 A small startup airline wants to provide service to the 5 Cleveland Chicago 350 cities in the table to the right. Cleveland Pittsburg 150 Allowing for multiple connecting flights, determine Philadelphi Detroit 600 all of the direct flights that Philadelphi Chicago 700 would be needed in order to service all five cities. Philadelphi Pittsburg 300 Detroit Chicago 300 Source: http://www.usembassy Detroit Pittsburg 300 malaysia.org.my/distance.html Chicago Pittsburg 450
  • 48. Undirected Tree An undirected tree is simply the symmetric closure of a tree. It is relation that results from a tree where all edge are made bidirectional, i.e., there is no defined direction. L23 48
  • 49. Connected Relation A relation is connected if for every a and b in R, there is a path from a to b. It is easier to see a connected relation using a digraph than it is to describe in using words. A A C C B B E E D D Connected Not Connected
  • 50. Spanning Tree of connected relations Textbook definition: “If R is a symmetric, connected relation on a set A, we say that a tree T on A is a spanning tree for R if T is a tree with exactly the same vertices as R and which can be obtained from R by deleting some edges of R.” Basically, a undirected spanning tree is one that connects all n elements of A with n-1 edges. To make a cycle connecting n elements, more than n-1 edges will be needed. Therefore, there are no cycles.
  • 51. Weighted Graph In the past, we have represented a undirected graph with unlabeled edges. It can also be represented with a symmetric binary matrix. A C MT = 0 1 1 0 1 0 1 0 B 1 1 0 1 D 0 0 1 0
  • 52. Weighted Graph (continued) By giving the edges a numeric value indicating some parameter in the relation between two vertices, we can create a weighted tree. A 3 C 5 4 7 B D
  • 53. Weighted Graph (continued) We can still use matrix notation to represent a weighted graph. Replace the 1’s used to represent an edge with the edge’s weight. A 0 indicates no edge. 0 5 3 0 MT = 5 0 4 0 3 4 0 7 0 0 7 0
  • 54. Exercise (Not drawn to scale) Detroit 300 600 200 300 Cleveland 400 350 Philadelphia 700 150 300 Chicago 450 Pittsburg Note R relation has 5 vertices .
  • 55. Minimal Spanning Tree Assume T represents a spanning tree for an undirected graph. The total weight of the spanning tree T is the sum of all of the weights of all of the edges of T. The one(s) with the minimum total weight are called the minimal spanning tree(s). As suggested by the “(s)” in the above definition, there may be a number of minimal spanning trees for a particular undirected graph with the same total weight.
  • 56. Nearest neighbour of a vertex A vertex u is nearest neighbour of a vertex v if u and v are adjacent and no other vertex is joined to v by an edge of lesser weight. L23 56
  • 57. Algorithms for Determining the Minimal Spanning Tree There are two algorithms presented in our textbook for determining the minimal spanning tree of an undirected graph that is connected and weighted.  Prim’s Algorithm: process of stepping from vertex to vertex  Kruskal’s Algoritm: searching through edges for minimum weights
  • 58. Prim’s Algorithm Let R be a symmetric, connected relation with n vertices. 1. Choose a vertex v1 of R. Let V = {v1} and E = { }. 2. Choose a nearest neighbor vi of V that is adjacent to vj, vj  V, and for which the edge (vi, vj) does not form a cycle with members of E. Add vi to V and add (vi, vj) to E. 3. Repeat Step 2 until |E| = n – 1. Then V contains all n vertices of R, and E contains the edges of a minimal spanning tree for R.
  • 59. Prim’s Algorithm in English The goal is to one at a time include a new vertex by adding a new edge without creating a cycle Pick any vertex to start. From it, pick the edge with the lowest weight. As you add vertices, you will add possible edges to follow to new vertices. Pick the edge with the lowest weight to go to a new vertex without creating a cycle.
  • 60. Kruskal’s Algorithm Let R be a symmetric, connected relation with n vertices and let S = {e1, e2, e3, …ek} be the set of all weighted edges of R. 1. Choose an edge e1 in S of least weight. Let E = {e1}. Replace S with S – {e1}. 2. Select an edge ei in S of least weight that will not make a cycle with members of E. Replace E with E  {ei} and S with S – {ei}. 3. Repeat Step 2 until |E| = n – 1.
  • 61. Kruskal’s Algorithm in English The goal is to one at a time include a new edge without creating a cycle. Start by picking the edge with the lowest weight. Continue to pick new edges without creating a cycle. Edges do not necessarily have to be connected. Stop when you have n-1 edges as R have n vertices.
  • 62. Answer: Kruskal to Find MST (Not drawn to scale) B D 2 H 3 2 5 E A C 2 3 4 F G Sequence of edge selections (D,E), (D,H)(A,C)(A,B)(E,G)(E,F)(C,E) =2+2+2+3+3+4+5=21,