Lecture 12: Graph Theory Tree - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Lecture 12: Graph Theory Tree

Description:

– PowerPoint PPT presentation

Number of Views:1956
Avg rating:3.0/5.0
Slides: 45
Provided by: hart66
Category:
Tags: graph | lecture | theory | tree

less

Transcript and Presenter's Notes

Title: Lecture 12: Graph Theory Tree


1
Lecture 12 Graph Theory - Tree
  • Introduction to Trees
  • Basic Definitions
  • Rooted Trees
  • Applications of Trees
  • Trees as Models
  • Binary Search Trees
  • Tree Traversal

2
12.1. Basic Definitions
  • A tree
  • Is a connected graph with no cycles
  • It can not contain multiple edges or loops.
  • Theorems
  • A graph is a tree if and only if there is a
    unique simple path between any two of its
    vertices. (Why?)
  • VE1 (Why?)

3
12.1. Basic Definitions
  • Examples
  • G1 and G2 are trees.
  • G3 and G4 are not a tree.

G4
G1
G2
G3
4
12.1. Basic Definitions
  • A forest
  • Is a graph where each of its connected components
    is a tree.

A graph with three connected components
5
12.1. Basic Definition
  • In chemistry
  • Representing molecules
  • Each vertex represents an atom.
  • Each edge represents the bond between the atoms.
  • Examples

Isobutane
Butane
6
12.2. Rooted Trees
  • A rooted tree
  • Is a directed graph T satisfying two conditions
  • It is a tree if the directions of the edges are
    ignored.
  • There is a unique vertex r such that the indegree
    of r is 0 and the indegree of any other vertex is
    1.
  • This vertex r is called the root of the rooted
    tree.
  • We can change any unrooted tree into a rooted
    tree by choosing any vertex as the root.

7
12.2. Rooted Trees
  • Examples
  • Different choices of the root produce different
    rooted trees.

With root a
T
With root c
8
12.2. Rooted Trees
  • Examples
  • A family tree is a rooted tree where
  • the vertices represents family members and
  • the edges represent parent-child relationship.

9
12.2. Rooted Trees
  • In business
  • Representing the structure of a large
    organization
  • Each vertex represents a position.
  • Each edge indicates that the person represented
    by the initial vertex is the (direct) boss of the
    person represented by the terminal vertex.
  • Examples

10
12.2. Rooted Trees
  • In computer science
  • Representing computer file systems
  • The root represents the root directory.
  • Each internal vertex represents a subdirectory.
  • A leave represents an ordinary file or empty
    subdir.
  • Examples

The root is the root directory / Internal
vertices are directories Leaves are files.
11
12.2. Rooted Trees
  • Rooted tree terminology
  • For a directed edge from vertex u to vertex v
  • u is a parent of v.
  • v is a child of u.
  • For a vertex u
  • Any vertices (excluding vertex u) on the path
    from the root to the vertex u are the ancestors
    of u.
  • Vertex u is the descendant of these vertices.
  • A terminal vertex or a leaf is a vertex that has
    no children. (There must be at least one leaf.
    Why?)
  • An internal vertex is one that has children.
  • A subtree is a subgraph of a tree.

12
12.2. Rooted Trees
  • Examples
  • Find the followings
  • Children of b?
  • Parent of b?
  • Ancestors of f?
  • Descendant of a?
  • Terminal vertices (leaves)?
  • Internal vertices?

13
12.2. Rooted Trees
  • Examples
  • A tree T and its subtree rooted at g.

T
14
12.2. Rooted Trees
  • Rooted tree terminology (continued)
  • An m-ary tree
  • Is a rooted tree in which each internal vertex
    has at most m children.
  • Special case binary tree where m 2.
  • A complete m-ary tree
  • Is a rooted tree in which each internal vertex
    has exactly m children.
  • A balanced m-ary tree with height h
  • Is a m-ary tree with all leaves being at levels h
    or h-1.
  • Level distance from root
  • Height maximum level

15
12.2. Rooted Trees
  • Examples
  • T1 is a complete binary tree. T1 is not balanced.
  • T2 is a balanced complete 5-ary tree.
  • T3 is a balanced 3-ary but not a complete m-ary
    tree for any m.

T3
T2
T1
16
12.3. Ordered Rooted Trees
  • Whats the total number of nodes (vertices) for a
    complete balanced binary tree of height h?
  • ?ltNlt?

17
12.3. Ordered Rooted Trees
  • An ordered rooted tree
  • Is a rooted tree where the children of each
    internal vertex are ordered.
  • In an ordered binary tree, if an internal vertex
    u has two children
  • The first child is called the left child.
  • The tree rooted at the left child is called the
    left subtree of vertex u.
  • The second child is called the right child.
  • The tree rooted at the right child is called the
    right subtree of vertex u.

18
12.2. Rooted Trees
  • Examples
  • The left child of d is f.
  • The right child of d is g.
  • (b) and (c) show the left and right subtrees of c.

19
12.5. Tree Traversal
  • Tree traversal
  • Is a procedure that systematically visits every
    vertex of an ordered rooted tree.
  • Visiting a vertex processing the data at the
    vertex.
  • Three most commonly used algorithms
  • Preorder traversal.
  • Inorder traversal.
  • Postorder traversal.

20
12.5.1. Preorder Traversal
  • Applications print a structured document

21
12.5.2. Inorder Traversal
  • Applications print out information stored in a
    binary search tree.

6
2
8
1
7
9
4
3
5
22
12.5.3. Postorder Traversal
  • Applications compute space used by files in a
    directory and its subdirectories.

9
cs16/
8
3
7
todo.txt1K
homeworks/
programs/
4
5
6
1
2
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
23
12.5. Tree Traversal
  • Let T be an ordered rooted tree with root r.
  • Suppose that T1, T2, ?, Tn are the subtrees at r
    from left to right in T.

T2
T3
T1
24
12.5.1. Preorder Traversal
  • Preorder traversal
  • Begins by visiting r.
  • Next traverses T1 in preorder, then T2 in
    preorder, ?, then Tn in preorder.
  • Ends after Tn has been traversed.

Preorder traversal
25
12.5.1. Preorder Traversal
  • Preorder traversal
  • An example of recursive algorithm.

26
12.5.1. Preorder Traversal
  • Examples
  • Preorder traversal
  • Visit root, visit subtrees left to right.

27
12.5.1. Preorder Traversal
28
12.5.1. Preorder Traversal
  • In binary tree the traversal is characterized by
  • Visiting a parent before its children and a left
    child before a right child.
  • Algorithm
  • Step 1 Visit the root.
  • Step 2 Go to the left subtree.
  • If one exists, do a preorder
    traversal.
  • Step 3 Go to the right subtree.
  • If one exists, do a preorder
    traversal.

29
12.5.1. Preorder Traversal
  • Applications print a structured document

30
12.5.2. Inorder Traversal
  • Inorder traversal
  • Begins by traversing T1 in inorder.
  • Next visits r, then traverses T2 in inorder, ?,
    then Tn in inorder.
  • Ends after Tn has been traversed.

In order traversal
31
12.5.2. Inorder Traversal
  • Examples
  • Inorder traversal
  • Visit leftmost subtree, visit root, visit other
    subtrees left to right.

32
12.5.2. Inorder Traversal
33
12.5.2. Inorder Traversal
  • Applications print out information stored in a
    binary search tree.

6
2
8
1
7
9
4
3
5
34
12.5.3. Postorder Traversal
  • Postorder traversal
  • Begins by traversing T1 in postorder.
  • Next traverses T2 in postorder, then traverses T3
    in postorder, ?, then Tn in postorder.
  • Ends by visiting r.

Postorder traversal
35
12.5.3. Postorder Traversal
  • Examples
  • Postorder traversal
  • Visit subtrees left to right, visit root.

36
12.5.3. Postorder Traversal
37
12.5.3. Postorder Traversal
  • Applications compute space used by files in a
    directory and its subdirectories.

9
cs16/
8
3
7
todo.txt1K
homeworks/
programs/
4
5
6
1
2
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
38
12.5. Tree Traversal
  • Shortcut to list the vertices in preorder,
    inorder, postorder
  • Draw a curve around the ordered rooted tree
    starting at the root and move along the edges.

39
12.5. Tree Traversal
  • Preorder list is obtained by
  • listing each vertex the first time this curve
    passes it.
  • Result a, b, d, h, e, i, j, c, f, g, k.
  • Inorder list is obtained by
  • listing a leaf the first time the curve passes it
    and
  • listing each internal vertex the second time the
    curve passes it.
  • Result h, d, b, i, e, j, a, f, c, k, g.
  • Postorder list is obtained by
  • listing a vertex the last time it is passes on
    the way back up to its parent.
  • Result h, d, i, j, e, b, f, k, g, c, a.

40
12.4. Binary Search Trees
  • Ordered rooted trees are often used to store
    information.
  • A binary search tree
  • Is a binary tree in which each vertex is labeled
    with a key such that
  • No two vertices have the same key.
  • If vertex u belongs to the left subtree of vertex
    v, then u ? v.
  • If vertex w belongs to the right subtree of
    vertex v, then v ? w.

41
12.4. Binary Search Trees
  • Binary search tree construction algorithm
  • Start with a tree containing just one vertex (the
    root).
  • Let v the root.
  • To add a new item a, do the following until stop
  • If a lt v
  • If v has a left child then v left child of v
    (move to the left)
  • Else add a new left child to v with this item as
    its key. Stop.
  • If a gt v
  • If v has a right child then v right child of v
    (move to the right).
  • Else add a new right child to v with this item as
    its key. Stop.

42
12.4. Binary Search Trees
  • Examples
  • Construct a binary search tree for the list 5, 9,
    8, 1, 2, 4, 10, 6.

43
12.4. Binary Search Trees
  • Binary search tree search algorithm
  • Let v the root.
  • To search the item a, do the following until
    stop
  • If a v then stop.
  • If a lt v
  • If v has a left child then v left child of v
    (move to the left)
  • Else stop.
  • If a gt v
  • If v has a right child then v right child of v
    (move to the right).
  • Else stop.

44
12.4. Binary Search Trees
  • Examples
  • Search for 7 in the following binary tree.
  • If not found, add it to the tree.

45
12.5. Tree Traversal
  • How many comparisons best case, worst case?

46
12.6. Further Readings
  • Introduction to Trees
  • Basic Definitions Section 12.1.
  • Rooted Trees Section 12.2.
  • Applications of Trees
  • Tree Traversal Section 12.2.
Write a Comment
User Comments (0)
About PowerShow.com