Trees - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Trees

Description:

Isomorphic Trees. Let T1 and T2 be rooted trees with roots r1 and r2. T1 and T2 are isomorphic if there is a one-to-one, onto ... T1 and T2 are isomorphic tree. ... – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 51
Provided by: wsu6
Category:
Tags: isomorphic | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • Terminology and Characterizations
  • Spanning Trees
  • Minimal Spanning Trees
  • Binary Trees
  • Tree Traversals
  • Decision Trees
  • Isomorphisms of Trees

2
Introduction
  • Family trees use vertices to represent the
    members of a family and edges to represent
    parent-child relationships

3
Introduction
  • A computer file system

4
Introduction
  • A file system in Visual Studio .NET

5
Introduction
  • A tree T is a simple graph satisfying If v and w
    are vertices in T, there is a unique simple path
    from v to w.
  • A rooted tree is a tree in which a particular
    vertex is designated the root.

6
Introduction
  • Which of the graphs are trees?

7
Terminology of Trees
  • Let v0 be root, suppose that x, y and z are
    vertices and that (v0, v1, ..., vn) is a simple
    path.
  • vn-1 is the parent of vn.
  • v0, v1, ..., vn-1 are ancestors of vn.
  • vn is a child of vn-1.
  • If x is an ancestor of y, y is descendant of x.
  • If x and y are children of z, x and y are
    siblings
  • If x has no children, x is terminal vertex (or
    leaf)
  • If x is not terminal vertex, x is an internal
    vertex.

8
Terminology of Trees
9
Properties of Trees
  • Let T be a graph with n vertices. The following
    are equivalent.
  • T is a tree.
  • T is connected and acyclic.
  • T is connected and has n-1 edges.
  • T is acyclic and has n-1 edges.
  • Prove if (d) then (a)

10
Binary Tree
  • In a binary tree, an internal vertex has two
    children, left child and right child.
  • The tree rooted at the left child of a vertex is
    called the left subtree of this vertex.

11
Binary Tree
  • A full binary tree is a binary tree in which each
    vertex has either two children or no children.
  • If T is a full binary tree with i internal
    vertices, then T has i1 terminal vertices and
    2i1 total vertices.
  • If a binary tree of height h has t terminal
    vertices, then lg t ? h

12
Huffman Coding
  • When us bit strings to encode the letters of the
    English alphabet.
  • We use 5 bits to represent each of 26 letters
  • The total number of bits used to encode data is
    five times the number of characters in the text.
  • Saving memory space, transmittal time.
  • Is there a coding scheme to encode text with
    fewer bits?
  • Huffman coding. (a graduate term paper by Huffman
    at MIT)
  • The scheme assumes that we already know how many
    times each letter occurs in the text!

13
Huffman Coding
  • procedure Huffman (C symbols ai with frequencies
    wi)
  • F forest of n rooted trees, each
    consisting of
  • the single vertex ai and assigned
    weight wi
  • while F is not a tree
  • begin
  • Replace the rooted trees T and T of
    least weights
  • from F with w(T) w(T) with a tree
    having a new
  • root that has T s its left subtree
    and T as its right
  • subtree. Lable the new edge to T
    with 0 and the
  • new edge to T with 1.
  • Assign w(T) w(T) as the weight of
    the new tree
  • end
  • end Huffman

14
Huffman Coding
  • Use Huffman coding to encode the following
    symbols with frequencies listed A 0.08, B0.10,
    C0.12, D0.15, E0.20, F0.35

15
Huffman Coding
  • Use Huffman coding to encode these symbols with
    given frequencies a 0.20, b 0.10, c 0.15, d
    0.25, e 0.30. What is the average number of bits
    required to encode a character?

16
Binary Search Trees
  • A binary search tree is a binary tree in which
  • each child of a vertex is designated as a right
    or left child, with each vertex labeled with a
    key
  • the key at a vertex is larger than the keys of
    all vertices in its left subtree and smaller than
    the keys of all vertices in its right subtree.

17
Binary Search Trees
  • To build a binary search tree
  • Create a root node, and assign the first item in
    the list as the key of the root node
  • To add a new item, repeatedly compare the new
    item with the keys of vertices already in the
    tree
  • if the item is less than the key of the vertex,
    move to the left
  • if the item is greater than the key of the
    vertex, move to the right
  • If the item is less than the respective vertex
    and this vertex has no left child, add a new
    vertex with this item as a new left child.
  • If the item is greater than the respective vertex
    and this vertex has no right child, add a new
    vertex with this item as a new right child.
  • Repeat steps 2-4 add another items in the list to
    the tree.

18
Binary Search Trees
  • To build a binary search tree for words
    mathematics, physics, gegraphy, zoology,
    meteorology, geology, psychology, and chemistry

19
procedure make_bin_search_tree(w, n) let T
be the tree with one vertex, root store w1
in root for i 2 to n do begin
v root search true while
search do begin s
word in v if wi lt s then
if v has no left child then add a left
child l to v store wi in l
search false else v left child of
v else if v has
no right child then add a right child r to v
store wi in r search
false else v right child of
v end end end
make_bin_search_tree
20
Binary Search Trees
  • To build a binary search tree for words
  • Old Programmers Never Die They Just Lose Their
    Memories

21
Decision Trees
  • A decision tree is 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.

22
Decision Trees
  • To sort three numbers, a1, a2, and a3.

23
Tree Traversal
  • Ordered rooted trees are used to store
    information, such as arithmetic expressions
    involving numbers, variables, and operations.
  • To traverse a tree, we use a labeling scheme
    called universal address system of the ordered
    rooted tree.
  • Label the root with the integer 0. Then label its
    k children from left to right with 1, 2, 3, ...,
    k.
  • For each vertex v at level n with label A, label
    its children from left to right with A.1, A.2,
    ..., A.m.

24
Tree Traversal
25
Preorder Tree Traversal
  • procedure preorder(PT)
  • if PT is empty then
  • return
  • process PT
  • preorder( the left subtree of PT )
  • preorder( the right subtree of PT )
  • end preorder

26
Preorder Tree Traversal
  • In which order does a preorder traversal visit
    the vertices in the ordered rooted tree T shown?

27
Inorder Tree Traversal
  • procedure inorder(PT)
  • if PT is empty then
  • return
  • inorder( the left subtree of PT )
  • process PT
  • inorder( the right subtree of PT )
  • end inorder

28
Inorder Tree Traversal
  • In which order does an inorder traversal visit
    the vertices in the ordered rooted tree T shown?

29
Infix Form of an Expression
  • Using inorder traversal, we get

Operands A, B, C, D, E Operators , -, , /
30
Arithmetic Expressions
  • The expression ((xy)?2)((x-4)/3) may be
    represented as a rooted binary tree.

31
Arithmetic Expressions
  • What is the inorder traversal of the binary tree?
  • infix form
  • What is the preorder traversal?
  • prefix form

32
Spanning Trees
  • Consider the road system in Minnesota. The
    highway department needs to plow the fewest roads
    in winter, so that, there are always cleared
    roads connecting any two towns.
  • But, how?
  • To find a connected subgraph with the minimum
    number of edges containing all vertices

33
Spanning Trees
34
Spanning Trees
  • A spanning tree of G is a subgraph of G that is a
    tree containing every vertex of G.
  • Example IP multicasting
  • A computer sends a single copy of data over the
    network. For data to reach receiving computers as
    quickly as possible, there should be no loops.

35
Depth-First Search
  • procedure dfs(V, E)
  • w v1 V v1 E ??
  • while V ? V do
  • while there is edge (w, vk) with vk ? V
  • add (w, vk) to E
  • add vk to V
  • w vk
  • w parent of w in T //backtracking
  • end dfs

36
Depth-First Search
  • Choose a vertex as a root.
  • Successively add vertices and edges, where each
    new edge is incident with the last vertex in the
    path and the new vertex is not in the path yet.
  • If the path goes through all vertices of the
    graph, we have the spanning tree.
  • Otherwise, move back to the next to last vertex
    in the path, and form a new path starting at this
    vertex through vertices that were not visited yet.

37
Depth-First Search
  • Find the spanning tree rooted at f

38
Breath-First Search
  • Choose a root vertex
  • Add all edges incident to this vertex.
  • Order all newly added vertices
  • For each new vertex, add each edge incident to
    this vertex as long as it does not form a cycle
  • Repeat steps 3 and 4

39
Breath-First Search
  • procedure bfs(V, E)
  • S (v1), V v1, E ??
  • while true do
  • for each x?? S, in order, do
  • if (x, y) is an edge not forming
    a cycle then
  • add edge (x, y) to E
  • add y to V
  • if no edges were added then
  • return(T)
  • S children of S
  • end bfs

40
Breath-First Search
  • Find the spanning tree rooted at e

41
Minimum spanning Trees
  • A minimum spanning tree of G is a spanning tree
    of G with minimum weight.
  • Example a company plans to build a communication
    network connecting its five computer enters. Any
    pair of these centers can be linked with a leased
    telephone line. Which links should be made to
    ensure that there is a path between any two
    computer centers so that the total cost of the
    network is minimized?

42
Minimum spanning Trees
  • The Prims Algorithm builds a tree by iteratively
    adding edges until a minimal spanning tree is
    obtained. At each iteration, it adds a
    minimum-weight edge that does not complete a
    cycle to the current tree.
  • Prims Algorithm is a greedy algorithm.

43
Minimum spanning Trees
  • procedure prim(w, n, s)
  • for i 1 to n do
  • v(i) 0 v(s) 1 E ??
  • for i 1 to n-1 do
  • min ?
  • for j 1 to n do
  • if v(j) 1 then
  • for k1 to n do
  • if v(k) 0
    and w(j, k) lt min then
  • add_vertex k
  • e
    (j, k) min w(j, k)
  • v( add_vertex ) 1
  • E E ? e
  • end prim

44
Prims Algorithm
45
Prims Algorithm
  • Use Prims algorithm to find a minimum spanning
    tree

46
Isomorphic Trees
  • Let T1 and T2 be rooted trees with roots r1 and
    r2. T1 and T2 are isomorphic if there is a
    one-to-one, onto function f from the vertex set
    of T1 to the vertex set of T2 satisfying
  • Vertices vi and vj are adjacent in T1 if and only
    if the vertices f(vi) and f(vj) are adjacent in
    T2.
  • f(r1) r2.

47
Nonisomorphisms Trees
  • Three nonisomorphic trees with five vertices.
  • Four nonisomorphic trees with four vertices.

48
Isomorphic Binary Trees
  • Let T1 and T2 be binary trees with roots r1 and
    r2. The T1 and T2 are isomorphic if there is a
    non-to-one, onto function f satisfying
  • T1 and T2 are isomorphic tree.
  • v is a left child of w in T1 iff f(v) is the left
    child of f(w) in T2.
  • v is a right child of w in T1 iff f(v) is the
    right child of f(w) in T2.

49
Isomorphic Binary Trees
  • Five nonisomorphic binary trees with three
    vertices

v1
50
Isomorphic Binary Trees
  • procedure bin_tree_isom (r1, r2)
  • if r1 null and r2 null then
  • return (true)
  • if r1 null or r2 null then
  • return (false)
  • lc_r1 left child of r1
  • lc_r2 left child of r2
  • rc_r1 right child of r1
  • rc_r2 right child of r2
  • return ( bin_tree_isom(lc_r1, lc_r2)
  • and bin_tree_isom(rc_r1,
    lc_r2) )
  • end bin_tree_isom
Write a Comment
User Comments (0)
About PowerShow.com